WooCommerce add discount based on shipping method
drkskwlkr |
PRO |
05/14/24 08:50:23 PM UTC (Edited) |
0 ⭐ |
1350 👁️ |
Never ⏰ |
[wordpress, woocommerce]
<?php
add_action('woocommerce_cart_calculate_fees', 'add_discount_for_shipping_method', 10, 1);
function add_discount_for_shipping_method($cart) {
// Below check required ONLY if you can't control the deployment of this snippet.
// It **must** execute on FRONT END ONLY.
if (is_admin() && !defined('DOING_AJAX')) {
return;
}
$targeted_shipping_method = 'flat_rate:3'; // Change this depending on your shipping method (slug:id)
$discount = -5; // Applied discount in currency units
$chosen_shipping_methods = WC()->session->get('chosen_shipping_methods');
$chosen_shipping_method = reset($chosen_shipping_methods);
if($chosen_shipping_method == $targeted_shipping_method) {
$cart->add_fee(__('Shipping Discount', 'woocommerce'), $discount);
}
}
Comments