根据Woocommerce中的产品数量替换特定的购物车项目 [英] Replace specific cart item based on product quantity in Woocommerce

查看:60
本文介绍了根据Woocommerce中的产品数量替换特定的购物车项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据购物车中产品的数量更改购物车内容。我的商店里只有5种产品:




  • Product_1 => 1个面板,

  • Product_2 = > 12个面板,

  • Product_3 => 18个面板,

  • Product_4 => 30个面板,

  • Product_5 => 60个面板,



它们被配置为不同的产品,因此没有捆绑,套件或其他任何东西。一个以上的面板产品显然比分别添加多个单个面板便宜。



然后,我还有一个定制产品,该产品根据客户提供的地板尺寸计算所需的面板数量,然后将必要数量的单个面板添加到购物车中。



我想在查看购物车时动态更改购物车的内容。



因此例如,如果楼层配置器计算出 54个单个面板并将其添加到购物车中,我想将购物车项目更改为:




  • 1个Product_4(30个面板)

  • 1个Product_2(12个面板)

  • 2个Product_1(1个面板)

  • 并打印一条消息,说明更改。



我检查了不同的解决方案,但没有检查过其中的一个提供这种功能:





  • I am trying to change the cart content based on the quantity of a product in the cart. I only have 5 products in the shop:

    • Product_1 => 1 panel,
    • Product_2 => 12 panels,
    • Product_3 => 18 panels,
    • Product_4 => 30 panels,
    • Product_5 => 60 panels,

    They are configured as different products, so no bundles, kits or anything. More than one panel products are obviously cheaper than adding a number of single panels separately.

    Then I have also a custom product that calculates the number of panels needed based on floor measures given by the customer and then adds to cart the necessary quantity of single panels.

    I Would like to change dynamically the content of the cart when it's reviewed.

    So for example, if the floor configurator calculate 54 single panels and added them to the cart, I would like to change the cart item to:

    • 1 Product_4 (30 panels)
    • 1 Product_2 (12 panels)
    • 2 Product_1 (1 panel)
    • and print a message stating the change.

    I've checked different solutions but none of them offer this kind of feature:

    So, based on this answer thread, I think I need to use hook woocommerce_before_calculate_totals and implement the logic, but need some help since I am a wordpress dev noob.

    Note that the add to cart button is called with Ajax in the floor configurator with:

    jQuery("button.ajax_add_to_cart").attr("data-quantity", parseInt(numLozasrealW * numLozasrealH));
    

    Any help is appreciated.

    解决方案

    The code bellow should do the trick, It will replace the custom product (quantity) by your other products. You will have to set your custom product ID, your 5 normal product IDs and your custom text notice.

    The code:

    add_action( 'woocommerce_add_to_cart', 'action_before_cart', 20, 3 );
    function action_before_cart( $cart_item_key, $product_id, $quantity ) {
        // HERE set your specific product ID
        $specific_product_id = 862;
    
        if( $product_id != $specific_product_id ) return;
    
        // HERE set your other product IDs related to panels
        $products_panels = array(
            861 => 50, // Product ID => for 50 panels
            860 => 30, // Product ID => for 30 panels
            859 => 18, // Product ID => for 18 panels
            858 => 12, // Product ID => for 12 panels
            857 => 1,  // Product ID => for  1 panel
        );
    
        // Loop through all "panels" products IDs
        foreach ( $products_panels as $product_id => $panels ) {
            if( $quantity >= $panels ){
                $qty = floor($quantity / $panels);
                $quantity = $quantity % $panels;
                $data = 'Panels: '.$panels.' | Quantity: '.$qty.' | Remain: '.$quantity;
                print_pr($data);
                WC()->cart->add_to_cart( $product_id, $qty );
            }
        }
        // Removing the specific product
        WC()->cart->remove_cart_item( $cart_item_key );
    
        // HERE set your custom notice text
        wc_add_notice('Here your custom notice text', 'notice');
    }
    

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

    As you can see below when custom product is added to cart, it's replaced by the other products to match the quantity of panels and display a custom notice:

    这篇关于根据Woocommerce中的产品数量替换特定的购物车项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆