1

I am creating a custom coupon and trying to exclude products to use that specific coupon but when i am adding products id in that code its only adding 1 product in the exclude not others.

 $permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';  
 $bemail= 'email@gmail.com'; 
 $coupon = new WC_Coupon();
 $coupon->set_code( substr(str_shuffle($permitted_chars), 0, 16) . '5489' );
 $coupon->set_amount(5489);
 $coupon->set_excluded_product_ids(1393,1324);
 $coupon->set_email_restrictions($bemail);
 $coupon->save();
Asher
  • 131
  • 3
  • 10

1 Answers1

1

try to pass an array of product ids in set_excluded_product_ids().

example:-

$pids = array(101, 102, 103);
$coupon->set_excluded_product_ids($pids);

Reference:- https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/class-wc-coupon.php#L622

theboss_dev
  • 1,152
  • 1
  • 8
  • 21