Merge pull request #19148 from woocommerce/tweak/post-processing-custom-coupons
Add filter to allow post processing on custom coupons
This commit is contained in:
commit
58c87e3fcd
|
@ -271,15 +271,7 @@ class WC_Discounts {
|
||||||
$this->apply_coupon_fixed_cart( $coupon, $items_to_apply );
|
$this->apply_coupon_fixed_cart( $coupon, $items_to_apply );
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
foreach ( $items_to_apply as $item ) {
|
$this->apply_coupon_custom( $coupon, $items_to_apply );
|
||||||
$discounted_price = $this->get_discounted_price_in_cents( $item );
|
|
||||||
$price_to_discount = wc_remove_number_precision( ( 'yes' === get_option( 'woocommerce_calc_discounts_sequentially', 'no' ) ) ? $discounted_price : $item->price );
|
|
||||||
$discount = wc_add_number_precision( $coupon->get_discount_amount( $price_to_discount / $item->quantity, $item->object, true ) ) * $item->quantity;
|
|
||||||
$discount = min( $discounted_price, $discount );
|
|
||||||
|
|
||||||
// Store code and discount amount per item.
|
|
||||||
$this->discounts[ $coupon->get_code() ][ $item->key ] += $discount;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -498,6 +490,31 @@ class WC_Discounts {
|
||||||
return $total_discount;
|
return $total_discount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply custom coupon discount to items.
|
||||||
|
*
|
||||||
|
* @since 3.3
|
||||||
|
* @param WC_Coupon $coupon Coupon object. Passed through filters.
|
||||||
|
* @param array $items_to_apply Array of items to apply the coupon to.
|
||||||
|
* @return int Total discounted.
|
||||||
|
*/
|
||||||
|
protected function apply_coupon_custom( $coupon, $items_to_apply ) {
|
||||||
|
foreach ( $items_to_apply as $item ) {
|
||||||
|
$discounted_price = $this->get_discounted_price_in_cents( $item );
|
||||||
|
$price_to_discount = wc_remove_number_precision( ( 'yes' === get_option( 'woocommerce_calc_discounts_sequentially', 'no' ) ) ? $discounted_price : $item->price );
|
||||||
|
$discount = wc_add_number_precision( $coupon->get_discount_amount( $price_to_discount / $item->quantity, $item->object, true ) ) * $item->quantity;
|
||||||
|
$discount = min( $discounted_price, $discount );
|
||||||
|
|
||||||
|
// Store code and discount amount per item.
|
||||||
|
$this->discounts[ $coupon->get_code() ][ $item->key ] += $discount;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allow post-processing for custom coupon types (e.g. calculating discrepancy, etc)
|
||||||
|
$this->discounts[ $coupon->get_code() ] = apply_filters( 'woocommerce_coupon_custom_discounts_array', $this->discounts[ $coupon->get_code() ], $coupon );
|
||||||
|
|
||||||
|
return array_sum( $this->discounts[ $coupon->get_code() ] );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deal with remaining fractional discounts by splitting it over items
|
* Deal with remaining fractional discounts by splitting it over items
|
||||||
* until the amount is expired, discounting 1 cent at a time.
|
* until the amount is expired, discounting 1 cent at a time.
|
||||||
|
|
Loading…
Reference in New Issue