2

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.

Javier L.
  • 23
  • 6
  • Please post the code you have used to try to achieve your goal (in your original question) if you'd like us to be able to help you. – Justin R. May 22 '18 at 15:38

1 Answers1

0

The code below 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:

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks a lot for your time Loic and your helpful code. However it doesn't work. Might be because the custom floor calculator is performing the add_to_cart with ajax? `jQuery("button.ajax_add_to_cart").attr("data-quantity", parseInt(numLozasrealW * numLozasrealH));` When I click on add to cart it hangs and never finishes adding. – Javier L. May 23 '18 at 15:32
  • @MalcolmX You didn't told in your question how the product was added to cart… With Ajax that is much more complicated and too broad for stackOverFlow. You should rethink this project and instead of the actual behavior, your custom floor calculator should add directly the multiple needed corresponding products to cart… But this is a real development and not for StackOverFlow. My answer here is answering your question with the material you provided in it and it works. – LoicTheAztec May 23 '18 at 15:48
  • Excuse my ignorance. I didn't know and couldn't imagine that the fact that the add_to_cart button was called with Ajax would make the whole thing as complicated as you say. Maybe using a hook that gets processed after the add to cart? Redoing everything is not an option at the moment I'm afraid. Sorry again and thanks for your time. – Javier L. May 23 '18 at 17:06
  • Any chance we can use another hook then? – Javier L. May 24 '18 at 08:54
  • @MalcolmX This is not about using another hook… This is about changing your product configurator...This is a development that need you to hire a developer for it. – LoicTheAztec May 24 '18 at 09:32
  • I'll send you a message then to see if you can give me a quote! Thanks – Javier L. May 24 '18 at 10:10