【发布时间】:2021-03-14 20:11:58
【问题描述】:
我正在研究一种方法,如果您的购物车中有超过 1 个产品,则可以提供累进折扣。我找到了this thread 并实际使用了这段代码:
//Discount by Qty Product
add_action( 'woocommerce_before_calculate_totals', 'quantity_based_pricing', 9999 );
function quantity_based_pricing( $cart ) {
global $product;
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
// Define discount rules and thresholds and product ID
$threshold1 = 2; // Change price if items > 1
$discount1 = 10; // Reduce unit flat price by 10
$threshold2 = 3; // Change price if items > 2
$discount2 = 20; // Reduce unit flat price by 20
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
if ( $product_in_cart === $product_id && $cart_item['quantity'] >= $threshold1 && $cart_item['quantity'] < $threshold2 ) {
$price = round( $cart_item['data']->get_price() - $discount1, 2 );
$cart_item['data']->set_price( $price );
} elseif ( $product_in_cart === $product_id && $cart_item['quantity'] >= $threshold2 ) {
$price = round( $cart_item['data']->get_price() - $discount2, 2 );
$cart_item['data']->set_price( $price );
}
}
}
为了使其更具动态性,我想为特定类别制定此规则,因此每次我在该类别中添加产品时,都会应用该规则。 我尝试使用
is_product_category ('example')if (is_product_category () && has_term ('example', 'product_cat') return;
我没有成功,谁能告诉我如何创造这个条件。
【问题讨论】:
标签: php wordpress woocommerce cart discount