【问题标题】:Cart item discount based on specific product variation in Woocommerce基于 Woocommerce 中特定产品变体的购物车商品折扣
【发布时间】:2019-02-06 15:46:28
【问题描述】:

我在我的 DIY 销售商店有一款名为“可重复使用的湿”的产品,它有不同的图案和不同的包装。

  • 5 可重复使用的湿成本 10 欧元
  • 10 可重复使用的湿成本 18 欧元

由于它们有很多不同的图案,客户可能想要 10 个湿的,但拿两个不同的包装 5 个,得到两个不同的图案。不幸的是,这会让他多花 2 欧元,我想避免这种情况。

我怎样才能检测到这种行为,更准确地说,我应该在哪里挂钩?

我想把它放在购物车预览中,如果我检测到这些包裹,可能会自动添加折扣券,但我不确定这是否是最有效的方法。

任何建议都会对我有所帮助。

【问题讨论】:

    标签: php woocommerce cart coupon discount


    【解决方案1】:

    您的产品名为“可重复使用的湿”似乎是一种可变产品,根据某些产品属性具有多种变体。

    因此,在您的情况下,可以通过 2 种不同方式应用折扣。

    但首先您需要定义“可重复使用的湿”数量包装中涉及的相关产品属性 SLUG 以及“包装的”相关术语 NAME 值5" 在代码中。

    如果设置不正确,代码将无法运行。

    1) 更改相关商品价格 (最好的方法):

    在这里,当购物车中有 2 件或更多相关商品时,我们将折扣单价设置为 9(18 / 2 = 9)。

    add_action( 'woocommerce_before_calculate_totals', 'conditionally_set_discounted_price', 30, 1 );
    function conditionally_set_discounted_price( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        // HERE your product attribute slug for "Reusable wet" (without "pa_")
        $product_attr = 'reusable-wet';
        $product_attr = 'color';
    
        // HERE set the corresponding (value) term NAME for "5 Reusable wet"
        $term_slug = '5 reusable wet';
        $term_slug = 'Black';
    
        // HERE set the discounted price for each cart item with "5 Reusable wet" when they are 2 or more
        $discounted_price = 9; // (18 / 2 = 9)
    
        $five_rw_count = 0; // Initializing variable
    
        // 1st Loop: count cart items with "5 Reusable wet"
        foreach ( $cart->get_cart() as $cart_item ){
            if( $cart_item['data']->get_attribute( $product_attr ) == $term_slug ){
                $five_rw_count += $cart_item['quantity'];
            }
        }
    
        // Continue if there is at least 2 units of "5 Reusable wet"
        if( $five_rw_count < 2 ) return; // Exit
    
        // 2nd Loop: set the discounted price for cart items with "5 Reusable wet"
        foreach ( $cart->get_cart() as $cart_item ){
            if( $cart_item['data']->get_attribute( $product_attr ) == $term_slug ){
                $cart_item['data']->set_price($discounted_price); // Set the discounted price
            }
        }
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。测试和工作


    2) 优惠券方式 (由于许多逻辑原因不是很方便):

    您必须在此处设置优惠券代码。此优惠券设置应如下所示:

    您将在“产品”字段中设置所有相关产品变体。

    完成后,您将在以下代码中设置优惠券名称(小写):

    add_action( 'woocommerce_before_calculate_totals', 'conditionally_auto_add_coupon', 30, 1 );
    function conditionally_auto_add_coupon( $cart ) {
        if ( is_admin() && !defined('DOING_AJAX') ) return; // Exit
    
        // HERE your product attribute slug for "Reusable wet" (without "pa_")
        $product_attr = 'reusable-wet';
    
        // HERE set the corresponding (value) term NAME for "5 Reusable wet"
        $term_slug = '5 reusable wet';
    
        // HERE set the coupon code (in lowercase)
        $coupon_code = 'multiplefive';
    
        $five_rw_count = 0; // Initializing variable
    
        // 1st Loop: count cart items with "5 Reusable wet"
        foreach ( $cart->get_cart() as $cart_item ){
            if( $cart_item['data']->get_attribute( $product_attr ) == $term_slug ){
                $five_rw_count++; // Increasing count
            }
        }
    
        // Apply the coupon if there is at least 2 units of "5 Reusable wet"
        if ( ! $cart->has_discount( $coupon_code ) && $five_rw_count >= 2 ) {
            $cart->add_discount( $coupon_code );  
        } 
        // Remove the coupon if there is at less than 2 units of "5 Reusable wet"
        elseif  ( $cart->has_discount( $coupon_code ) && $five_rw_count < 2 ) {
            $cart->remove_coupon( $coupon_code );
        }
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。测试和工作


    相关:Auto apply or remove a coupon in Woocommerce cart for a specific product id

    【讨论】:

    • 去了第一个解决方案,除了我过滤了 categoryId 和价格。非常感谢! :)
    • 有没有办法捕捉所有产品的特定变化。因此,如果我有一个名为“盒装”的变体和多种尺寸的“盒装”,优惠券将适用于包含“盒装”一词但不包含“未装箱”的所有变体
    猜你喜欢
    • 2019-01-31
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 2017-06-05
    • 2011-04-24
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    相关资源
    最近更新 更多