Hi,

I encountered an issue while adding a product to the cart when "commerce_coupon_usage" is enabled and "redeem coupon" is enabled to anonymous users (I think it's a possible reason).

The complete error is :

EntityMetadataWrapperException : Missing data values in EntityMetadataWrapper->value() (line 83 in /data/project/repository/modules/contrib/entity/includes/entity.wrapper.inc).

After debugging, I found a bug in "commerce_coupon_usage.module" at line 396, when "$unchanged" is loaded from "$order->original", property which is missing.
I don't know why at the first call this property is missing, and just after it is present.

My suggest is to replace the following code :

<?php
$unchanged = entity_metadata_wrapper('commerce_order', $order->original);
?>

by :

<?php
$unchanged = entity_metadata_wrapper('commerce_order', isset($order->original) ? $order->original : $order);
?>

Do you think it's safe to apply this fix ?

Unfortunately I'm not sure to be able to explain how to reproduce this issue on a clean setup.
However I can test fixes on mine.

Thanks

Comments

Sebastien @Actualys created an issue. See original summary.

sebastien m.’s picture

Status: Active » Needs review
StatusFileSize
new787 bytes

Here is a patch.

lunazoid’s picture

Status: Needs review » Reviewed & tested by the community

I was having the same problem after upgrading commerce_coupon (from 7.x-1.x) which was preventing some orders from going through. (I'm not sure why it was only some orders, but it may have been a result of the workflow unique to these orders.) Applying the patch in #2 seems to have fixed the problem!

mglaman’s picture

+++ b/modules/usage/commerce_coupon_usage.module
@@ -393,7 +393,7 @@ function commerce_coupon_usage_commerce_order_update($order) {
-  $unchanged = entity_metadata_wrapper('commerce_order', $order->original);
+  $unchanged = entity_metadata_wrapper('commerce_order', isset($order->original) ? $order->original : $order);

The entity controller should ALWAYS be setting ->original.

Are you using Commerce Entitycache?

mglaman’s picture

Status: Reviewed & tested by the community » Needs review
lunazoid’s picture

I do not have commerce_entitycache installed...

mglaman’s picture

Ok, because this is part of entity controller

      // Load the stored entity, if any. If this save was invoked during a
      // previous save's insert or update hook, this means the $entity->original
      // value already set on the entity will be replaced with the entity as
      // saved. This will allow any original entity comparisons in the current
      // save process to react to the most recently saved version of the entity.
      if (!empty($entity->{$this->idKey})) {
        // In order to properly work in case of name changes, load the original
        // entity using the id key if it is available.
        $entity->original = entity_load_unchanged($this->entityType, $entity->{$this->idKey});
      }

So it looks like an unsaved order is being passed to the function, which doesn't make sense since it looks to be an update hook.

/**
 * Implements hook_commerce_order_update().
 */
function commerce_coupon_usage_commerce_order_update($order) {

This makes me feel like something else is the issue.

karlshea’s picture

I'm also running into this. I narrowed it down to a rule that changes the order status on "Before saving a commerce order". So it's definitely possible for an unsaved order to make it to this function.