Advertisement
drkskwlkr

WooCommerce add discount based on shipping method

May 14th, 2024 (edited)
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.84 KB | Source Code | 0 0
  1. <?php
  2.  
  3. add_action('woocommerce_cart_calculate_fees', 'add_discount_for_shipping_method', 10, 1);
  4. function add_discount_for_shipping_method($cart) {
  5.     // Below check required ONLY if you can't control the deployment of this snippet.
  6.     // It **must** execute on FRONT END ONLY.
  7.     if (is_admin() && !defined('DOING_AJAX')) {
  8.         return;
  9.     }
  10.    
  11.     $targeted_shipping_method = 'flat_rate:3';      // Change this depending on your shipping method (slug:id)
  12.     $discount = -5;                                // Applied discount in currency units
  13.  
  14.     $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods');
  15.     $chosen_shipping_method = reset($chosen_shipping_methods);
  16.  
  17.     if($chosen_shipping_method == $targeted_shipping_method) {
  18.         $cart->add_fee(__('Shipping Discount', 'woocommerce'), $discount);
  19.     }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement