Revert "Reset, please ignore"

This reverts commit 3e59e64da3.
This commit is contained in:
Alex Bishop 2013-01-29 23:41:57 +10:30
parent 3e59e64da3
commit 13de8d9203
4 changed files with 45 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

@ -954,6 +954,7 @@ class WC_Cart {
$this_item_is_discounted = false;
$product_cats = wp_get_post_terms( $values['product_id'], 'product_cat', array("fields" => "ids") );
$product_ids_on_sale = woocommerce_get_product_ids_on_sale();
// Specific products get the discount
if ( sizeof( $coupon->product_ids ) > 0 ) {
@ -984,6 +985,11 @@ 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 == 'yes' )
if ( in_array( $values['product_id'], $product_ids_on_sale, true ) || in_array( $values['variation_id'], $product_ids_on_sale, true ) || in_array( $values['data']->get_parent(), $product_ids_on_sale, true ) )
$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 );
@ -1147,6 +1153,11 @@ 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 == 'yes' )
if ( in_array( $values['product_id'], $product_ids_on_sale, true ) || in_array( $values['variation_id'], $product_ids_on_sale, true ) || in_array( $values['data']->get_parent(), $product_ids_on_sale, true ) )
$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()
);
@ -311,6 +316,23 @@ class WC_Coupon {
}
}
// Exclude Sale Items
if ( $this->exclude_sale_items == 'yes' ) {
$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, true ) || in_array( $cart_item['variation_id'], $product_ids_on_sale, true ) || in_array( $cart_item['data']->get_parent(), $product_ids_on_sale, true ) ) {
$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;

View File

@ -192,4 +192,11 @@ $available_methods = $woocommerce->shipping->get_available_shipping_methods();
<?php do_action( 'woocommerce_after_cart_totals' ); ?>
<div class="debug">
<?php
print_r(woocommerce_get_product_ids_on_sale() );
print_r( $woocommerce )
?>
</div>
</div>