Skip to content

Instantly share code, notes, and snippets.

@kloon
Last active December 27, 2019 15:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kloon/4633463 to your computer and use it in GitHub Desktop.
Save kloon/4633463 to your computer and use it in GitHub Desktop.
WooCommerce Disable Coupons on Sale Items
<?php
// Exclude coupons from being applied when products on sale
add_filter( 'woocommerce_coupon_is_valid', 'woocommerce_coupon_check_sale_items', 10, 2 );
function woocommerce_coupon_check_sale_items( $valid, $coupon ) {
global $woocommerce;
$valid_for_cart = $valid;
if (sizeof($woocommerce->cart->get_cart())>0) : foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) :
if ( function_exists( 'get_product') )
$product = get_product( $cart_item['product_id'] );
else $product = new WC_Product( $cart_item['product_id'] );
if ( $product->is_on_sale() )
$valid_for_cart = false;
endforeach; endif;
if ( ! $valid_for_cart ) $valid = false;
return $valid;
}
?>
@mikakaltoft
Copy link

What does this function do that the "Exclude sale items" check box function does not do?

@hasnatbabur
Copy link

thanks

@ItsmePNP
Copy link

ItsmePNP commented Dec 27, 2019

What does this function do that the "Exclude sale items" check box function does not do?

@mikakaltoft It does it manually, for all coupons. So useful when you have more number of coupons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment