EntityMetadataWrapperException: Unable to get the data property

currency_code

as the parent data structure is not set. in EntityStructureWrapper->getPropertyValue() (line 442 of /~/~/sites/all/modules/entity/includes/entity.wrapper.inc).

This happens when i am trying to add any product to cart ?!! can not fiend a solution

Comments

rszrama’s picture

Priority: Critical » Normal
Status: Active » Closed (won't fix)

Not enough info here; bug reports need to be reproducible, such as "I installed this module" or "I setup this rule." If this is a vanilla install, try using Commerce Repair to see if a field got missed on installation (though that sort of thing has been happening less and less).

tonyoflow’s picture

i also had a similar problem but the 'currency code' in my case is just 'data' svolved it halfway i think.
since i got the same error when adding to cart and trying to view orders,
i went into the database and whiped clean/did a TRUNCATE on the order spesific tables:

commerce_order
commerce_order_revision
field_data-commerce_order_total
field_revision_commerce_order_total

(!note: deletes current order data.)

and the cart and orders works fine again!

but suddenly i got problems with search_api_index at the 'view all products page'.
And now after i removed the indexes with UI to rebuild again,
i get the same damn error!? when trying to rebuild (..or run cron)

the error:

An AJAX HTTP error occurred. HTTP Result Code: 500 Debugging information follows. Path: /batch?id=154&op=do StatusText: Service unavailable (with message) ResponseText: EntityMetadataWrapperException: Unable to get the data property as the parent data structure is not set. in EntityStructureWrapper->getPropertyValue() (line 442 of /home4/brevisfo/public_html/profiles/commerce_kickstart/modules/contrib/entity/includes/entity.wrapper.inc).

deggertsen’s picture

Just thought I should chime in. I've run into this problem on multiple websites. Here's the full error.
EntityMetadataWrapperException: Unable to get the data property currency_code as the parent data structure is not set. in EntityStructureWrapper->getPropertyValue() (line 438 of /sites/all/modules/entity/includes/entity.wrapper.inc).

I've managed to work around this by removing the lines that throw this error as shown below:

diff --git a/includes/entity.wrapper.inc b/includes/entity.wrapper.inc
index 67764e7..8e02ea4 100644
--- a/includes/entity.wrapper.inc
+++ b/includes/entity.wrapper.inc
@@ -434,9 +434,6 @@ class EntityStructureWrapper extends EntityMetadataWrapper implements IteratorAg
   protected function getPropertyValue($name, &$info) {
     $options = array('language' => $this->getPropertyLanguage(), 'absolute' => TRUE);
     $data = $this->value();
-    if (!isset($data)) {
-      throw new EntityMetadataWrapperException('Unable to get the data property ' . check_plain($name) . ' as the parent data structure is not set.');
-    }
     return $info['getter callback']($data, $options, $name, $this->type, $info);
   }
 
@@ -447,9 +444,6 @@ class EntityStructureWrapper extends EntityMetadataWrapper implements IteratorAg
     if (!empty($info['raw getter callback'])) {
       $options = array('language' => $this->getPropertyLanguage(), 'absolute' => TRUE);
       $data = $this->value();
-      if (!isset($data)) {
-        throw new EntityMetadataWrapperException('Unable to get the data property ' . check_plain($name) . ' as the parent data structure is not set.');
-      }
       return $info['raw getter callback']($data, $options, $name, $this->type, $info);
     }
     return $this->getPropertyValue($name, $info);

It would be nice to have a permantent solution, but I'm not sure how to get to the bottom of where it's coming from.

Added a couple of issues I believe are related.

Tsegaye’s picture

In my case, #3 will make the order bypass the coupons applied because it originally did not get the coupon data, that's why the entity module returned it did not get the parent data.

Joe Huggans’s picture

I can confirm that this is related to the coupon module in my case, it happens if you add a coupon to an order, then leave the review page and then return to the review page.

donquixote’s picture

I also experience this error.
"past" module gives me a stack trace:

[%type] (string): EntityMetadataWrapperException
[!message] (string): Unable to get the data property currency_code as the parent data structure is not set.
[%function] (string): EntityStructureWrapper->getPropertyValue()
[%file] (string): sites/all/modules/contrib/entity/includes/entity.wrapper.inc
[%line] (string): 457
[%trace] (string):
 #0 sites/all/modules/contrib/entity/includes/entity.wrapper.inc(86): EntityStructureWrapper->getPropertyValue('currency_code', Array)
 #1 sites/all/modules/contrib/entity/includes/entity.wrapper.inc(273): EntityMetadataWrapper->value()
 #2 sites/all/modules/contrib/commerce/modules/line_item/commerce_line_item.module(1522): EntityValueWrapper->value()
 #3 sites/all/modules/contrib/commerce/modules/line_item/includes/views/handlers/commerce_line_item_handler_area_line_item_summary.inc(97): commerce_line_items_total(Array)
 #4 sites/all/modules/contrib/views/plugins/views_plugin_display.inc(2698): commerce_line_item_handler_area_line_item_summary->render(false)
 #5 sites/all/modules/contrib/views/theme/theme.inc(73): views_plugin_display->render_area(Object(commerce_line_item_handler_area_line_item_summary), false)
 #6 includes/theme.inc(1136): template_preprocess_views_view(Array, 'views_view')
 #7 sites/all/modules/contrib/views/plugins/views_plugin_display.inc(2692): theme('views_view', Array)
 #8 sites/all/modules/contrib/views/includes/view.inc(1264): views_plugin_display->render()
 #9 sites/all/modules/contrib/commerce/commerce.module(373): view->render()
#10 sites/all/modules/contrib/commerce/modules/cart/includes/commerce_cart.pages.inc(45): commerce_embed_view('commerce_cart_f...', 'default', Array, 'cart')
#11 includes/menu.inc(527): commerce_cart_view()
#12 index.php(21): menu_execute_active_handler()
#13 {main}
[severity_level] (string): 3

The culprit is this code in commerce_line_item.module:

function commerce_line_items_total($line_items, $types = array()) {
  [..]

  // Populate an array of how many line items on the order use each currency.
  foreach ($line_items as $delta => $line_item_wrapper) {
    // Convert the line item to a wrapper if necessary.
    if (!$line_item_wrapper instanceof EntityMetadataWrapper) {
      $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item_wrapper);
    }

    $line_item_currency_code = $line_item_wrapper->commerce_total->currency_code->value();

In my case, I have a custom line item type where the commerce_total is empty, that is, it has no field items.

Solution

The question is: Do we consider such a line item "broken", or is this is a rare but valid case that needs to be supported?

Consider it "broken", and throw a custom exception.

Do not just let Entity API throw its exception.
Instead, either
- catch the exception from Entity API, and throw a custom exception, saying "Line item 123 of type xyz does not have any items in commerce_total field. Cannot compute order total from 5 line items.", or
- check whether commerce_total exists (e.g. !empty($line_item_wrapper->commerce_total->value()), and then throw a custom exception.

Consider it "broken", but treat it as price === zero

As above, either catch the EntityMetadataWrapperException, or do a check before.

Log a watchdog message, saying "line item 123 of type xyz does not have any items in commerce_total field. It was assumed that this item is for free.".

Then skip this line item for calculating the sum.

Consider it a valid case, treat as zero

As above: Either catch the EntityMetadataWrapperException, or do a check before.

Then simply count the item as zero / skip it, and do not log an error or throw an exception.

Notes

I want to say the entire function commerce_line_items_total() looks way more complicated than it should be.
Also, it looks like it will fail if the list is empty? Just a guess.

donquixote’s picture

I found some code duplication:
- commerce_order_calculate_total()
- commerce_line_items_total()

These two share a lot of code, and are susceptible to the same error, if commerce_total has no field items for one line item.

The differences:
- commerce_order_calculate_total() also removes orphan line item references from the order.
- commerce_order_calculate_total() writes the result into $order_wrapper->commerce_order_total, instead of returning it.

It could be refactored such that commerce_order_calculate_total() would call commerce_line_items_total() for the calculation.

donquixote’s picture

I found some code duplication:
- commerce_order_calculate_total()
- commerce_line_items_total()

Another difference:
commerce_order_calculate_total() also looks into the components for each $line_item_wrapper->commerce_total.
commerce_line_items_total() does not do this.
This is strange.
Shouldn't both give the same result?

donquixote’s picture

Status: Closed (won't fix) » Active

I re-open this, because there is now more information.
If the questions in my previous posts can be answered, I can produce a patch, which fixes this issue and also cleans up the two functions.