Just like Promotions in Commerce 2.x, the shipping method config entity should embed configuration for condition plugins.
These conditions accept a shipment entity.

We should implement the following conditions to start with:
- Order total (can we reuse the one from Commerce, pass it $shipment->getOrder()? mglaman might know)
- Destination country (we can add state/province support in a followup)

Other conditions we'll want to have:
- Customer has role ($shipment->getOrder()-.getCustomer())
- Shipment weight (once we add the weight field to the shipment)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bojanz created an issue. See original summary.

iajay’s picture

Atleast when there is NO shippable product then the Shipping pane should not be visible in checkout
Or is this possible and I am missing something obvious?

klagler’s picture

- Total number of ordered items would also be great to have as condition.

bojanz’s picture

Assigned: Unassigned » bojanz

This issue can now proceed, Commerce 8.x-2.x has a new Conditions API, and it's being used on promotions and payment gateways.

bojanz’s picture

Status: Active » Needs work
FileSize
6.48 KB

Initial untested work. I'll come back to this after I tag beta3 of shipping.

This gives us the order total and customer role conditions. The patch would need to provide a shipment weight condition (written similar to the OrderTotalPrice condition). We can leave a "Shipping address" condition to a followup.

SchwebDesign’s picture

Thanks for your work on this. The order weight (shipment weight?) is our current use case for this, so just wanted to check on any status of this since beta3 was tagged. If it'd be any help, we'll be able to test this thoroughly (with weight condition) to report that it works.

In the meantime, i've added this patch and ran the Drupal update. That looks to have worked well and conditions (customer-billing address and role, and order - total price) are now showing up at:
admin/commerce/config/shipping-methods/add

travis-bradbury’s picture

We can leave a "Shipping address" condition to a followup.

My interest was in conditions on the shipping address so I added a condition for it. It's just the OrderBillingAddress from the order module with some find-and-replace.

The #5 worked as expected so I'd call this RTBC except you wanted a shipment weight condition.

Nathaniel’s picture

#7 is working for different delivery rates for different zip codes using the shipping address.

travis-bradbury’s picture

Status: Needs work » Needs review
FileSize
12.42 KB
5.93 KB

I added a shipment weight condition that's similar to the order total price one.

Londova’s picture

Is it possible to add a condition based on quantity?

SchwebDesign’s picture

@Londova, there is a quantity condition.

Thanks so much for the shipping weight condition.

I'm getting this error after initial save of a shipping method with shipping weight condition:

Notice: Undefined index: weight in Drupal\commerce_shipping\Plugin\Commerce\Condition\ShipmentWeight->buildConfigurationForm() (line 40 of modules/contrib/commerce_shipping/src/Plugin/Commerce/Condition/ShipmentWeight.php).

Also, i'm wondering if it's feasible to allow multiple conditions of each type or a range condition.

E.g. I want to enter shipping methods for 30 different non-overlapping weight ranges and the customer's order is the lowest weight, it will show ALL shipping methods to the customer if i use the "Less than or equal to" condition. If I could ALSO add a "greater than" weight condition, the system would just show them the ONE that applies to their order, making things much more user friendly in cart.

SchwebDesign’s picture

@Londova, MY APOLOGIES, there is NOT a quantity condition yet. I could've sworn I saw it, but i was wrong.

travis-bradbury’s picture

Status: Needs review » Needs work

Needs work per #11

I'm getting this error after initial save of a shipping method with shipping weight condition:

Notice: Undefined index: weight in Drupal\commerce_shipping\Plugin\Commerce\Condition\ShipmentWeight->buildConfigurationForm() (line 40 of modules/contrib/commerce_shipping/src/Plugin/Commerce/Condition/ShipmentWeight.php).

SchwebDesign’s picture

FileSize
56.64 KB

FWIW on further testing I also noticed the initial shipping cost shown in the cart and checkout order summary doesn't seem to follow the conditions.

In my case it shows the rate that would apply for respective weight of a quantity of 1 (although this order has 4) and yet the calculated rates are correct on the left (per my example in #11). My shipping rates only depend on shipping weight conditions... no other conditions, so it theoretically should show the correct lowest rate even in cart. See screenshot attached.

This is the output of Global: Order total (Global: Order total) in the commerce_cart_block and commerce_checkout_order_summary views, I believe. ...or is this a configuration change that needs to be made?

Not sure if this is something reconfigured or perhaps this is a side effect of the error in previous post.

travis-bradbury’s picture

Status: Needs work » Needs review
FileSize
12.42 KB
498 bytes

This fixes the undefined index.

I'm not sure what's up with the issue you have in the screenshot. I don't think it is related to conditions. Does it happen with a brand new order?

SchwebDesign’s picture

Thanks. I can confirm that fixed the error in #11. That said, things behave otherwise the same as outlined above.

Yes, that happens with a brand new order. I tested (drush cr first) removing all items from cart and adding new ones to cart to start over as well logged in.

I don't mean to complicate this thread if this is not related, but to answer your question thoroughly I also then tested as anonymous user and when clicking "Recalculate shipping" got this error in the logs:

 Undefined index: #op in Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\Login->validatePaneForm() (line 295 of /home/cpanelusr/public_html/stage/modules/contrib/commerce/modules/checkout/src/Plugin/Commerce/CheckoutPane/Login.php)  

This is on Commerce 2.0.0-rc1 installed

AndyD328’s picture

Thanks for the conditions patch.

Will it have a 'NOT' filter at some point? eg 'is NOT in 'selected countries' to make it easy to filter for international orders.

bojanz’s picture

There is no plan for NOT support right now. We couldn't figure out a sane UX, decided to roll without it for the immediate future.

Looked at the #15 patch. It's missing tests and schema for the two added conditions (Commerce has some that can be copied). We also need to add the condition operator field (AND/OR), like promotions have.

bojanz’s picture

Status: Needs review » Needs work

I'll try to work on some of these points tomorrow. Feel free to reroll in the meantime.

SchwebDesign’s picture

Thanks!

bojanz’s picture

FileSize
21.24 KB

Next iteration.
Adds unit tests, schema. Renames ShipmentShippingAddress to ShipmentAddress. Adds condition_operator (AND/OR support).
Needs an update function for setting the condition_operator, and we're done.

bca-wm’s picture

Would it be possible to add a condition for the order contains xxxx product type?

A use case would be having different product types physically stored at different locations, and thus needing different shipment methods/prices to the customer.

Thanks.

  • bojanz committed 7e9755f on 8.x-2.x authored by tbradbury
    Issue #2826053 by tbradbury, bojanz, SchwebDesign: Allow shipping...
bojanz’s picture

Status: Needs work » Fixed

Committed #21 + an update hook. Gave tbradbury commit credit for his work on the individual conditions.

See you in followups for the rest.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

heddn’s picture

This was committed and not removed from the readme.txt. Was a follow-up ever opened to cleanup the readme?

hsponner’s picture

I'd also appreciate the NOT condition as AndyD328 in #17, find it very usefull for international shipping with standard shipping + conditional for few defined countries.