Ability to exclude ids for coupons

This commit is contained in:
Mike Jolley 2011-11-01 16:25:54 +00:00
parent acdb8baad0
commit 91874aec5e
4 changed files with 61 additions and 11 deletions

View File

@ -36,7 +36,10 @@ function woocommerce_coupon_data_meta_box($post) {
woocommerce_wp_checkbox( array( 'id' => 'individual_use', 'label' => __('Individual use', 'woothemes'), 'description' => __('Check this box if the coupon cannot be used in conjunction with other coupons', 'woothemes') ) );
// Product ids
woocommerce_wp_text_input( array( 'id' => 'product_ids', 'label' => __('Product IDs', 'woothemes'), 'placeholder' => __('N/A', 'woothemes'), 'description' => __('(optional) Comma separate product IDs which need to be in the cart to use this coupon, or for "Product Discounts" are discounted.', 'woothemes') ) );
woocommerce_wp_text_input( array( 'id' => 'product_ids', 'label' => __('Product IDs', 'woothemes'), 'placeholder' => __('N/A', 'woothemes'), 'description' => __('(optional) Comma separate IDs which need to be in the cart to use this coupon or, for "Product Discounts", which products are discounted.', 'woothemes') ) );
// Exclude Product ids
woocommerce_wp_text_input( array( 'id' => 'exclude_product_ids', 'label' => __('Exclude Product IDs', 'woothemes'), 'placeholder' => __('N/A', 'woothemes'), 'description' => __('(optional) Comma separate IDs which must not be in the cart to use this coupon or, for "Product Discounts", which products are not discounted.', 'woothemes') ) );
// Usage limit
woocommerce_wp_text_input( array( 'id' => 'usage_limit', 'label' => __('Usage limit', 'woothemes'), 'placeholder' => __('Unlimited usage', 'woothemes'), 'description' => __('(optional) How many times this coupon can be used before it is void', 'woothemes') ) );
@ -82,6 +85,7 @@ function woocommerce_process_shop_coupon_meta( $post_id, $post ) {
$type = strip_tags(stripslashes( $_POST['discount_type'] ));
$amount = strip_tags(stripslashes( $_POST['coupon_amount'] ));
$product_ids = strip_tags(stripslashes( $_POST['product_ids'] ));
$exclude_product_ids = strip_tags(stripslashes( $_POST['exclude_product_ids'] ));
$usage_limit = (isset($_POST['usage_limit']) && $_POST['usage_limit']>0) ? (int) $_POST['usage_limit'] : '';
$individual_use = isset($_POST['individual_use']) ? 'yes' : 'no';
$expiry_date = strip_tags(stripslashes( $_POST['expiry_date'] ));
@ -91,6 +95,7 @@ function woocommerce_process_shop_coupon_meta( $post_id, $post ) {
update_post_meta( $post_id, 'coupon_amount', $amount );
update_post_meta( $post_id, 'individual_use', $individual_use );
update_post_meta( $post_id, 'product_ids', $product_ids );
update_post_meta( $post_id, 'exclude_product_ids', $exclude_product_ids );
update_post_meta( $post_id, 'usage_limit', $usage_limit );
update_post_meta( $post_id, 'expiry_date', $expiry_date );

View File

@ -419,13 +419,44 @@ class woocommerce_cart {
// Product Discounts
if ($this->applied_coupons) foreach ($this->applied_coupons as $code) :
$coupon = &new woocommerce_coupon( $code );
if ((in_array($values['product_id'], $coupon->product_ids) || in_array($values['variation_id'], $coupon->product_ids))) :
$this_item_is_discounted = false;
// Specific product ID's get the discount
if (sizeof($coupon->product_ids)>0) :
if ((in_array($values['product_id'], $coupon->product_ids) || in_array($values['variation_id'], $coupon->product_ids))) :
$this_item_is_discounted = true;
endif;
else :
// No product ids - all items discounted
$this_item_is_discounted = true;
endif;
// Specific product ID's excluded from the discount
if (sizeof($coupon->exclude_product_ids)>0) :
if ((in_array($values['product_id'], $coupon->exclude_product_ids) || in_array($values['variation_id'], $coupon->exclude_product_ids))) :
$this_item_is_discounted = false;
endif;
endif;
// Apply filter
$this_item_is_discounted = apply_filters( 'woocommerce_item_is_discounted', $this_item_is_discounted, $values );
// Apply the discount
if ($this_item_is_discounted) :
if ($coupon->type=='fixed_product') :
$this->discount_total = $this->discount_total + ( $coupon->amount * $values['quantity'] );
elseif ($coupon->type=='percent_product') :
$this->discount_total = $this->discount_total + ( $total_item_price / 100 ) * $coupon->amount;
endif;
endif;
endforeach;
endif;

View File

@ -30,14 +30,15 @@ class woocommerce_coupon {
if ($coupon && $coupon->post_status == 'publish') :
$this->id = $coupon->ID;
$this->type = get_post_meta($coupon->ID, 'discount_type', true);
$this->amount = get_post_meta($coupon->ID, 'coupon_amount', true);
$this->individual_use = get_post_meta($coupon->ID, 'individual_use', true);
$this->product_ids = array_filter(array_map('trim', explode(',', get_post_meta($coupon->ID, 'product_ids', true))));
$this->usage_limit = get_post_meta($coupon->ID, 'usage_limit', true);
$this->usage_count = (int) get_post_meta($coupon->ID, 'usage_count', true);
$this->expiry_date = ($expires = get_post_meta($coupon->ID, 'expiry_date', true)) ? strtotime($expires) : '';
$this->id = $coupon->ID;
$this->type = get_post_meta($coupon->ID, 'discount_type', true);
$this->amount = get_post_meta($coupon->ID, 'coupon_amount', true);
$this->individual_use = get_post_meta($coupon->ID, 'individual_use', true);
$this->product_ids = array_filter(array_map('trim', explode(',', get_post_meta($coupon->ID, 'product_ids', true))));
$this->exclude_product_ids = array_filter(array_map('trim', explode(',', get_post_meta($coupon->ID, 'exclude_product_ids', true))));
$this->usage_limit = get_post_meta($coupon->ID, 'usage_limit', true);
$this->usage_count = (int) get_post_meta($coupon->ID, 'usage_count', true);
$this->expiry_date = ($expires = get_post_meta($coupon->ID, 'expiry_date', true)) ? strtotime($expires) : '';
if (!$this->amount) return false;
@ -60,7 +61,8 @@ class woocommerce_coupon {
global $woocommerce;
if ($this->id) :
// Product ids
if (sizeof( $this->product_ids )>0) :
$valid = false;
if (sizeof($woocommerce->cart->cart_contents)>0) : foreach ($woocommerce->cart->cart_contents as $cart_item_key => $cart_item) :
@ -71,6 +73,17 @@ class woocommerce_coupon {
if (!$valid) return false;
endif;
// Exclude product ids
if (sizeof( $this->exclude_product_ids )>0) :
$valid = true;
if (sizeof($woocommerce->cart->cart_contents)>0) : foreach ($woocommerce->cart->cart_contents as $cart_item_key => $cart_item) :
if (in_array($cart_item['product_id'], $this->exclude_product_ids) || in_array($cart_item['variation_id'], $this->exclude_product_ids)) :
$valid = false;
endif;
endforeach; endif;
if (!$valid) return false;
endif;
if ($this->usage_limit>0) :
if ($this->usage_count>$this->usage_limit) :
return false;

View File

@ -93,6 +93,7 @@ Yes you can! Join in on our GitHub repository :) https://github.com/woothemes/wo
* Made use of transients to store average ratings and improve performance
* Custom field for product total_sales when sold
* Best sellers widget based on new total_sales field
* Ability to exclude product ids
* Edit category - image fix
* Order Complete email heading fix
* 100% discount when price excludes tax logic fix