php - WooCommerce 折扣 : buy one get one 50% off

标签 php wordpress woocommerce cart discount

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

  • WooCommerce 的定价交易
  • WooCommerce 所有折扣精简版
  • WooCommerce 扩展优惠券功能

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

    不购买插件可以实现吗?

    谢谢

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

    最佳答案

    更新 (与您的评论有关)
    此版本将在此定义的可变产品的购物车中的所有产品变体上全局运行:

    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

    关于php - WooCommerce 折扣 : buy one get one 50% off,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46264431/

    相关文章:

    php - CodeIgniter 中缩短的电子邮件主题

    javascript - 将 PHP 数组转换为 JavaScript 对象

    php - 类别 ID 上的 query_posts 不起作用

    MySQL:返回一个表中以另一个表为条件的值列表

    javascript - 枚举每行 HTML 中的数字

    php - PHP 5.3.17 中的 echo() 产生奇怪的结果

    php - 添加多个类名称以在 Wordpress 中发布缩略图

    html - 如何使用与没有帖子计数不同的帖子计数来设置小部件的样式

    html - 按钮上的可点击区域出错

    php - 即使产品在 WooCommerce 购物车中添加了两次,也将所有项目设置在独立的行中