特定产品类别的 WooCommerce 累进数量折扣
WooCommerce progressive quantity discount for specific product categories
我正在研究一种方法,如果您的购物车中有多个产品,则可以提供累进折扣。我发现 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;
我没有成功,谁能告诉我如何创造这个条件。
您的代码中缺少针对特定产品类别的内容。请尝试以下操作:
add_action( 'woocommerce_before_calculate_totals', 'quantity_based_pricing', 9999 );
function quantity_based_pricing( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Define categories and discount rules (quantity threshold and discount amount)
$categories = array('example');
$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
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// Target specific product category(ies)
if ( has_term ($categories, 'product_cat', $cart_item['product_id']) ) {
if ( $cart_item['quantity'] >= $threshold1 && $cart_item['quantity'] < $threshold2 ) {
$price = round( $cart_item['data']->get_price() - $discount1, 2 );
$cart_item['data']->set_price( $price );
} elseif ( $cart_item['quantity'] >= $threshold2 ) {
$price = round( $cart_item['data']->get_price() - $discount2, 2 );
$cart_item['data']->set_price( $price );
}
}
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
我正在研究一种方法,如果您的购物车中有多个产品,则可以提供累进折扣。我发现 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;
我没有成功,谁能告诉我如何创造这个条件。
您的代码中缺少针对特定产品类别的内容。请尝试以下操作:
add_action( 'woocommerce_before_calculate_totals', 'quantity_based_pricing', 9999 );
function quantity_based_pricing( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Define categories and discount rules (quantity threshold and discount amount)
$categories = array('example');
$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
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// Target specific product category(ies)
if ( has_term ($categories, 'product_cat', $cart_item['product_id']) ) {
if ( $cart_item['quantity'] >= $threshold1 && $cart_item['quantity'] < $threshold2 ) {
$price = round( $cart_item['data']->get_price() - $discount1, 2 );
$cart_item['data']->set_price( $price );
} elseif ( $cart_item['quantity'] >= $threshold2 ) {
$price = round( $cart_item['data']->get_price() - $discount2, 2 );
$cart_item['data']->set_price( $price );
}
}
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。