Im working with Product attributes I could notice a bug when having translated taxonomies and products in the cart form in the product display

How to reproduce:

Install i18n module, configure content translation and taxonomy translation
Install entity translation and configure for translate a product type

Add any other language for your drupal
Create a taxonomy add some terms and translate the terms
Create a product type enable translation via Entity translation module
Add a term reference field that references the created taxonomy
Enable translation for the created term reference field
Create a 1 or more products with different attributes (select different values for each product in your term reference field)
Translate the product (Select the correct term for the language of the translation in the term reference field)
Create a product display for your products
Select all the products you already created for use with this product display
Translate your product display
Select "Add to cart form" as the format for the product reference field in manage display tab of your product display content type.
Now enable a block for switch the language

You will see that the Add to cart form only works ok with one of the languages, "the language of your products" with the other language the select list of the add to cart form is empty and does not displayed the translated taxonomies.

Check the images for understand better

One of the images show the empty select box for the attribute color in the "add to cart form" this is because the original language of the product does not match the actual selected language

The other image shows correctly the select box because the actual selected language matches the product's original language

if you need more details let me know.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ssoulless’s picture

Ok after a lot of debug I could find the problem but do not know what is the best way of solve it.

commerce_cart.module line 2049


'#options' => array_intersect_key($data['options'], drupal_map_assoc($used_options[$field_name])),

The #options represents the values for the select lists of the "add to cart form"

what it does is find the keys that match with the $used_options[$field_name] and the $data['options'] arrays

When the language of the site is the product's language it works ok because the $used_options['field_name'] values match with the desired $data['options'] that are the taxonomy ids or tid's

but when using the site in another language the values of the $used_options['field_name'] array are still the tid's that correspond to the original product's language as result no items are displayed in the select list of the "add to cart form"

check the image of my debug so you can understand better

The first image is when the site's language is the original product's language
and the second image is when I change the site's language with the language switcher, so the taxonomy terms are the translated ones

ssoulless’s picture

Anonymous’s picture

Probably running into this same problem today. I did not actually translate any of my taxonomies but I did enable this option. My product display node shows the add to cart form attributes (in my case select a date and select a location) as long as I ONLY have one product. As soon as I create a second product for display on the same display node, the add to cart form button and attributes disappear.

I haven't considered the i18n connection but will further investigate. Could be a mismatch between en and und langauges between the display node and the product?

Anonymous’s picture

Ok, figured out what is happening in my case.

Multilingual site i18n module and associated modules installed.

1. I created a content type for product display called 'Courses'.
2. I created several products.
3. Created two product display nodes, 'Course 1', node id = 2521 and 'Course 2', node id = 2522

When I added ONE product to each display content node, all the product reference "add to cart" attributes displayed normally.

When I added a second product to a display content node the add to cart form for that node no longer displayed. The button was missing and the attributes were missing.

My products were set to language neutral, my product display nodes were set to "English".

I edited the product display nodes and set them to "und" (language neutral).

I had to "reselect" the products associated with each node as somehow this information was lost when I changed the language settings? (strange)...

Once I edited each product display node again and selected the associated products, the add to cart button and product attributes are displaying normally.

Not sure what to make of this bug. Will investigate further.

Ian Clysdale’s picture

Here's a patch that seems to fix this issue. When loading the options, it checks if the entity translation module is enabled, and if so it takes the mild performance hit of loading the taxonomy terms as entities and pulling the names from there.

Ian Clysdale’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 5: commerce-entity-translation-attribute-widgets-2293537-5.patch, failed testing.

Ian Clysdale’s picture

Status: Needs work » Needs review
FileSize
1.12 KB

Now with better error handling. :)

Status: Needs review » Needs work

The last submitted patch, 8: commerce-entity-translation-attribute-widgets-2293537-7.patch, failed testing.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 8: commerce-entity-translation-attribute-widgets-2293537-7.patch, failed testing.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 8: commerce-entity-translation-attribute-widgets-2293537-7.patch, failed testing.

Ian Clysdale’s picture

OK, sorry for the re-testing, but I figured the problem had to be with the test suites, and was running it to see if it passed.

However, they seem to be passing for other patches. I'm more than a little confused, and if anyone's got any insight in why the tests are failing here, I'd love to know.

What's failing is the check for whether the order object was created and saved - my patch doesn't go anywhere close to saving order objects, but only tweaks the display of add-to-cart forms. And the tests all pass if I run them in SimpleTest in my environment - both in a real site using entity translation, and in a minimal Drupal install with entity translation disabled.

I'd really like to bring this in to Commerce (and know if it fixes the problem for other folks), so anyone smarter than me, I'd love to hear from you.

mmtt’s picture

I've tried the patch and it seems to work. I would be great if it could be commited.

Sakhmed’s picture

Super! #8 worked for me

MariskaTh’s picture

Hello,

I tried patch #8, it didn't change anything. I have no idea why it does work for Sakhmed and mmtt and not for me.

Looking forward to more experiences from other people.

Best regards,
Mariska.

rszrama’s picture

Version: 7.x-1.9 » 7.x-1.x-dev
Priority: Major » Normal
Status: Needs work » Needs review

Not sure why this caused test failures; probably the target version. Updating.

czigor’s picture

I can reproduce the issue. Some additional info:
- The taxonomy vocabulary needs to use i18n_taxonomy translation, not entity_translation.
- The term reference field needs to be a product attribute.
- I'm using latest dev from everything.

The patch in #8 does not work for me. The attached patch does. The issue is related to EMW's buggy language handling.

sjmobley’s picture

Issue tags: +sprint
mglaman’s picture

+++ b/modules/cart/commerce_cart.module
@@ -2099,7 +2099,10 @@ function commerce_cart_add_to_cart_form($form, &$form_state, $line_item, $show_q
+            $column = reset($columns);
...
+            $used_options[$field_name][] = $field_value[0][$column];

Reviewing this, but based on other code examples, we could just do $columns[0]

mglaman’s picture

Status: Needs review » Needs work
+++ b/modules/cart/commerce_cart.module
@@ -2099,7 +2099,10 @@ function commerce_cart_add_to_cart_form($form, &$form_state, $line_item, $show_q
             if ($product_wrapper->{$field_name}->raw() != NULL) {
...
-            $used_options[$field_name][] = $product_wrapper->{$field_name}->raw();

Actually, various places use the "raw" value check. We should fix these for consistancy.

mglaman’s picture

Status: Needs work » Needs review
FileSize
2.27 KB

Here is a patch that removes usages of the product entity wrapper. Using the following core methods to retrieve the field value, the entity wrapper's "raw" method is no longer being used.

            $columns = array_keys($field['columns']);
            $field_items = field_get_items('commerce_product', $product, $field_name);
            $field_value = $field_items[0][$columns[0]];

Status: Needs review » Needs work

The last submitted patch, 23: 2293537-23.patch, failed testing. View results

mglaman’s picture

+++ b/modules/cart/commerce_cart.module
@@ -2088,7 +2090,7 @@ function commerce_cart_add_to_cart_form($form, &$form_state, $line_item, $show_q
-                if ($product_wrapper->{$used_field_name}->raw() != $form['attributes'][$used_field_name]['#default_value']) {
+                if ($field_value != $form['attributes'][$used_field_name]['#default_value']) {

I see. This call on raw was from a different field, causing failures.

mglaman’s picture

Status: Needs work » Needs review
FileSize
2.39 KB

Here is an updated patch to fix that test failure.

czigor’s picture

#26 looks better and works just as well as #19 for me. The taxonomy terms appear translated now in the product attribute select.

However, the product fields (like price) are not updated when selecting another term if the language is not English. This might be a different issue though.

czigor’s picture

Getting rid of all product EMWs fixes the aforementioned issue too.

mglaman’s picture

Assigned: Unassigned » rszrama
Status: Needs review » Reviewed & tested by the community

This looks good, marking it RTBC and assigning to rzrama for feedback on the decision to use things like reset($product_field_value[0]);. I don't see any other way, since it could be an option field, taxonomy term reference field, or entity reference field, which all have different value keys, but all have the value as the first key.

rszrama’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
8.32 KB

The approach seems reasonable, and in local testing with both an options field and a taxonomy term reference field as attributes, it all still functions properly. For maintainability, though, I think we should standardize on using a variable named *_items to hold the field_get_items() response and ones named *_value for the reset value. (The patch was only doing that in one place, so I updated it to do it in all of them.)

I'm a little nervous about hardcoding the 0 array key, buuuut we're talking about attribute field values that by nature should only be single value. If someone comes up with a failing scenario, we can just add an intermediate reset() to extract the first item from the items array. I'm only aware of items arrays being non-zero indexed in the event a multi-value field has some values removed without resetting the delta array.

We'll see what happens. : D

  • rszrama committed 72873ea on 7.x-1.x authored by czigor
    Issue #2293537 by mglaman, Ian Clysdale, czigor, rszrama, ssoulless:...
rszrama’s picture

Assigned: rszrama » Unassigned
Status: Needs review » Fixed

Tests passed. Committed.

Status: Fixed » Closed (fixed)

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