Added 'Exclude Sale Products' option to coupons

Added exclude sale products functionality based of the 'exclude product
ids' functionality in wc-coupon/wc-cart.

Compares product ID's in cart vs ID's in the array
'woocommerce_get_product_ids_on_sale'
This commit is contained in:
Alex Bishop 2013-01-29 13:54:54 +10:30
parent 4119f00b0c
commit b763c7449a
3 changed files with 50 additions and 0 deletions

View File

@ -56,6 +56,9 @@ function woocommerce_coupon_data_meta_box( $post ) {
// Apply before tax
woocommerce_wp_checkbox( array( 'id' => 'apply_before_tax', 'label' => __( 'Apply before tax', 'woocommerce' ), 'description' => __( 'Check this box if the coupon should be applied before calculating cart tax.', 'woocommerce' ) ) );
// Exclude Sale Products
woocommerce_wp_checkbox( array( 'id' => 'exclude_sale_items', 'label' => __( 'Exclude sale item', 'woocommerce' ), 'description' => __( 'Check this box if the coupon should not apply to sale items. Be sure when ticking this box for cart coupons as the presence of any sale item in the cart will stop the coupon from applying', 'woocommerce' ) ) );
echo '</div><div class="options_group">';
// minimum spend
@ -199,6 +202,7 @@ function woocommerce_process_shop_coupon_meta( $post_id, $post ) {
$expiry_date = woocommerce_clean( $_POST['expiry_date'] );
$apply_before_tax = isset( $_POST['apply_before_tax'] ) ? 'yes' : 'no';
$free_shipping = isset( $_POST['free_shipping'] ) ? 'yes' : 'no';
$exclude_sale_items = isset( $_POST['exclude_sale_items'] ) ? 'yes' : 'no';
$minimum_amount = woocommerce_clean( $_POST['minimum_amount'] );
$customer_email = array_filter( array_map( 'trim', explode( ',', woocommerce_clean( $_POST['customer_email'] ) ) ) );
@ -227,6 +231,7 @@ function woocommerce_process_shop_coupon_meta( $post_id, $post ) {
update_post_meta( $post_id, 'expiry_date', $expiry_date );
update_post_meta( $post_id, 'apply_before_tax', $apply_before_tax );
update_post_meta( $post_id, 'free_shipping', $free_shipping );
update_post_meta( $post_id, 'exclude_sale_items', $exclude_sale_items );
update_post_meta( $post_id, 'product_categories', $product_categories );
update_post_meta( $post_id, 'exclude_product_categories', $exclude_product_categories );
update_post_meta( $post_id, 'minimum_amount', $minimum_amount );

View File

@ -984,6 +984,12 @@ class WC_Cart {
if ( sizeof( array_intersect( $product_cats, $coupon->exclude_product_categories ) ) > 0 )
$this_item_is_discounted = false;
// Sale Items excluded from discount
if ( $coupon->exclude_sale_items() && sizeof( woocommerce_get_product_ids_on_sale() ) > 0 )
$product_ids_on_sale = woocommerce_get_product_ids_on_sale()
if ( in_array( $values['product_id'], $product_ids_on_sale ) || in_array( $values['variation_id'], $product_ids_on_sale ) || in_array( $values['data']->get_parent(), $product_ids_on_sale ) )
$this_item_is_discounted = false;
// Apply filter
$this_item_is_discounted = apply_filters( 'woocommerce_item_is_discounted', $this_item_is_discounted, $values, $before_tax = true, $coupon );
@ -1146,6 +1152,12 @@ class WC_Cart {
if ( sizeof( array_intersect( $product_cats, $coupon->exclude_product_categories ) ) > 0 )
$this_item_is_discounted = false;
// Sale Items excluded from discount
if ( $coupon->exclude_sale_items() && sizeof( woocommerce_get_product_ids_on_sale() ) > 0 )
$product_ids_on_sale = woocommerce_get_product_ids_on_sale()
if ( in_array( $values['product_id'], $product_ids_on_sale ) || in_array( $values['variation_id'], $product_ids_on_sale ) || in_array( $values['data']->get_parent(), $product_ids_on_sale ) )
$this_item_is_discounted = false;
// Apply filter
$this_item_is_discounted = apply_filters( 'woocommerce_item_is_discounted', $this_item_is_discounted, $values, $before_tax = false, $coupon );

View File

@ -53,6 +53,9 @@ class WC_Coupon {
/** @public array Array of category ids. */
public $exclude_product_categories;
/** @public string "yes" if coupon does NOT apply to items on sale. */
public $exclude_sale_items;
/** @public string Minimum cart amount. */
public $minimum_amount;
@ -98,6 +101,7 @@ class WC_Coupon {
$this->free_shipping = esc_html( $coupon_data['free_shipping'] );
$this->product_categories = is_array( $coupon_data['product_categories'] ) ? $coupon_data['product_categories'] : array();
$this->exclude_product_categories = is_array( $coupon_data['exclude_product_categories'] ) ? $coupon_data['exclude_product_categories'] : array();
$this->exclude_sale_items = esc_html( $coupon_data['exclude_sale_items'] );
$this->minimum_amount = esc_html( $coupon_data['minimum_amount'] );
$this->customer_email = esc_html( $coupon_data['customer_email'] );
@ -131,6 +135,7 @@ class WC_Coupon {
'free_shipping' => 'no',
'product_categories' => array(),
'exclude_product_categories' => array(),
'exclude_sale_items' => 'no',
'minimum_amount' => '',
'customer_email' => array()
);
@ -173,6 +178,16 @@ class WC_Coupon {
public function enable_free_shipping() {
return $this->free_shipping == 'yes' ? true : false;
}
/**
* Check if a coupon excludes sale items.
*
* @access public
* @return void
*/
public function exclude_sale_items() {
return $this->exclude_sale_items == 'yes' ? true : false;
}
/**
@ -301,6 +316,23 @@ class WC_Coupon {
}
}
// Exclude Sale Items
if ( exclude_sale_items() && sizeof( $woocommerce_get_product_ids_on_sale) > 0 ) {
$valid_for_cart = true;
$product_ids_on_sale = woocommerce_get_product_ids_on_sale();
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( in_array( $cart_item['product_id'], $product_ids_on_sale ) || in_array( $cart_item['variation_id'], $product_ids_on_sale ) || in_array( $cart_item['data']->get_parent(), $product_ids_on_sale ) ) {
$valid_for_cart = false;
}
}
}
if ( ! $valid_for_cart ) {
$valid = false;
$error = __( 'Sorry, this coupon is not applicable if your cart contains sale items.', 'woocommerce' );
}
}
// Exclude Categories
if ( sizeof( $this->exclude_product_categories ) > 0 ) {
$valid_for_cart = true;
@ -318,6 +350,7 @@ class WC_Coupon {
$error = __( 'Sorry, this coupon is not applicable to your cart contents.', 'woocommerce' );
}
}
}
$valid = apply_filters( 'woocommerce_coupon_is_valid', $valid, $this );