Created
November 26, 2018 16:34
-
-
Save MinaPansuriya/37bcab4e77172e02a742f62c83559899 to your computer and use it in GitHub Desktop.
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
/** | |
* @Title: Wocommerce apply coupon automatically to the cart for specific products | |
* @Author: Mina Pansuriya | |
* @Blog URL :http://minapansuriya.com/wocommerce-apply-coupon-automatically-to-the-cart-for-specific-products/ | |
**/ | |
add_action( 'woocommerce_before_cart', 'pbs_apply_auto_discount_coupons' ); | |
function pbs_apply_auto_discount_coupons() { | |
global $woocommerce; | |
if(is_cart() || is_checkout()) | |
{ | |
$coupon_code = 'Sat$10Off'; // replace this Coupon Code with yours | |
if ( $woocommerce->cart->has_discount( $coupon_code ) ) | |
{ | |
return; | |
} | |
foreach ($woocommerce->cart->get_cart() as $cart_item ) | |
{ | |
$autocoupon = array("263", "15"); // Replace this product IDs with yours | |
if(in_array($cart_item['product_id'], $autocoupon)) | |
{ | |
$woocommerce->cart->add_discount( $coupon_code ); | |
wc_print_notices(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment