代码之家  ›  专栏  ›  技术社区  ›  LoïcR

基于特定商品变化的购物车商品折扣

  •  2
  • LoïcR  · 技术社区  · 6 年前

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

    因为他们有很多不同的模式,客户可以想要10湿,但采取两个不同的包5,得到两个不同的模式。不幸的是,这将花费他2欧元以上,我想避免这种情况。

    我怎样才能发现这种行为,更准确地说,我应该把它挂在哪里?

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

    1 回复  |  直到 6 年前
        1
  •  3
  •   LoicTheAztec    6 年前

    是一个变量积 基于某些产品属性的多种变体。

    对你来说, 折扣可采用两种不同的方式

    但首先需要定义相关的产品属性 这是涉及到“可重复使用的湿”数量包装和相关的术语 姓名 代码中“5包”的值。

    如果没有以正确的方式完成这些任务,代码将无法工作。

    1) :

    这里我们定了一个折扣单价 9 ( 18 / 2 = 9 ) 当购物车中有2个或更多相关项目时。

    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
            }
        }
    }
    

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


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

    enter image description here

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

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

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


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