Advertisement
palsushobhan

vendor-wise-individual-use-only-coupons

Oct 8th, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.62 KB | None | 0 0
  1. add_filter('woocommerce_apply_individual_use_coupon', function($keep_coupon, $the_coupon, $applied_coupons) {
  2.     $vendor_wise_coupons = [];
  3.     if( $the_coupon->get_individual_use() ) {
  4.         $coupon_id = $the_coupon->get_id();
  5.         $coupon_vendor = wcfm_get_vendor_id_by_post( $coupon_id );
  6.         if( $coupon_vendor ) {
  7.             foreach ( $applied_coupons as $applied_coupon ) {
  8.                 $this_coupon = new WC_Coupon($applied_coupon);
  9.                 $this_coupon_vendor = wcfm_get_vendor_id_by_post( $this_coupon->get_id() );
  10.                 if( $this_coupon_vendor !== $coupon_vendor) {
  11.                     if( !$this_coupon->get_individual_use()) {
  12.                         $vendor_wise_coupons[$this_coupon_vendor][] = $applied_coupon;
  13.                     } else {
  14.                         $vendor_wise_coupons[$this_coupon_vendor] = [$applied_coupon];
  15.                     }
  16.                 }
  17.             }
  18.             foreach($vendor_wise_coupons as $vendor_id => $coupons) {
  19.                 $keep_coupon = array_merge($keep_coupon, $coupons);
  20.             }
  21.         }
  22.     }
  23.     return array_unique( $keep_coupon );
  24. }, 10, 3);
  25.  
  26. add_filter('woocommerce_apply_with_individual_use_coupon', function($is_allowed, $the_coupon, $individual_coupon, $applied_coupons) {
  27.     $coupon_id = $the_coupon->get_id();
  28.     $coupon_vendor = wcfm_get_vendor_id_by_post( $coupon_id );
  29.     $individual_use_coupon_vendor = wcfm_get_vendor_id_by_post( $individual_coupon->get_id() );
  30.     if( $coupon_vendor && $coupon_vendor !== $individual_use_coupon_vendor ) {
  31.         return true;
  32.     }
  33.     return $is_allowed;
  34. }, 10, 4);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement