1

In order to apply a price calculation rule for certain products only, I have created a new product type. But I am having problems changing the product type for the selected products. Right now it seems to me that the only way to apply the new product type to these products is to delete them and then re-import them (using Feeds) with the new product type selected. This works as expected.

But there are hundreds of products and it seems to me that there should be an easier way. I have read this post How Can I change the machine name of product type but I am a bit wary that I might break something by changing things in the database.

Does anybody have a cleaner solution for this?

  • Are you able to create a "view" which contains a list of those hundreds of products, with 1 column for each of the relevant field-values that you'd need for creating equivalent products using that new product type? PS: only a "bit" wary? I'd rather say a "lot", or "terribly" ... So for sure I'd not use change things in the DB. – Pierre.Vriens Feb 14 '17 at 10:19
  • Yes, I could clone the existing Products view and add a filter to only show the relevant products. But... how do I apply my price calculation rule to the products in that view? I tried to create a condition, but with no luck... – TBJ Feb 14 '17 at 10:45
  • I'm thinking of a solution that does the equivalent of "change machine name". That solution requires the views result as input to be processed. So may I assume you can adapt that price calculation rule somehow to also (or only) support that new product type? – Pierre.Vriens Feb 14 '17 at 10:50
1

For sure I'd not use the approach of changing things in the Database directly. Instead I would try to use the Rules module to create a clone (using the new product type) of the selected products. A solution that could possibly work is to (also) get the Views Rules module involved. Some details about this module (from its project page):

Provides Views directly as Rules actions and loops to seamlessly use view result data.

If you're not familiar with Rules (yet), the previous quote may seem a bit cryptic (it may make you think like "so what, how can this help me?"). Therefor some more details about how to move forward using these modules:

  1. Create a view (using Views) so that you have 1 Views result (row) for each product of old product type and for which you want to create a similar one with the new product type. This view should have fields (columns) for each of the fields from your old products that you want to use to create your new product (with your new product type). Important: use a Views display type of "Rules".
  2. Create a custom rule in which you use the Views Rules module to iterate over each of these Views results in a Rules action, using the Rules technique known as a "Rules Loop". If you're not familiar with Rules Loops, then have a look at the tutorial titled "Lists and loops".
  3. For each iteration step in your Rules Loop, perform a Rules Action to create an entity which corresponds to your product but with the new product type. At that point you'll have all data from each column of your Views results available as so called Rules Parameters (remember: they are the values used in your products with the old product type). So at that point it's a piece of cake to populate the various fields of your product with the new product type.

  4. The only remaining thing is to decide about the Rules Event to use for this rule to be triggered. But that's up to your own imagination (any Rules Event will do). Optionally, you may also want to add whatever extra Rules Condition(s), also up to your own imagination.

Possibly, you also want to delete the product (of the old product type), e.g using a variation of this rule (to delete those products instead of creating new entities). I would definitely do that in a separate rule (not as an extra Rules Action in the same rule), to be able to verify that the new products are created (correctly) before deleting the products with the old product types (better safe then sorry ...).

Note: Refer to my answer to "How to use Rules to send an E-mail after saving a new comment, to users who checked a boolean in their profile?" (the option 2-part of it) for an example (with screenprints) about how to use the Views Rules module.

  • Wow, that was a very detailed answer. I am familiar with Rules but I haven't used the Views Rules modules and I haven't used loops. I will try your suggestion out. Thanks! – TBJ Feb 14 '17 at 12:26
  • @TBJ : merci for your comment. Let us know how it turned out after trying it. It seems that not a lot of Rules-fans know about the "Views Rules" module, but that's really one of the must-have add-ons if you like using Rules. BTW, using the above approach you could make it somehow similar to using the "node_convert" module also (to convert just any content type instead of just a content type). I might post a self answered question on that some day ... – Pierre.Vriens Feb 14 '17 at 12:33
  • Hmmm, I got stuck at the configuration of row variables in the new rules view. I chose Commerce Product as data type for all the fields, but that results in plain text fields in the action part of the rule. As if I wanted all the products with the new product type to have fixed names and prices :-)... – TBJ Feb 15 '17 at 10:49
  • I have tried several different options.. "text", "list of commerce products" , "rules configuration" etc. But in the "action" I always get a plain text field for fixed values to fill in. Except for "Product type" where I can actually choose the new product type in a list. – TBJ Feb 15 '17 at 10:59
  • @TBJ : check my updated answer (the "note" I added). If with that you still can't get it to work, how about you post a new question about how to use "Views Rules" in your specific case (with relevant screenprint(s) where appropriate)? After you would get that question answered, you may be able to continue with the approach I suggested here. Just using "comments" around here would be very tough to get it sorted out, no? – Pierre.Vriens Feb 15 '17 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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