【问题标题】:Buy one get second 50% off for WooCommerce product variationsWooCommerce 产品变体买一送二 50% 折扣
【发布时间】:2020-04-15 07:39:47
【问题描述】:

我希望为特定可变产品设置特定折扣,但针对某些选定的变体而不是所有变体: 例如:我的变量 id - 1571
变体 ID - 1572
变体 ID - 1573

因此,如果客户购买一种产品,他们会以 50% 的折扣获得另一种(相同的)产品(购买一种产品可获得另一种 50% 的折扣)。

我尝试了很多折扣插件,我发现最接近的是:

通过其中一些,我能够设置小计折扣或每种产品的折扣,但不完全是我想要的(买 1 送 1)。还有其他专业插件我不想去。

我找到的最近的代码是WooCommerce discount: buy one get one 50% off with a notice

是否可以针对可变产品的特定产品变体对第二件商品进行折扣(仅针对每个产品变体)

  1. 马克

【问题讨论】:

  • 当您询问代码来自现有答案代码而不对其进行真正更改的问题时,请不要在您的问题中使用它,并始终将相关链接添加到原始答案中…

标签: wordpress woocommerce cart discount


【解决方案1】:

要为可变产品的某些特定产品变体在第二件商品上享受 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文件中经过测试并且可以工作。


要为购买的每件商品以 50% 的折扣购买一件商品,而不是购买第二件商品的 50% 折扣,请替换:

$discount -= number_format( $price / 2, 2 );

作者:

$multiplier = ( $qty % 2 ) === 0 ? $qty / 2 : ( $qty - 1 ) / 2;
$discount  -= number_format( $price / 2 * $multiplier, 2 );

要获得第二个免费而不是第二个项目的 50% OFF,请替换代码行:

$discount -= number_format( $price / 2, 2 );

作者:

$discount -= $price;

要为购买的每件商品免费获得一件商品,而不是第二件商品的 50% 折扣,请更换:

$discount -= number_format( $price / 2, 2 );

作者:

$multiplier = ( $qty % 2 ) === 0 ? $qty / 2 : ( $qty - 1 ) / 2;
$discount  -= $price * $multiplier;

【讨论】:

    【解决方案2】:

    你真的很亲密!
    折扣金额计算错误

    add_action('woocommerce_cart_calculate_fees', 'add_custom_discount_2nd_at_50', 10, 1 );
    function add_custom_discount_2nd_at_50( $wc_cart ){
    
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    $discount = 0;
    $items_prices = array();
    $qty_notice = 0;  //  <==  Added HERE
    
    // Set HERE your targeted variable product ID
    $targeted_product_id = 1571 ;
    
    foreach ( $wc_cart->get_cart() as $key => $cart_item ) {
        if( $cart_item['product_id'] == $targeted_product_id ){
            $qty = intval( $cart_item['quantity'] );
            $qty_notice += intval( $cart_item['quantity'] ); //  <==  Added HERE
            for( $i = 0; $i < $qty; $i++ )
                $items_prices[] = floatval( $cart_item['data']->get_price());
        }
    }
    $count_items_prices = count($items_prices);
    //to get the discount of lowest price sorting in descending order 
    rsort($items_prices);
    if( $count_items_prices > 1 ) foreach( $items_prices as $key => $price )
        if( $key % 2 == 1 ) $discount -= number_format($price / 2, 2 );
    if( $discount != 0 ){
      // The discount
      # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    $wc_cart->add_fee('50% off de segunda almohada' , (($price/2)*($discount))^⁻1, true  ); //EDITED
      // Displaying a custom notice (optional)
      wc_clear_notices();
      if(!is_checkout()){
      wc_add_notice( __("Hurrah!! You got 50% off discount on the 2nd item"), 'notice');
    }}
    //  Display a custom notice on cart page when quantity is equal to 1.
    elseif( $qty_notice == 1){ 
        wc_clear_notices();
      if(!is_checkout()){
        wc_add_notice( __( "Add one more to get 50% off on 2nd item" ), 'notice');
    }}
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-12
      • 2018-02-26
      • 2018-03-08
      • 2020-12-31
      • 1970-01-01
      • 2019-02-06
      • 2020-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多