仅对 Woocommerce 中的第二个购物车商品应用折扣
Apply a discount only on the second cart item in Woocommerce
如何获取和修改我购物车中第二件商品的价格?
我想在第二个产品上打-3% 的折扣(购物车中的商品已经按价格排序,最高的)。
我认为它必须在 woocommerce_before_calculate_totals
中计算或者喜欢 woocommerce_cart_calculate_fees
中的折扣?
谢谢
已更新 (添加了与 Woocommerce 3+ 的兼容性)
对于产品项目,最好使用 woocommerce_before_calculate_totals
操作挂钩:
add_action( 'woocommerce_before_calculate_totals', 'discount_on_2nd_cart_item', 10, 1 );
function discount_on_2nd_cart_item( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Initialising
$count = 0;
$percentage = 3; // 3 %
// Iterating though each cart items
foreach ( $cart->get_cart() as $cart_item ) {
$count++;
if( 2 == $count){ // Second item only
$price = $cart_item['data']->get_price(); // product price
$discounted_price = $price * (1 - ($percentage / 100)); // calculation
// Set the new price
$cart_item['data']->set_price( $discounted_price );
break; // stop the loop
}
}
}
或使用购物车折扣(负购物车费用):
add_action( 'woocommerce_cart_calculate_fees', 'discount_on_2nd_cart_item', 10, 1 );
function discount_on_2nd_cart_item( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Initialising
$count = 0;
$percentage = 3; // 3 %
// Iterating though each cart items
foreach ( $cart->get_cart() as $cart_item ) {
$count++;
if( 2 == $count){ // Second item only
$price = $cart_item['data']->get_price(); // product price
$discount = $price * $percentage / 100; // calculation
$second_item = true;
break; // stop the loop
}
}
if( isset($discount) && $discount > 0 )
$cart->add_fee( __("2nd item 3% discount", 'woocommerce'), -$discount );
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且有效。
如何获取和修改我购物车中第二件商品的价格?
我想在第二个产品上打-3% 的折扣(购物车中的商品已经按价格排序,最高的)。
我认为它必须在 woocommerce_before_calculate_totals
中计算或者喜欢 woocommerce_cart_calculate_fees
中的折扣?
谢谢
已更新 (添加了与 Woocommerce 3+ 的兼容性)
对于产品项目,最好使用 woocommerce_before_calculate_totals
操作挂钩:
add_action( 'woocommerce_before_calculate_totals', 'discount_on_2nd_cart_item', 10, 1 );
function discount_on_2nd_cart_item( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Initialising
$count = 0;
$percentage = 3; // 3 %
// Iterating though each cart items
foreach ( $cart->get_cart() as $cart_item ) {
$count++;
if( 2 == $count){ // Second item only
$price = $cart_item['data']->get_price(); // product price
$discounted_price = $price * (1 - ($percentage / 100)); // calculation
// Set the new price
$cart_item['data']->set_price( $discounted_price );
break; // stop the loop
}
}
}
或使用购物车折扣(负购物车费用):
add_action( 'woocommerce_cart_calculate_fees', 'discount_on_2nd_cart_item', 10, 1 );
function discount_on_2nd_cart_item( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Initialising
$count = 0;
$percentage = 3; // 3 %
// Iterating though each cart items
foreach ( $cart->get_cart() as $cart_item ) {
$count++;
if( 2 == $count){ // Second item only
$price = $cart_item['data']->get_price(); // product price
$discount = $price * $percentage / 100; // calculation
$second_item = true;
break; // stop the loop
}
}
if( isset($discount) && $discount > 0 )
$cart->add_fee( __("2nd item 3% discount", 'woocommerce'), -$discount );
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且有效。