Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* @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