Im getting this notice in some views using the Commerce Line Item: Total field.

Notice: Undefined index: currency_code in commerce_price_field_formatter_view() (line 410 of C:\damp\www\...\sites\all\modules\commerce\modules\price\commerce_price.module).

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rszrama’s picture

Can you provide more information on those line items? Do they not have total values? Not sure why the currency_code would suddenly go missing...

EndEd’s picture

FileSize
23.08 KB
121.44 KB

Well, here is the export of the views, also an image of the result views where i get the notice. I suppose, yes. Those line items have total values. Hope this helps

rszrama’s picture

Hmm, unfortunately I cannot import the View because it looks like maybe you're using some custom code or Panels. : ?

Are you still getting this problem? I still don't know how you could end up with a price field array that didn't have a currency_code key unless something in your site is messing up the price field data prior to display.

EndEd’s picture

Yep I still get this problem with that views in particular but I tried to recreate it and it came error free so i suppose something goes wrong with the first one. Anyway, thanks as always for the patience.

rszrama’s picture

Ok, should we go ahead and close this issue out then until we can reproduce it?

EndEd’s picture

yes

rszrama’s picture

Status: Active » Closed (cannot reproduce)

Cool, thanks.

hoZt’s picture

Status: Closed (cannot reproduce) » Active

I am getting this same error when doing a view with aggregation. I am trying to come up with a list of best selling items so am grouping by the product_id and summing the total and quantity columns. If I change the formatter to raw I will not get the warning. The result is displaying correctly even with the warnings.

Here is an export of the view:

$view = new view;
$view->name = 'best_sellers';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'commerce_line_item';
$view->human_name = 'Best Sellers';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Best Sellers';
$handler->display->display_options['group_by'] = TRUE;
$handler->display->display_options['access']['type'] = 'none';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['query']['options']['query_comment'] = FALSE;
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '100';
$handler->display->display_options['style_plugin'] = 'table';
$handler->display->display_options['style_options']['columns'] = array(
'commerce_product' => 'commerce_product',
'field_product_shelf_name' => 'field_product_shelf_name',
'quantity' => 'quantity',
'commerce_total' => 'commerce_total',
);
$handler->display->display_options['style_options']['default'] = 'quantity';
$handler->display->display_options['style_options']['info'] = array(
'commerce_product' => array(
'sortable' => 1,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
'empty_column' => 0,
),
'field_product_shelf_name' => array(
'sortable' => 1,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
'empty_column' => 0,
),
'quantity' => array(
'sortable' => 1,
'default_sort_order' => 'desc',
'align' => '',
'separator' => '',
'empty_column' => 0,
),
'commerce_total' => array(
'sortable' => 1,
'default_sort_order' => 'desc',
'align' => '',
'separator' => '',
'empty_column' => 0,
),
);
$handler->display->display_options['style_options']['override'] = 1;
$handler->display->display_options['style_options']['sticky'] = 0;
$handler->display->display_options['style_options']['order'] = 'desc';
$handler->display->display_options['style_options']['empty_table'] = 0;
/* Relationship: Commerce Line item: Referenced product */
$handler->display->display_options['relationships']['commerce_product_product_id']['id'] = 'commerce_product_product_id';
$handler->display->display_options['relationships']['commerce_product_product_id']['table'] = 'field_data_commerce_product';
$handler->display->display_options['relationships']['commerce_product_product_id']['field'] = 'commerce_product_product_id';
$handler->display->display_options['relationships']['commerce_product_product_id']['required'] = 0;
/* Field: Commerce Line item: Product */
$handler->display->display_options['fields']['commerce_product']['id'] = 'commerce_product';
$handler->display->display_options['fields']['commerce_product']['table'] = 'field_data_commerce_product';
$handler->display->display_options['fields']['commerce_product']['field'] = 'commerce_product';
$handler->display->display_options['fields']['commerce_product']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['commerce_product']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['commerce_product']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['commerce_product']['alter']['external'] = 0;
$handler->display->display_options['fields']['commerce_product']['alter']['replace_spaces'] = 0;
$handler->display->display_options['fields']['commerce_product']['alter']['trim_whitespace'] = 0;
$handler->display->display_options['fields']['commerce_product']['alter']['nl2br'] = 0;
$handler->display->display_options['fields']['commerce_product']['alter']['word_boundary'] = 1;
$handler->display->display_options['fields']['commerce_product']['alter']['ellipsis'] = 1;
$handler->display->display_options['fields']['commerce_product']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['commerce_product']['alter']['trim'] = 0;
$handler->display->display_options['fields']['commerce_product']['alter']['html'] = 0;
$handler->display->display_options['fields']['commerce_product']['element_label_colon'] = 1;
$handler->display->display_options['fields']['commerce_product']['element_default_classes'] = 1;
$handler->display->display_options['fields']['commerce_product']['hide_empty'] = 0;
$handler->display->display_options['fields']['commerce_product']['empty_zero'] = 0;
$handler->display->display_options['fields']['commerce_product']['hide_alter_empty'] = 0;
$handler->display->display_options['fields']['commerce_product']['type'] = 'commerce_product_reference_sku_plain';
$handler->display->display_options['fields']['commerce_product']['settings'] = array(
'show_quantity' => 0,
'default_quantity' => '1',
'combine' => 1,
'line_item_type' => 0,
);
$handler->display->display_options['fields']['commerce_product']['field_api_classes'] = 0;
/* Field: Commerce Product: Shelf Tag */
$handler->display->display_options['fields']['field_product_shelf_name']['id'] = 'field_product_shelf_name';
$handler->display->display_options['fields']['field_product_shelf_name']['table'] = 'field_data_field_product_shelf_name';
$handler->display->display_options['fields']['field_product_shelf_name']['field'] = 'field_product_shelf_name';
$handler->display->display_options['fields']['field_product_shelf_name']['relationship'] = 'commerce_product_product_id';
$handler->display->display_options['fields']['field_product_shelf_name']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['field_product_shelf_name']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['field_product_shelf_name']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['field_product_shelf_name']['alter']['external'] = 0;
$handler->display->display_options['fields']['field_product_shelf_name']['alter']['replace_spaces'] = 0;
$handler->display->display_options['fields']['field_product_shelf_name']['alter']['trim_whitespace'] = 0;
$handler->display->display_options['fields']['field_product_shelf_name']['alter']['nl2br'] = 0;
$handler->display->display_options['fields']['field_product_shelf_name']['alter']['word_boundary'] = 1;
$handler->display->display_options['fields']['field_product_shelf_name']['alter']['ellipsis'] = 1;
$handler->display->display_options['fields']['field_product_shelf_name']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['field_product_shelf_name']['alter']['trim'] = 0;
$handler->display->display_options['fields']['field_product_shelf_name']['alter']['html'] = 0;
$handler->display->display_options['fields']['field_product_shelf_name']['element_label_colon'] = 1;
$handler->display->display_options['fields']['field_product_shelf_name']['element_default_classes'] = 1;
$handler->display->display_options['fields']['field_product_shelf_name']['hide_empty'] = 0;
$handler->display->display_options['fields']['field_product_shelf_name']['empty_zero'] = 0;
$handler->display->display_options['fields']['field_product_shelf_name']['hide_alter_empty'] = 0;
$handler->display->display_options['fields']['field_product_shelf_name']['group_column'] = 'entity id';
$handler->display->display_options['fields']['field_product_shelf_name']['field_api_classes'] = 0;
/* Field: Commerce Product: Brand */
$handler->display->display_options['fields']['field_product_brand']['id'] = 'field_product_brand';
$handler->display->display_options['fields']['field_product_brand']['table'] = 'field_data_field_product_brand';
$handler->display->display_options['fields']['field_product_brand']['field'] = 'field_product_brand';
$handler->display->display_options['fields']['field_product_brand']['relationship'] = 'commerce_product_product_id';
$handler->display->display_options['fields']['field_product_brand']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['field_product_brand']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['field_product_brand']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['field_product_brand']['alter']['external'] = 0;
$handler->display->display_options['fields']['field_product_brand']['alter']['replace_spaces'] = 0;
$handler->display->display_options['fields']['field_product_brand']['alter']['trim_whitespace'] = 0;
$handler->display->display_options['fields']['field_product_brand']['alter']['nl2br'] = 0;
$handler->display->display_options['fields']['field_product_brand']['alter']['word_boundary'] = 1;
$handler->display->display_options['fields']['field_product_brand']['alter']['ellipsis'] = 1;
$handler->display->display_options['fields']['field_product_brand']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['field_product_brand']['alter']['trim'] = 0;
$handler->display->display_options['fields']['field_product_brand']['alter']['html'] = 0;
$handler->display->display_options['fields']['field_product_brand']['element_label_colon'] = 1;
$handler->display->display_options['fields']['field_product_brand']['element_default_classes'] = 1;
$handler->display->display_options['fields']['field_product_brand']['hide_empty'] = 0;
$handler->display->display_options['fields']['field_product_brand']['empty_zero'] = 0;
$handler->display->display_options['fields']['field_product_brand']['hide_alter_empty'] = 0;
$handler->display->display_options['fields']['field_product_brand']['type'] = 'taxonomy_term_reference_plain';
$handler->display->display_options['fields']['field_product_brand']['field_api_classes'] = 0;
/* Field: SUM(Commerce Line Item: Quantity) */
$handler->display->display_options['fields']['quantity']['id'] = 'quantity';
$handler->display->display_options['fields']['quantity']['table'] = 'commerce_line_item';
$handler->display->display_options['fields']['quantity']['field'] = 'quantity';
$handler->display->display_options['fields']['quantity']['group_type'] = 'sum';
$handler->display->display_options['fields']['quantity']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['quantity']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['quantity']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['quantity']['alter']['external'] = 0;
$handler->display->display_options['fields']['quantity']['alter']['replace_spaces'] = 0;
$handler->display->display_options['fields']['quantity']['alter']['trim_whitespace'] = 0;
$handler->display->display_options['fields']['quantity']['alter']['nl2br'] = 0;
$handler->display->display_options['fields']['quantity']['alter']['word_boundary'] = 1;
$handler->display->display_options['fields']['quantity']['alter']['ellipsis'] = 1;
$handler->display->display_options['fields']['quantity']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['quantity']['alter']['trim'] = 0;
$handler->display->display_options['fields']['quantity']['alter']['html'] = 0;
$handler->display->display_options['fields']['quantity']['element_label_colon'] = 1;
$handler->display->display_options['fields']['quantity']['element_default_classes'] = 1;
$handler->display->display_options['fields']['quantity']['hide_empty'] = 0;
$handler->display->display_options['fields']['quantity']['empty_zero'] = 0;
$handler->display->display_options['fields']['quantity']['hide_alter_empty'] = 0;
$handler->display->display_options['fields']['quantity']['set_precision'] = 1;
$handler->display->display_options['fields']['quantity']['precision'] = '1';
$handler->display->display_options['fields']['quantity']['format_plural'] = 0;
/* Field: SUM(Commerce Line item: Total) */
$handler->display->display_options['fields']['commerce_total']['id'] = 'commerce_total';
$handler->display->display_options['fields']['commerce_total']['table'] = 'field_data_commerce_total';
$handler->display->display_options['fields']['commerce_total']['field'] = 'commerce_total';
$handler->display->display_options['fields']['commerce_total']['group_type'] = 'sum';
$handler->display->display_options['fields']['commerce_total']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['commerce_total']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['commerce_total']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['commerce_total']['alter']['external'] = 0;
$handler->display->display_options['fields']['commerce_total']['alter']['replace_spaces'] = 0;
$handler->display->display_options['fields']['commerce_total']['alter']['trim_whitespace'] = 0;
$handler->display->display_options['fields']['commerce_total']['alter']['nl2br'] = 0;
$handler->display->display_options['fields']['commerce_total']['alter']['word_boundary'] = 1;
$handler->display->display_options['fields']['commerce_total']['alter']['ellipsis'] = 1;
$handler->display->display_options['fields']['commerce_total']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['commerce_total']['alter']['trim'] = 0;
$handler->display->display_options['fields']['commerce_total']['alter']['html'] = 0;
$handler->display->display_options['fields']['commerce_total']['element_label_colon'] = 1;
$handler->display->display_options['fields']['commerce_total']['element_default_classes'] = 1;
$handler->display->display_options['fields']['commerce_total']['hide_empty'] = 0;
$handler->display->display_options['fields']['commerce_total']['empty_zero'] = 0;
$handler->display->display_options['fields']['commerce_total']['hide_alter_empty'] = 0;
$handler->display->display_options['fields']['commerce_total']['click_sort_column'] = 'amount';
$handler->display->display_options['fields']['commerce_total']['type'] = 'commerce_price_formatted_components';
$handler->display->display_options['fields']['commerce_total']['settings'] = array(
'calculation' => FALSE,
);
$handler->display->display_options['fields']['commerce_total']['field_api_classes'] = 0;
/* Sort criterion: Commerce Line item: Product (commerce_product) */
$handler->display->display_options['sorts']['commerce_product_product_id']['id'] = 'commerce_product_product_id';
$handler->display->display_options['sorts']['commerce_product_product_id']['table'] = 'field_data_commerce_product';
$handler->display->display_options['sorts']['commerce_product_product_id']['field'] = 'commerce_product_product_id';

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'best-sellers';

jantoine’s picture

I ran into the same problem with agregate views. Updating the "Group Column" setting for all fields to "Entity ID" removed the error. I'm not sure why this works, so I am leaving the issue open.

Cheers,

Antoine

bengt’s picture

I have the same issue and it appears with views (using Views 7.x-3.0 and Drupal Commerce 7.x-1.1) using aggregation. I can get around it as @AntoineSolutions did. (I am trying to make a view of multi-valued product display pages which shouldn't show multiple rows per product display node as explained here: http://drupal.org/node/1200536#comment-4957410.)

But I had an idea of showing the lowest priced product (i.e. in multi-valued product display pages) instead of just the first referenced product in the product reference field "field_product". So I thought I could set the Aggregation type to "Lowest" and the Group column to "Amount" for the field Commerce Product: Price. This produces the PHP notices but still doesn't give me the lowest priced product.

Sorry, I don't want to hijack this issue, but I realize now this actually becomes two questions (the first one is the original):
1. Why the notice(s)?
2. Why doesn't Aggregation type "Lowest" give me the lowest priced product?

(Besides the original notice I also gets the following notice: Notice: Undefined index: currency_code in commerce_line_item_unit_price_multiply() (row 202 of /home/../sites/all/modules/commerce/modules/line_item/commerce_line_item.rules.inc).

bengt’s picture

Found that I can get rid of the notices even if I have Aggregation type to "Lowest" and the Group column to "Amount", if I also check "Currency_code" under Group columns (additional). But this doesn't solve the second question in #10 and I don't know what it means to add this additional group column.

vegtabill’s picture

Just checking on the status of this one. I, too, am experiencing the error in question. I was able to get rid of the notification using the method described by AntoineSolutions; however, this caused my summed values to display incorrectly (they were no longer properly summed). Removing the Entity ID grouping option fixed the sums, but reintroduced the error notification.

If progress has been made with this, I would greatly appreciate a shove in the appropriate direction! Thanks in advance.

BassistJimmyJam’s picture

Status: Active » Needs review
FileSize
715 bytes

I'm having the same issue. I have a view of orders that is aggregated by group (we use og and our orders are group content) and I'm trying to sum the order totals. The summing works and the totals are formatted properly, but I'm getting the notice below once for each row in the view:

Notice: Undefined index: currency_code in commerce_price_field_formatter_view()

I added dpm($item); to commerce_price_field_formatter_view() and the array does indeed only have a single index, "amount", which contains the sum of the raw amounts. This should work according to bojanz's comment at http://drupal.org/node/1123492#comment-4563692, and it technically does, but the notices are an issue.

Grouping by the entity id as suggested by AntoineSolutions above breaks the sum, so it is not a valid workaround.

The attached patch will check the array for a currency code in the array and uses it if present or uses NULL, which is essentially what is already being passed when it is empty but without the notice. Commerce will then fallback to using the default currency code.

What this patch does not do is address the underlying issue. If all of the price field values are using the same currency code, that is the currency code that should be used. If the price field's are using different currency code... that's an interesting, not to mention much more difficult, problem to tackle.

vegtabill’s picture

Thank you, BassistJimmyJam, this is perfect for my situation. Thanks for pointing out the potential issue with different currency codes, but in this case I don't think that will be a problem for me.

Thanks again!

esbenvb’s picture

NO NEED TO PATCH!

As benght says in #11 you can just add the currency code to the aggregated columns.

I did this: http://imgur.com/zCB1Y and it removed the notices.

rszrama’s picture

Issue tags: +1.3 review

Tagging. We either need to patch this or document the fix.

helior’s picture

Status: Needs review » Postponed (maintainer needs more info)

I'm having a hard time replicating this issue for review... Can somebody provide an exported view? Or maybe confirm that the issue is resolved?

BassistJimmyJam’s picture

Status: Postponed (maintainer needs more info) » Needs review
FileSize
6.43 KB

Attached is a view of aggregated commerce orders. Without the patch in #13 this throws the notice. If you add the currency code as an additional grouping column in the field's aggregation settings the notice goes away. However, the view will SUM the currency code which results in a "0" causing the currency code to fallback to the default, which is essentially what my patch does without adding an extra aggregation column to the query.

Setting back to needs review because of the attached patch.

helior’s picture

Ah I see now, thank you for that export @BassistJimmyJam! I also noticed that "Formatted amount with components" also suffers from that same issue (especially when there are no configured price components, which will in turn produce invalid arguments for it's loop). We should probably tackle both issues here while we're at it.

helior’s picture

Assigned: Unassigned » rszrama
chrisgonzales’s picture

I had this issue with views 7.x-3.3 and commerce 7.x-1.2, I was having a similar issue to #10 and doing #11 fixed my issue.

rszrama’s picture

Assigned: rszrama » Unassigned
Status: Needs review » Needs work

I briefly chatted this over in IRC with helior, and I think we need to push for a deeper solution. The patch as written simply covers up the underlying problem, which is that we need some way to tell Views that aggregation should only work on the amount column of price fields. Additionally, we need to be able to tell Views that even though only the amount column should be aggregated, we still need a currency code to render the price field.

Seems to me we can do this by either choosing a representative currency code from the result set or just letting the user specify the currency code in the aggregation settings. The problem is we can't really do currency conversion in the query, and I don't know if Views can technically handle returning the total amounts per currency so we can convert them to a target currency post-query.

rszrama’s picture

Had a chance to talk this over with merlinofchaos, and we can actually find a pointer in views_handler_field_field.inc in its get_value() method. The comments start at around line 654, where we see where data is being left out on purpose. It would be nice to find some core solution, even a manual way to supply default values, but the quick fix we can search for would involve a custom Price field handler.

To register our own field handler, we can find the necessary code at the top of field.views.inc. That module invokes hook_field_views_data() to let our module specify its own Views data for its fields. Ostensibly we just need to override the get_value() method, but perhaps there are other compelling reasons to begin to implement our own handlers.

Matt Habermehl’s picture

Quick thanks to bengt in #11: Your solution fixed my problem.

lorenz’s picture

Hi,

I am using Commerce Kickstart 7.x-2.0-rc1. I have the same error which appeared when i was working on entity fields and enabling some for translation.

What is the exact path to the admin page where I can adjust the settings as in #11 ?

Where is the file commerce_price.module and the price module located in case I want to apply the patch as in #19? I do not find such a module anywhere in profiles/commerce_kickstart/modules/. I have the modules "Commerce Extra Price Formatters" and "Price" enabled.

Does an update to the Kickstart version 7.x-2.0-rc2 solve this? I tried the upgrade but it failed, and does so for some other people as well, as described here:
http://www.drupalcommerce.org/questions/4683/update-database-fails-error...

Thank you for your help!

drupalerocant’s picture

Thanks very much bengt in #11 did the trick for me.

chaiwei’s picture

#15 solutions works for me. Thanks to #11 answer as well.

Just tick the "currency code" in the aggregation (additonal) section.

zterry95’s picture

Thanks #22, #23, Ryan detailed explanations.

rszrama’s picture

Status: Needs work » Fixed

Alrighty, so I buckled down to solve this, and what I came up with based on my previous comments is a Price field Views handler that looks to see if the field has aggregation on, and if it finds aggregation on the amount column it adds a currency code to the values object. It's a bit of a hack to avoid overriding the entire get_value() array, because we're adding a pseudo-column to the result set, but it's using a column name that can never have a collision b/c it's appending a column name to another in a way that Views won't generate.

In testing using the example View from #18 above I don't run into any issues, and it simply passes through to views_handler_field_field if aggregation isn't enabled.

Commit: http://drupalcode.org/project/commerce.git/commitdiff/49a3085

trianam’s picture

The solution #15 works for me

rszrama’s picture

Glad to hear it. : )

rszrama’s picture

Hah, ok. Well, you shouldn't need to use that old method, because what it's really doing is just creating a 'currency_code' value of 0 - i.e. an invalid currency code. However, because of a fluke in PHP, that will result in your site's default currency being used. Obviously not an issue for single currency sites.

But still, if you were to use my patch or Commerce 1.x-dev, your aggregation hack will be unnecessary and/or ignored.

Daniel Wentsch’s picture

Thanks @bengt for #11, that solved the issue for me :)

check "Currency_code" under Group columns (additional)

rszrama’s picture

That is an incorrect fix. You should just update to Commerce 1.6.

Daniel Wentsch’s picture

Okay, gonna do that, didn't see the update yet.

rszrama’s picture

No sweat. fwiw, the fix applied in Commerce 1.6 will simply ignore the "currency code" checkbox in the aggregation settings even if it's clicked - so no ill effect if you already "fixed" it the other way. The difference is the approach in #11 simply uses a NULL (or empty) currency code instead of one from the result set.

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