作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望为特定的可变产品设置特定折扣,但对于某些选定的变体而不是所有变体:
例如:我的变量 ID - 1571
变体 ID - 1572
变体 ID - 1573
因此,如果客户购买一种产品,他们会以 50% 的折扣获得另一种(相同的)产品(购买一件产品可享受 50% 的折扣)。
我尝试了很多折扣插件,我发现最接近的是:
最佳答案
要为可变产品的某些特定产品变体在第二个项目上获得 50% 的折扣,您将使用以下内容:
add_action('woocommerce_cart_calculate_fees', 'add_custom_discount_2nd_at_50', 10, 1 );
function add_custom_discount_2nd_at_50( $cart ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// YOUR SETTINGS:
$product_variations_ids = array(1572, 1573); // <== HERE your targeted product variations
// Initializing variables
$discount = 0;
$product_names = array();
// Loop through cart items
foreach ( $cart->get_cart() as $key => $cart_item ) {
if ( in_array( $cart_item['variation_id'], $product_variations_ids ) ) {
$qty = (int) $cart_item['quantity'];
$price = (float) $cart_item['data']->get_price();
$name = (string) $cart_item['data']->get_name();
if ( $qty > 1 ) {
$discount -= number_format( $price / 2, 2 );
}
elseif( $qty = 1 ) {
$product_names[] = $name;
}
}
}
// Applying the discount
if( $discount != 0 ){
$cart->add_fee('Buy one get one 50% off', $discount );
}
// Display a custom reminder notice on cart page (otional)
if( ! empty($product_names) ){
wc_clear_notices(); // clear other notices on checkout page.
if( ! is_checkout() ){
wc_add_notice( sprintf(
__( "Add one more to get 50%% off on the 2nd item for %s" ),
'"<strong>' . implode(', ', $product_names) . '</strong>"'
), 'notice' );
}
}
}
代码位于您的事件子主题(或事件主题)的 functions.php 文件中,经过测试并有效。
$discount -= number_format( $price / 2, 2 );
经过:
$multiplier = ( $qty % 2 ) === 0 ? $qty / 2 : ( $qty - 1 ) / 2;
$discount -= number_format( $price / 2 * $multiplier, 2 );
$discount -= number_format( $price / 2, 2 );
经过:
$discount -= $price;
$discount -= number_format( $price / 2, 2 );
经过:
$multiplier = ( $qty % 2 ) === 0 ? $qty / 2 : ( $qty - 1 ) / 2;
$discount -= $price * $multiplier;
关于wordpress - WooCommerce 产品变体买一送二 50%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61223671/
跟随这个问题Generate a fictitious stock option price variation 我想模拟价格变化,同时用户发出买入或卖出指令,就像真实的证券交易所一样。 (我做了一个
我是一名优秀的程序员,十分优秀!