When using custom order bundles (even if not recommended) which don't contain the commerce_discounts field, the module throws a critical exception. We need to check the existence of the commerce_discounts field before attempting to read from it via the wrapper.

EntityMetadataWrapperException: Unknown data property commerce_discounts. in EntityStructureWrapper->getPropertyInfo() (line 335 of
...../commerce_kickstart/modules/contrib/entity/includes/entity.wrapper.inc).

This was previously fixed in #1835528: commerce_discount_usage_commerce_order_insert function crash others module using bundle of commerce_order entity type., but a rewrite has re-exposed it.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

andyg5000’s picture

gonzalves’s picture

I did apply this patch and still I have the issue. I am using Commerce Marketplace as well. This error happens when I try to add/edit any product. Not able to do anything with discounts module activated... (I have not even activated usage module) Please help...

andyg5000’s picture

Hey @gonozalves,

The error is a generic exception whenever you try to use a wrapper to read from an undefined property. In this case, the issue was with commerce discount. It's trying to access $order->commerce_discounts->value() which may not exist on all order bundles. I'm not familiar with marketplace, but your error could be completely unrelated. You can try to backtrace the error on line 335 of the entity.wrapper.inc class to see what property the code is trying to access.

andyg5000’s picture

Status: Needs review » Needs work

After looking through commerce marketplace and commerce discounts, I think there are still places to be fixed in commerce discounts as you've suggested. We need to make sure that anywhere *wrapper*->commerce_discounts is read/written to, that we check the existence of the field first. I'm setting this to needs work so that we can go through the module and update every instance with one patch.

andyg5000’s picture

Here's an updated patch that checks the wrapper properties existence before attempting to read/write to it in all instances.

gonzalves’s picture

Hi Andy, thank you very much for your quick response... unfortunately, this patch failed.. did not apply at all... thanks again.. rodrigues.

andyg5000’s picture

Make sure you've reverted the change from the previous patch. The easiest way would be to grab a fresh copy of the dev release and apply it to that. I just tested locally and it worked fine.

gonzalves’s picture

Hi Andy, I did a manual patch and it seems to be working absolutely fine!! Thanks a ton Andy...

GuGuss’s picture

Hi,

I've applied your patch properly but stil get the same error when accessing a product page.

Not sure what to do from here ;-)

andyg5000’s picture

Hey @guguss,

What modules are you using that define a new order bundle (marketplace)? Also, are you able to backtrace the calls on line 335 of entity.wrapper.inc? If you don’t have debug setup, you can put dpm(debug_backtrace()) on line 334 (with devel module enabled). From there, you should be able to see which line from commerce_discoutns is attempting to access the commerce discounts property. There’s a possibility that it’s another module that integrates with commerce_discount, or I may have missed a call in my patch. I’m on #drupal-commerce IRC if you want to chat on this.

andyg5000’s picture

@guguss,

I missed you on IRC it looks like. However, your screenshot shows me that the issue is in commerce coupon. "Unknown data property commerce_coupons" and not commerce_discounts (as related to this issue). I'm creating an issue there and will supply a patch as well.

andyg5000’s picture

andyg5000’s picture

@guguss, checkout #2301043: Empty check on $order->commerce_coupons to prevent fatal error for your issue with the coupon module. Feedback on that would be great as well!

gonzalves’s picture

Andy, You are the best!! I applied the next 2 patches and now the issue with my coupon module is also gone. I was not able to enable the coupon module.

  • bojanz committed fa79c63 on 7.x-1.x authored by andyg5000
    Issue #2285199 by andyg5000: Fixed Empty check on $order->...
bojanz’s picture

Title: Empty check on $order->commerce_discounts to prevent fatal error » Add support for multiple order types
Category: Bug report » Feature request
Priority: Major » Normal
Status: Needs review » Active

I've committed Andy's fix from #5. The bug is now resolved.

However, we really should support multiple order types, either by creating the field on all found order types, or by having a setting where you can choose which order types should be supported.
Once that is done, this commit ("fa79c63") can be reverted.

andyg5000’s picture

I would vote for a setting where you can select the order bundles that have discounts. In which case the patch can be reverted and we can replace field checks with settings checks if that makes more sense.

gonzalves’s picture

I also vote. Andy, if I use this commit, will it affect my implementation with Marketplace (since you you know my situation well) ?

andyg5000’s picture

@gonzalves. You already have the commit applied to your instance. The good news is that if you grab a -dev version of this module it's now included so you don't have to re-apply the patch when you update the module. For now you can just keep your module the way it is.

gonzalves’s picture

Ok.. Thanks Andy..

yurg’s picture

Hi,

Have found this thread (and related closed one at #2301043: Empty check on $order->commerce_coupons to prevent fatal error ) since we're using Marketplace and have same issue with coupon / discount line item missed at order bundle created by Marketplace. We're using 2x-dev version of Coupon and dev version of Commerce Discount where all mentioned in both threads patches should be applied ( http://awesomescreenshot.com/03344r8i9b ); still, coupon is being applied to *native* Commerce order, ignoring Marketplace order, which is an it's exact copy bundle of Commerce order. Guess we're in same situation @gonzalves had but not sure what else should be done since we've carefully tried all steps advised. Would really appreciate any help and/or piece of advice.

Thank you in advance!

matthiasm11’s picture

The commerce_coupon module adds its commerce_coupons field to each type of orders. (without a settings form)
Programmed the same approach for the commerce_discounts field in the attached patch.

joelpittet’s picture

That looks like a reasonable patch to get in. Could someone give this a thumbs up(RTBC) and I'll commit.

matthiasm11’s picture

Status: Needs review » Closed (duplicate)

Closing this issue since the patch (with exactly the same code and purpose) has already been copied and committed in https://www.drupal.org/project/commerce_discount/issues/1872922#comment-...