1

I need to create 2000 coupons to sell, but I would like the customers who will use them to always pay for shipping. Currently the threshold for getting free shipping is set above 69€. I tried to use the code below (taken from here: Applied coupons disable Free shipping conditionally in Woocommerce).

It applies to all coupons though, and I'd like to apply it only on coupons with the prefix 'pd'.

add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 10, 2 );
function coupons_removes_free_shipping( $rates, $package ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return $rates;

$applied_coupons = WC()->cart->get_applied_coupons();

if( sizeof($applied_coupons) > 0 ) {
    // Loop through shipping rates
    foreach ( $rates as $rate_key => $rate ) {
        // Targeting "Free shipping" only
        if( 'free_shipping' === $rate->method_id  ) {
            unset($rates[$rate_key]); // Removing current method
        }
    }
}
return $rates;
}
karel
  • 5,489
  • 46
  • 45
  • 50
  • Check the output of `WC()->cart->get_applied_coupons();` and see if you can access coupon names through `$applied_coupons`. – dmanexe Mar 08 '19 at 11:37
  • Thank @dmanexe! I'll try! I must say I don't know php that much and it might be hard. – Nikas Bergaglio Mar 08 '19 at 12:48
  • You can use `print_r` to see what an array has: http://php.net/manual/en/function.print-r.php Then you can access specific coupons through this completely made up example: `$applied_coupons["coupon"]["name"]` , completely depending on how `$applied_coupons` is setup. – dmanexe Mar 08 '19 at 12:55
  • Thank @dmanexe! I'll try right away :) – Nikas Bergaglio Mar 08 '19 at 14:28

0 Answers0