11

Is there a way to have different payment methods available for different product types?

This is my scenario and what I've figured out so far:

I have two product types on my Drupal Commerce site, for selling bus travel passes. One type is just to book the bus pass where you select the date and times (custom line items) for your buss tickets. The other type is for actual payments, where you can choose a buss pass, add additional tours (with custom line items too) and then pay with Paypal.

I am using a customized version of the example payment module, for the booking payment type, and using the Drupal Commerce Paypal module for the actual payments.

What I need is to have the custom booking payment method available for one product type, and the paypal payment method for the other product type.

I have tried to add a condition for the product type and haven't been able to do this. We can add a condition for a particular product, but not for a product type. This would be ideal: https://skitch.com/ipwa/eywxa/add-a-new-condition-chasquibus

What I have done for now is to clone the Paypal rule for each payment product type, and selected that products SKU as a condition. This is not ideal, because if a user has more than one payment product, we get a duplicate Paypal payment method available for each additional payment product on the order. I guess this makes sense since we have a payment method rule for each one.

My solution would be able to have a condition for product type on the payment method rule, or be able to have the four payment products SKUs in a 'Order contains a particular product' condition.

1 Answer 1

10
+50

The product's type is a property of the commerce_product entity, so you can use the built-in "Data comparison" condition to test it.

Just add a new condition of type "Data comparison", and make the data selector look like this:

Screenshot 1

When you continue you'll get a screen like this:

Screenshot 2

As you can see you get a nice drop down list of product types to choose from. I just tested that and it works a treat.

If that isn't working for whatever reason, it's pretty easy to implement your own rules condition in a custom module:

function MYMODULE_rules_condition_info() {
  return array(
    'commerce_product_is_type' => array(
      'label' => t('Product is a certain type'), 
      'parameter' => array(
        'product' => array(
          'label' => t('Product'),
          'type' => 'commerce_product',
        ),
        'type' => array(
          'label' => t('Product Type (Machine Name)'),
          'type' => 'text'
        )
      ), 
      'group' => t('Commerce Product'),
      'callbacks' => array(
        'execute' => 'MYMODULE_commerce_product_is_type',
      )
    ),
  );
}


function MYMODULE_commerce_product_is_type($product, $type) {
  return $product->type == $type;
}
6
  • Wow, thanks, this seems the way to go for now. Although the answer doesn't exactly answer the question (I might need to file an issue for Commerce to provide a product type rule condition), it does solve my problem. I'll leave the bounty open to let more people answer, but it seems so far like you'll get the bounty. Thanks for the screenshots btw, helps a lot.
    – ipwa
    Jun 18, 2012 at 20:13
  • @ipwa Turns out it wasn't too tricky, I've updated the answer
    – Clive
    Jun 18, 2012 at 20:29
  • I wish you hadn't deleted your previous answer and added this one as another answer instead. When I try to use commerce-product:type as a Data selector for the Data comparison condition I get this error: "Data selector commerce-product:type for parameter data is invalid."
    – ipwa
    Jun 19, 2012 at 20:50
  • @ipwa Which one do you mean? The one with code or the very first one? I've put the code back in the answer in case you meant that one
    – Clive
    Jun 19, 2012 at 20:54
  • 1
    @ipwa No probs, glad to help. Just in case you want to see the original answer you can view it here
    – Clive
    Jun 22, 2012 at 0:26

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.