Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MinaPansuriya/37bcab4e77172e02a742f62c83559899 to your computer and use it in GitHub Desktop.
Save MinaPansuriya/37bcab4e77172e02a742f62c83559899 to your computer and use it in GitHub Desktop.
/**
* @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