Discussions

check for "product-types" in rules in addition to "particular product" check

I try to build up a rule condition for different payment methods - in my case bank transfer and paypal. I have 2 different product-types - "normal products" and "file products". File Downloads should be paid via paypal - that means - if you have more than one "file product" in your cart only paypal as a payment is available.

Is it possible to add a rule condition to check for a specific product-type ? I could solve my problem by adding all "file products" to a "Order contains particular product" rule-condition, but that too much work if you have a lot of downloadable products..

Posted: Dec 15, 2011

Comments

rfay Randy Fay on December 15, 2011

Data comparison... product->type

You will have to loop through the line items in the order and check each. And you'll have to use entity-has-field or a compare on the line item type to bring the product into scope.

SteffenR on December 21, 2011

Hi - thx for your answer. I just tried but couldn't figure out how to achieve a working result.

I created a loop through site:current-cart-order:commerce-line-items and a rules component that checks with Entity has field for commerce_file_line_item_files. In my rules debug log this evaluates to true if i have "file product".
In the rules action i tried to enable the Payment Method Bank Transfer with the parameter [site:current-cart-order].
But these settings only product a WSOD with an php error
PHP Fatal error:  Call to a member function getPluginName() on a non-object in commerce_payment.rules.inc on line 203

SteffenR

rfay Randy Fay on December 21, 2011

Sounds like a bug to me. Can you post a feature module that demonstrates this, or at least attach a textfile containing your rules? (Please don't paste long rules into the comments themselves; attach textfiles instead)

rfay Randy Fay on December 21, 2011

Yes, you found a bug. I've posted a patch in http://drupal.org/node/1379258.

Attached is my solution (which requires that patch). In my site I have "Example payment" and "No payment" methods,and corresponding product types. I deleted the payment rules for both payment types and then created a new one (the main rule attached). Then it loops through the line items in the order and if it finds an example-type product, it enables the example method. If it finds a no-payment type product, it enables the no-payment product.

You will have to write another rule, of course, to make sure they don't put two items of different types into the cart.