Created
May 15, 2024 11:50
-
-
Save braddalton/d51da2c70321414ce98de223580c15d3 to your computer and use it in GitHub Desktop.
Discount per shipping method in woocommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function apply_shipping_based_discounts() { | |
$chosen_shipping_methods = WC()->session->get('chosen_shipping_methods'); | |
$chosen_shipping_method = $chosen_shipping_methods[0]; | |
$discount = 0; | |
if ($chosen_shipping_method === 'free_shipping') { | |
$discount = 10; // Discount for free shipping | |
} elseif ($chosen_shipping_method === 'flat_rate') { | |
$discount = 5; // Discount for flat rate | |
} elseif ($chosen_shipping_method === 'local_pickup') { | |
$discount = 3; // Discount for local pickup | |
} | |
if ($discount > 0) { | |
WC()->cart->add_fee(__('Shipping Discount', 'woocommerce'), -$discount); | |
} | |
} | |
add_action('woocommerce_cart_calculate_fees', 'apply_shipping_based_discounts'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment