As soon as a condition is set on a shipping rate, it no longer appears in checkout, no matter what the option is. Tried setting condition to be greater or less than a certain weight, store, etc. and it never appears as an option on shipping page.

Using flat rate shipping options

Commerce 2.0
Drupal 8.4

No messages in log pertaining to this.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jasonschweb created an issue. See original summary.

jasonschweb’s picture

Issue summary: View changes
mortona2k’s picture

The order price condition seems to work, but order weight isn't working for me.

smartparty’s picture

Having the same issue and I've noticed that in ShipmentWeight.php under public function evaluate(EntityInterface $entity) { the weight value is empty.

If you manually define the weight for the purpose of debugging (i.e. $weight = new Weight(999, 'g');) then it works correctly.

l_v’s picture

I've same issue: weight condition doesn't work.
Also trying to dump $this->get('weight')->first() al line 244 in Drupal\commerce_shipping\Entity\Shipment return null.

mortona2k’s picture

Title: Shipping Conditions not working » Shipping weight conditions not working
Status: Active » Needs review
FileSize
462 bytes

Here's a quick fix that recalculates the weight if empty.

There will be issues if !$this->get('weight')->isEmpty() fails to return true after calling recalculateWeight().

I changed the title to Shipping weight conditions.. hope this is ok.

smartparty’s picture

Didn't see the wood for the trees with the recalculateWeight function. Thank you, works for me as an easy fix for now.

l_v’s picture

It seems to works for me too, thank you

Mirroar’s picture

Version: 8.x-2.0-beta4 » 8.x-2.x-dev
Issue tags: +Needs tests
FileSize
422 bytes

I've just spent a large amount debugging this problem, and got to the conclusion that the problem is in Shipment::populateFromProposedShipment(). The PackerManager uses that function to populate its shipment entities, and since that function just directly sets the shipment items instead of using Shipment::setItems(). This means that the shipment's weight is never calculated, even though the items are added.

I feel it's cleaner to add the call to Shipment::recalculateWeight() at that point rather than in getWeight, and it fixes the issue just as well, so I attached a new patch that does that. I guess we still need a test for this, nonetheless.

pushhockey’s picture

Hi Guys,

I have the same issue with weights, thought it was something i had done wrong. Now, my problem is how to apply the patch using composer or manually as i haven't done this before. I have done a google search and read a load of stuff, that, quite frankly is above my paygrade! I feel i need to download cweagans but not sure where to put the file? Then hopefully I can follow the instructions here http://www.anexusit.com/blog/how-to-apply-patches-drupal-8-composer - however there is so much misinformation out there at the moment I wondered if someone could walk me through the steps.

Many thanks for the patch @Mirroar

mortona2k’s picture

@pushhockey I'd encourage you to look elsewhere instead of asking in this thread. However, what you want to do is add cweagans/composer-patches to your composer file, and add the patch in extra: patches. When you run composer install, you should get composer patches and the patch installed. See the usage example here: https://github.com/cweagans/composer-patches/blob/master/README.md

bojanz’s picture

Issue tags: -Needs tests

Thank you Mirroar for tracking this down!

Adding a test and and some cleanup.

  • bojanz committed 97d7639 on 8.x-2.x authored by Mirroar
    Issue #2917519 by mortona2k, Mirroar, bojanz: Shipping weight conditions...
bojanz’s picture

Status: Needs review » Fixed

Done.

Status: Fixed » Closed (fixed)

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

Marco van Bemmel’s picture

Wow, mortona2k and Mirroar, thank you so much for these patches. After three years this seems te be an issue again?

I use this module (8.x-2.0-rc2 and/or dev version of 17 march 2021) in combination with Commerce Shipping Weight Tariff, but the shipping-cost based on weight still did not work correctly. After applying patch # 6 and # 9 it all seems to work correct now!

Is there a reason why this piece of code is not added to the module anymore?