【问题标题】:WooCommerce discount: buy one get one 50% offWooCommerce 折扣:买一送一 50% 折扣
【发布时间】:2018-02-26 02:43:26
【问题描述】:

我希望为特定的可变产品设置特定折扣,如果客户购买一种产品,他们会以 50% 的折扣获得另一种(相同的)产品(购买一种产品可获得 50% 的折扣)。我试过很多折扣插件,我发现最接近的是:

  • WooCommerce 的定价交易

  • WooCommerce All Discounts Lite

  • WooCommerce 扩展优惠券功能

通过使用这些插件,我可以设置小计折扣或每个产品的折扣,但不完全是我想要的(买 1 送 1)。还有其他专业插件我不想去。

不购买插件可以实现吗?

谢谢

找到类似的东西 https://www.fldtrace.com/buy-3-get-1-free-coupon-woocommerce

【问题讨论】:

标签: php wordpress woocommerce cart discount


【解决方案1】:

更新 (与您的 cmets 有关)

此版本将适用于购物车中此已定义变量产品的所有产品变体:

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();

    // Set HERE your targeted variable product ID
    $targeted_product_id = 40;

    foreach ( $wc_cart->get_cart() as $key => $cart_item ) {
        if( $cart_item['product_id'] == $targeted_product_id ){
            $qty = intval( $cart_item['quantity'] );
            for( $i = 0; $i < $qty; $i++ )
                $items_prices[] = floatval( $cart_item['data']->get_price());
        }
    }
    $count_items_prices = count($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 ){
        // Displaying a custom notice (optional)
        wc_clear_notices();
        wc_add_notice( __("You get 50% of discount on the 2nd item"), 'notice');

        // The discount
        $wc_cart->add_fee( 'Discount 2nd at 50%', $discount, true  );
        # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    }
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

此代码在 Woocommerce 3+ 上经过测试并且可以正常工作。


原答案:

有很多方法可以为特定可变产品 ID 的第二件商品添加 50% 的自定义折扣。下面我使用带有负值的add_fee() 方法(所以它增加了折扣)
(可选)它会显示自定义通知:

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;

    // Set HERE your targeted variable product ID
    $targeted_product_id = 40;

    foreach ( $wc_cart->get_cart() as $key => $cart_item ) {
        if( $cart_item['product_id'] == $targeted_product_id ){
            $price = $cart_item['data']->get_price();
            $quantity = intval( $cart_item['quantity'] );
            for( $i = 1, $j = 0; $i <= $quantity; $i++ ){
                if( $i % 2 == 0 &&  $quantity > 1 ) $j++;
            }
            if( $quantity > 1 ) number_format($discount -= $price * $j / 2, 2 );
        }
    }
    if( $discount != 0 ){
        // Displaying a custom notice (optional)
        wc_clear_notices();
        wc_add_notice( __("You get 50% of discount on the 2nd item"), 'notice');
        
        $wc_cart->add_fee( 'Discount 2nd at 50%', $discount, true  );
        # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    }
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

此代码在 Woocommerce 3+ 上经过测试并且可以正常工作。

相关:WooCommerce discount: buy one get one 50% off with a notice

【讨论】:

  • 感谢@loictheaztec 的帮助,我已经尝试了代码,只有当我添加超过 1 个具有相同变化的相同产品时,它才能完美运行。但是,当我尝试添加同一产品的不同变体时,它不起作用。 这里是可能有帮助的截图 ibb.co/k7CtzQ ibb.co/j1v2tk
  • @abdul 仅当所有变体的价格都相同时,才能对多个变体进行此操作。因此,如果价格相同,我可以做到。
  • 对不同价格的变化有什么建议吗?如果不是,那么我认为我必须调整价格并使用相同的价格。
  • 我会更乐意标记它,感谢您的努力和帮助。非常感谢......你太棒了:) @loictheaztec
  • 我认为我们可以在$count_items_prices = count($items_prices); 之后添加rsort($items_prices); 以获得最低价格的折扣。
猜你喜欢
  • 2018-03-08
  • 1970-01-01
  • 2020-11-12
  • 2020-12-31
  • 1970-01-01
  • 2014-02-12
  • 2016-10-07
  • 1970-01-01
  • 2023-03-31
相关资源
最近更新 更多