Change payment method based on rules
I want to change payment mehthod based on rules is that can be possible.
I want to change payment mehthod based on rules is that can be possible.
It's possible. I have a commerce site that picks from three payment methods depending on the shoppers geographical location and the type of product in the basket.
The former is done with Smart IP which exposes a ruleset I can use in admin/commerce/config/payment-methods
.
Switching methods depending on the contents of the basket was a little more complicated so I ended up writing a custom hook_rules_event_info_alter
to pass the info I needed into Rules.
admin/commerce/config/payment-methods should list your Payment methods.
Edit one and add a condition based on when you want to show them .
example based on order balance:
Condition: Order balance comparison
Parameter: Order: [commerce_order], Operator: = 0
This would only show the payment method when an order balance = 0
the Condition based on product or product type are:
'Order Contains a particular product'
'Order contains products of particular product types' (as of Drupal Commerce 1.5)
I have written this code in rule(it was select available payment method) condition may be it helps some one
<?php
$line_items = $commerce_order->commerce_line_items['und'];
foreach($line_items as $lid) {
$line_item = commerce_line_item_load($lid['line_item_id']);
$pid = $line_item->data['context']['product_ids'][0];
$product = commerce_product_load($pid);
$field_part = $product->field_minimum_participants['und'][0]['value'];
if($field_part == 1) {
return TRUE;
}
}
return FALSE;
?>
Comments
I also have same requirement of using multiple payment methods for different product types. Please let me know, What should I do if the basket/cart contains different type of products.