Problem / motivation
If we follow recommended pane order (shipping before billing) as it has been written by Bojan here and here, we need to provide a shipping condition for orders. It will allow admins to set restrictions for payment method based on the selected shipping method. It is useful when some shipping methods require payment in cash and some others expect payment by bank transfer etc.
Background
Drupal commerce introduced payment condition as a result of the feature request Add an OrderPaymentGateway condition in version 8.x-2.6. It allows admins to set restriction for shipping method based on the selected payment method. In that case the problem is, that the billing method must be selected (and saved) before selecting shipping method, which is not in recommended order.
Solution
Provide a shipping condition for Order entity.
Set shipping pane before billing pane in the order checkout flow, set restriction on the page /admin/commerce/config/payment-gateways/manage/{method_id}, check the "Limit by shipping method" checkbox in the Order tab and select a required shipping methods.

In the following patch I used the code from OrderPaymentGateway condition and adapted it for the shipping purposes.
| Comment | File | Size | Author |
|---|---|---|---|
| #24 | 2976252-24.patch | 10.93 KB | jsacksick |
| #10 | provide_shipping_condition-2976252-10.patch | 4.27 KB | mhawwari |
| #8 | provide_shipping_condition-2976252-8.patch | 4.25 KB | dwkitchen |
| #4 | limit_by_shipping.png | 13.21 KB | martin_klima |
| #2 | provide_shipping_condition-2976252-2.patch | 4.25 KB | martin_klima |
Comments
Comment #2
martin_klimaComment #3
mirom commentedWorks great
Comment #4
martin_klimaComment #5
parisekWorking great
Comment #6
mglamanThere are no tests for this.
Comment #7
mellowtothemax commentedThis works great thanks. In my case I needed a payment method of cash on delivery but only for one shipping method so I had to create a new custom checkout flow for payment method and moved the billing there.
Comment #8
dwkitchen commentedUpdating to align with #3170419: Move all Shipping Conditions to the Shipping Category
Comment #9
mellowtothemax commentedThe patches are not applying on latest commerce shipping rc2
Comment #10
mhawwari commentedRerolled patch to work with the latest version.
Comment #11
mellowtothemax commented#10 worked for me on rc2. Thank you.
Comment #12
oneICT commentedHello,
Thank you, I was looking for this but I'm not sure how to define this.
In my case I have the option to "come and get the order" like a take-away or deliver the order.
In case of take-away we accept cash or online payment
In case of delivery we accept online payment only
What I have done so far:
After defining this I get the error:
There are no payment gateways available for this order. Please try again later.
When I remove the conditions on the payment gateways both payment options appear.
But online payment remains available although I selected to deliver the order...
Thing is that when switching between the delivery options nothing really happens.
I would expect a refresh or loading of options.
Like when choosing the payment options cash or online.
Any idea what I'm missing?
Kind regards,
Johnny
Comment #13
mellowtothemax commentedUnfortunately latest patch no longer works for current version.
Comment #14
primsi commentedRe-roll for the patch.
Comment #15
primsi commentedAdded tests and schema fixes.
Comment #17
primsi commentedRemoving leftover debug screenshot.
Comment #18
berdirMissing class docblock.
Tests and so on look good to me, so removing that tag.
Comment #19
ekes commentedRerolled to apply to current dev.
Also added the class docblock.
Comment #20
jsacksick commentedRetitling for clarity.
order_shippingis too generic as Commerce core provides an "order_shipping_address" condition.We need to rename it to
order_shipping_method.Additionally, we should change the category to "Shipment" (instead of Shipping, to be consistent with what Commerce shipping is currently doing.
Also, to be consistent with what Commerce is doing in general, the condition needs to store the shipping method UUIDS (similar to the Store & customer conditions for example).
Will upload a patch that addresses these soon.
Comment #21
jsacksick commentedComment #22
jsacksick commentedBetter with the condition itself :).
Comment #24
jsacksick commentedComment #25
jsacksick commentedWe're probably also missing a unit test for the new condition, similar to what we have for other conditions provided by commerce_shipping.
Comment #27
jsacksick commentedWent ahead and committed the patch from #24.