dsafgdafgdf45345 2018-08-31 15:10
浏览 64
已采纳

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

I’ve, on my DIY sell store, a product named ‘Reusable wet’ which come in different patterns and different packages.

  • 5 Reusable wet cost €10
  • 10 Reusable wet cost €18

Since they are a lot of different patterns, the customer could want 10 wet, but take two different packages of 5, to get two different patterns. Unfortunately, it would cost him 2 more euros, and I want to avoid this situation.

How could I detect this behaviour, and more precisely, where should I hook that?

I thought of putting it in the cart preview, maybe automatically add a reduction coupon if I detect these packages, but I’m not sure if it’s the most efficient way of doing it.

Any suggestions would help me.

  • 写回答

1条回答 默认 最新

  • doushui3061 2018-09-01 03:55
    关注

    It seems that your product named "Reusable wet" is a variable product with multiple variations based on some product attributes.

    So in your case, the discount can be applied in 2 different ways.

    But first you will need to define the related product attribute SLUG that is involved in "Reusable wet" quantity pack and the related term NAME value for the "pack of 5" in the code.

    If this seetings are not done in the right way, the code will not work.

    1) Changing related item prices (the best way):

    Here we set a discounted unit price of 9 (18 / 2 = 9) when there is 2 related items or more in cart.

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

    Code goes in function.php file of your active child theme (or active theme). Tested and works


    2) The coupon way (not very convenient for many logical reasons):

    Here you will have to set the coupon code. This coupon settings should be like:

    enter image description here

    Where you will set all related product variations in the "products" field.

    Once done you will set the coupon name (in lowercase) in the following code:

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

    Code goes in function.php file of your active child theme (or active theme). Tested and works


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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?