我的目的是为带有特定产品标签的WooCommerce产品添加多个批量动态定价。我使用了"小格式">
基于带有特定产品标签答案代码的WooCommerce产品的批量动态定价,这是我的尝试:
function bbloomer_quantity_based_pricing( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
// count tag found
$tag_found = 0;
// Loop through cart items, count how many times tag occurs
foreach ( $cart->get_cart() as $cart_item ) {
// Get an instance of the WC_Product object
$product = $cart_item['data'];
// Get product id
$product_id = $cart_item['product_id'];
// if product has tag
if( has_term( 'petit-format', 'product_tag', $product_id ) ) {
// Get quantity from product in cart
$quantity = $cart_item['quantity'];
// if product quantity > 1
if ( $quantity > 1) {
$tag_found = $tag_found + $quantity;
} else {
$tag_found += 1;
}
}
}
// Define discount rules and thresholds
$threshold1 = 2; // Change price if items > 2
$discount1 = 0.142857; // Reduce unit price by 5%
$threshold2 = 3; // Change price if items > 3
$discount2 = 0.285714; // Reduce unit price by 10%
$threshold3 = 6;
$discount3 = 0.428571;
$threshold4 = 10;
$discount4 = 0.5;
$threshold5 = 20;
$discount5 = 0.571429;
$threshold6 = 50;
$discount6 = 0.607143;
$threshold7 = 100;
// if tag found >= $threshold
if ( $tag_found >= $threshold1 ) {
// Loop through cart items, add discount
foreach ( $cart->get_cart() as $cart_item ) {
// Get an instance of the WC_Product object
$product = $cart_item['data'];
// Get product id
$product_id = $cart_item['product_id'];
// if product has tag
if( has_term( 'petit-format', 'product_tag', $product_id ) ) {
// calculate new price
$price = round( $cart_item['data']->get_price() * ( 1 - $discount1 ), 2 );
// set new price
$cart_item['data']->set_price( $price );
}
}
}
elseif ( $tag_found >= $threshold2 ) {
// Loop through cart items, add discount
foreach ( $cart->get_cart() as $cart_item ) {
// Get an instance of the WC_Product object
$product = $cart_item['data'];
// Get product id
$product_id = $cart_item['product_id'];
// if product has tag
if( has_term( 'petit-format', 'product_tag', $product_id ) ) {
// calculate new price
$price = round( $cart_item['data']->get_price() * ( 1 - $discount2 ), 2 );
// set new price
$cart_item['data']->set_price( $price );
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'bbloomer_quantity_based_pricing', 10, 1 );
但是我只有第一个折扣有效,而另一个不起作用。
$discount1
作品,但功能不使用第二个折扣。任何建议吗?
我的例子只有两个折扣,但我需要创建10个以上的折扣。
没有必要在每个条件下再次遍历整个购物车,反向应用
- 通过
if/elseif/else
条件可获得多重折扣 - 我使用了WC_Cart:: get_cart_item_quantity()而不是在第一个
foreach
循环中循环购物车。因为这样更快
得到
function action_woocommerce_before_calculate_totals( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
// Settings
$term = 'petit-format';
$taxonomy = 'product_tag';
// Initialize
$counter = 0;
// Loop through cart items, count how many times tag occurs
foreach( WC()->cart->get_cart_item_quantities() as $product_id => $cart_item_quantity ) {
// Contains the definite term
if ( has_term( $term, $taxonomy, $product_id ) ) {
// Add the cart item quantity from this certain product to the counter
$counter += $cart_item_quantity;
}
}
// Define discount rules and thresholds
if ( $counter >= 1 ) {
// Less than or equal to 2
if ( $counter <= 2 ) {
$discount = 0.05; // Reduce unit price by 5%
// Between 2 and 10
} elseif ( $counter > 2 && $counter <= 10 ) {
$discount = 0.10; // Reduce unit price by 10%
// Between 10 and 20
} elseif ( $counter > 10 && $counter <= 20 ) {
$discount = 0.20; // Reduce unit price by 20%
// Between 20 and 50
} elseif ( $counter > 20 && $counter <= 50 ) {
$discount = 0.50; // Reduce unit price by 50%
// Default
} else {
$discount = 0.05; // Reduce unit price by 5%
}
// Loop through cart items, add discount
foreach ( $cart->get_cart() as $cart_item ) {
// Get product ID in
$product_id = $cart_item['product_id'];
// Contains the definite term
if ( has_term( $term, $taxonomy, $product_id ) ) {
// calculate new price
$price = round( $cart_item['data']->get_price() * ( 1 - $discount ), 2 );
// Set new price
$cart_item['data']->set_price( $price );
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );