1

I searched, and searched in many, many articles and and I have not found a solution. What I want to do is to create a rule to delete a product by sku? Or a rule to delete products.

I show you my problem:

In my installation of commerce, I have two products. The first (the mother) is a "course". The second is a "enrollment". When you buy a course, the enrollment is added by a rule.

Well...but what if the user wants to delete the enrollment? What I want is that, if a user removes the enrollment, the "course" is removed too and vice-versa.

1 Answer 1

1

You are making the assumption a commerce order in cart status contains products. In fact a commerce order consists of line-items that in turn link to products. These line items also contain also the unit price and the amount of the product (and some other details). It can be done but it requires some juggling with Rules as there is not a remove from cart rules.

So your Rules should fire on the event A product is removed from cart. Your condition should be to check for the product SKU, and in the actions you will have to run through all line items and delete the right one.

So you need to add a Loop through each line item on the commerce order to find the right product.

This is the easiest if you create a Rules component where you send the current line-item and the SKU to remove. Then in that Rules component add the following conditions:

  • Entity had field. (Make sure the line item has a product field)
  • Data comparison. (Make sure the product SKU matches the input SKU)

Action:

On the main Rule, use the following actions:

  • Set a data value (to set your SKU to remove, this will be sent to the component).
  • Add a loop (Provide it with the commerce-order:line-items to loop over those)
  • In the loop, add your component as an action and provide it with the loop's current item and the SKU to remove.

Final note: As it appears you will always be selling these products in pairs, you might find the Commerce Product Bundle interesting. It allows you to combine two products into one. I suppose it will remove both products also when removed from the cart.

4
  • 1
    Ok, many thanks! I will try what you mention me and if I do leave here the explanation. Barry CollinsOk, many thanks! May 14, 2016 at 18:24
  • Unfortunately there has been no luck. I followed your instructions and those videotutorial no luck. Can you help me? Do you have a rule that can be imported to study I try to adapt to my needs? (Please!) May 15, 2016 at 18:30
  • What are you running into specifically? I do not have a need for such Rule and I currently do not have a development environment to create it. So I cannot easily export something for you. If you tell me where you get stuck and what is not working I might be able to provide you with more details. May 15, 2016 at 19:01
  • You followed this tutorial? It is basically what you need, with the Rule, the loop and the component. However your Views trigger is An item is removed from the cart. Your condition has to check for the removed produck SKU using a Data comparison (and you probably need a entity has a field condition before that, to get access to the product SKU field). Then do the loop in the actions. In the end (I forgot it in my initial answer), use Save an entity to save the commerce order. Make sure this is not inside the loop, but below it. May 15, 2016 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.