Я ищу хук Woocommerce, который поможет изменить процент скидки на основе ограничений двух разных категорий продуктов при применении определенного купона.
Например, если клиент добавит конкретный купон, я хотел бы:
Если товар в корзине относится к категории продуктов А, то он дает скидку 10% на этот товар.
если он относится к категории продуктов B, он даст 20% скидку на этот товар
Я нашел этот код, но он все еще получает скидку на купон от Coupons в WooCommerce.
add_filter( 'woocommerce_coupon_get_discount_amount', 'alter_shop_coupon_data', 20, 5 );
function alter_shop_coupon_data( $round, $discounting_amount, $cart_item, $single, $coupon ){
## ---- Your settings ---- ##
// Related coupons codes to be defined in this array (you can set many)
$coupon_codes = array('10percent');
// Product categories at 20% (IDs, Slugs or Names) for 20% of discount
$product_category20 = array('hoodies'); // for 20% discount
$second_percentage = 0.2; // 20 %
## ---- The code: Changing the percentage to 20% for specific a product category ---- ##
if ( $coupon->is_type('percent') && in_array( $coupon->get_code(), $coupon_codes ) ) {
if( has_term( $product_category20, 'product_cat', $cart_item['product_id'] ) ){
$original_coupon_amount = (float) $coupon->get_amount();
$discount = $original_coupon_amount * $second_percentage * $discounting_amount;
$round = round( min( $discount, $discounting_amount ), wc_get_rounding_precision() );
}
}
return $round;
}