Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot translate the values of "Custom Product Attributes" of "Variable Product" in Woocommerce #277

Closed
ventouris opened this issue Oct 3, 2015 · 54 comments

Comments

@ventouris
Copy link

I installed the latest version of both qtranslate-x and Woocommerce for qtranslate-x. However, I cannot translate the values of the attributes in Woocommerce. I use the following for the label:

[:en]Action[:de]Vorgang[:]

and this one for the values:

[:en]Renting | Return[:de]Anmietung | Rückgabe[:]

The label works like a charm :) The values doesn't work. I don't have any error on the console.

The HTML result is this one:

<select id="vorgang" class="" name="attribute_vorgang" data-attribute_name="attribute_vorgang"><option value="">Choose an option</option><option value="[:en]Renting" class="attached enabled">Renting</option><option value="Return[:de]Anmietung" class="attached enabled">(DE) ReturnAnmietung</option><option value="Rückgabe[:]" class="attached enabled"></option></select>

@johnclause
Copy link
Member

I have not tried to reproduce, but my guess would be that the problem is in '|' character. Do you have the problem if you remove it? I remember some fields in Woocommerce may have multiple values separated by '|', so it may interfere. You would have to escape it if needed. Please, let me know if my guess is correct.

@johnclause
Copy link
Member

BTW, why do you need to do raw translation? Do you use integrating plugin: https://wordpress.org/plugins/woocommerce-qtranslate-x/

@ventouris
Copy link
Author

@johnclause You are right. The problem is the "|" character. The vertical line that used for the select values. I am not sure how I can escape it. As for the woocommerce-qtranslate-x:

When I first installed it, I had the two flags about the section, but changing the language kept the same content for the attributes. Didn't use it for the translation. I tried to update the qtranslate-x from the Github directly from the source code, by uploading the new files from the FTP and since then, the woocommerce-qtranslate-x doesn't work. I cannot see the flags any more.

@johnclause
Copy link
Member

I am not sure how I can escape it

Find the HTML code like &#xxxx; for | and try it. Otherwise ask Woo how they designed it.

changing the language kept the same content for the attributes

You probably need to resave all attributes after installing integrating plugin on a developed site, I recall something like this.

I cannot see the flags any more

Just install everything nicely, properly and freshly. People report that it all actually works well enough. It does a few things that unachievable with simple raw translations, sending emails in the language of order, for example.

I am closing this issue for now, we can still write into a closed issue, or we can re-open it, if needed.

@ventouris
Copy link
Author

I removed them all and installed from the start. Now, the flags are back there, but even if I save the attributes and update the page, the content changed for both the languages. :(

Edit: Plus, when I use the escaped code, after the update, they turned to "|" again.

@johnclause
Copy link
Member

Cannot you use different characters instead of '|'? Or then you have to dig out the place in the code and see how easy it would be to adjust it. Ask Woo what do they suggest? This problem is unrelated to qtx.

@ventouris
Copy link
Author

I think that that problem of the character "|" is a side issue and not the main one. The main issue that it is related with qtx is that the flags on Product Data table don't work. You change flags, make changes on product data, save both attributes and page and it changes it for both languages. There was also another issue about it, so I am not alone on this.

And just show that the same happened on the Yoast SEO table. Even if I have enabled the Yoast & qtranslate X plugin. I can move between the two flags, but the content doesn't change at all.

@johnclause
Copy link
Member

Maybe those fields are not multilingual and need integration? Do they have a color bar on the left side like other multilingual fields do?

Please provide step-by-step instructions how to reproduce the problem, preferably under a publicly available theme like "Storefront" and the minimum number of active plugins. For example, you mentioned "value of attributes", but I do not know which admin or front page do you refer to, etc.

If there is a similar issue, please, cite it here with a link.

Do you have any errors in Java Console, which can be opened with Ctrl+Shift+J? Errors in error_log? Have you done the initial troubleshooting: https://qtranslatexteam.wordpress.com/troubleshooting/? What did you find out?

@johnclause johnclause reopened this Oct 8, 2015
@ventouris
Copy link
Author

Install the qTranslate-X either from Wordpress directly or from Github. Download Yoast and Woocommerce support from Github is the Wordpress has an older version. Install them on admin.

Here is a screenshot:
screenshot

The latest version of Yoast SEO & qTranslate add the colors on the inputs and makes them translatable. The one from Wordpress admin, doesn't. But as you can see on the product data table, there isn't a flag and the inputs that I am referring as attributes with labels and values don't have a translatable color.

Nothing weird on the Dev Console.

@johnclause johnclause changed the title Cannot translate the values of attributes in Woocommerce Cannot translate the values of "Custom Product Attributes" of "Variable Product" in Woocommerce Oct 10, 2015
@johnclause johnclause added the bug label Oct 10, 2015
@johnclause
Copy link
Member

Indeed, there is a bug in the latest qtx, otherwise front end should work well. Custom Variable products were never integrated yet, but raw values like yours should work. Maybe it is more logical to have it like "[:en]Renting[:de]Anmietung[:] | [:en]Return[:de]Ruckgabe[:]", or it may not be important, I am not sure. The latter should work by design, the original version might work by accident.

Anyway, I will update plugin "Woo&qtx" shortly to fix the problems.

@ventouris
Copy link
Author

That's great! Please, let me know so I can test any side effects and possible fixes or bugs. Thank you :)

@johnclause
Copy link
Member

I updated plugin https://github.com/qTranslate-Team/woocommerce-qtranslate-x at GitHub. Could you please test it? Update qTranslate-X to the latest 3.4.6.1 too.

Please, read section "Known Issues" before testing, in order not to report what is already known.

"Custom Product Variations", which was your original trouble, has never been finished. Woocommerce has done it in an unexpected way, so it is unclear how to integrate that piece, but raw format should work. Although, most people use "Product Attributes" instead, which work more stable.

@ventouris
Copy link
Author

It seems that it works like a charm. I did almost a few general tests. I will do more in the week. If you want, we can close the issue and in case I find a related bug, I can open it again :)

@johnclause
Copy link
Member

I will probably release it now shortly for everyone. Thank you, @ventouris, very much for all your help.

@AleCss81
Copy link

@johnclause Hi John, I've implemented the solution you've explained here some months ago, but it doesn't work for me

I've put this on custom product value filed:

@johnclause
Copy link
Member

@AleCss81: I think your message is not finished, is it?

@AleCss81
Copy link

@johnclause hi John, the message were been truncated, I don't know why. Maybe, my code was deleted automatically by editor bacause I haven't used the correct tool to insert code.

Now, I was saying that I've put this code on custom product value: [:en]White [:it]Bianco[:] | [:en]Vanilla [:it]Vaniglia[:] | [:en]Natural [:it]Naturale{:} | [:en]Pink [:it]Rosa[:] | [:en]Grey [:it]Grigio[:]

but it doesn't work. The selection element not show any variations. You can see the result here: http://hugsfactory.com/product/nursery-sack/

Can you help me?

Thank you very much

@johnclause
Copy link
Member

johnclause commented Apr 27, 2016

@AleCss81: I cannot reproduce what you describe. It works for me. Try to delete the attributes and re-create them? Contact us if you wish to give us admin access.

@AleCss81
Copy link

@johnclause Hi John, is it working on this page http://hugsfactory.com/product/nursery-sack/?

As you can see in the screenshot, it doesn't work! At least for me, in all browsers.

image

Apart from that, I have used the "contact us" form providing the admin access as you have suggested.

Thank you very much, I wait for a response.

@dieSuse
Copy link

dieSuse commented Apr 28, 2016

Hi,

I had the same problem as AleCss81. It took me crazy for days and the solution above didn't work for me neither.

I found out that some translations __() were not correct in class WC_Product_Variable method get_variation_attributes(), so that the result variation array was always empty. I derived an own class My_WC_Product_Variable and changed the code in get_variation_attributes().

Maybe it was not the cleanest way but it worked for me.

Regards,
Suse.

@johnclause
Copy link
Member

@dieSuse: could you display your changes, so that @AleCss81 could use the same solution until we come up with a better one?

@AleCss81
Copy link

@johnclause yes, it could be useful! @dieSuse effectively, I don't understand what you have implemented, can you explain what to do? Many thanks

@dieSuse
Copy link

dieSuse commented Apr 29, 2016

Hi all,

I confess I do not remember in detail but here is my class:

has_child() ) { return $variation_attributes; } $attributes = $this->get_attributes(); foreach ( $attributes as $attribute ) { if ( ! $attribute['is_variation'] ) { continue; } $values = array(); $attribute_field_name = 'attribute_' . sanitize_title( $attribute['name'] ); // Get used values from children variations foreach ( $this->get_children() as $child_id ) { $variation = $this->get_child( $child_id ); if ( ! empty( $variation->variation_id ) ) { if ( ! $variation->variation_is_visible() ) { continue; // Disabled or hidden } $child_variation_attributes = $variation->get_variation_attributes(); if ( isset( $child_variation_attributes[ $attribute_field_name ] ) ) { $values[] = __($child_variation_attributes[ $attribute_field_name ]); } } } // empty value indicates that all options for given attribute are available if ( in_array( '', $values ) ) { $values = $attribute['is_taxonomy'] ? wp_get_post_terms( $this->id, $attribute['name'], array( 'fields' => 'slugs' ) ) : wc_get_text_attributes( $attribute['value'] ); // Get custom attributes (non taxonomy) as defined } elseif ( ! $attribute['is_taxonomy'] ) { $text_attributes = wc_get_text_attributes( $attribute['value'] ); $assigned_text_attributes = $values; $values = array(); // translation $i=0; foreach ($text_attributes as $attr) { $text_attributes[$i] = __($attr); $i++; } // Pre 2.4 handling where 'slugs' were saved instead of the full text attribute if ( version_compare( get_post_meta( $this->id, '_product_version', true ), '2.4.0', '<' ) ) { $assigned_text_attributes = array_map( 'sanitize_title', $assigned_text_attributes ); foreach ( $text_attributes as $text_attribute ) { if ( in_array( sanitize_title( $text_attribute ), $assigned_text_attributes ) ) { $values[] = $text_attribute; } } } else { foreach ( $text_attributes as $text_attribute ) { if ( in_array( $text_attribute, $assigned_text_attributes ) ) { $values[] = $text_attribute; } } } } $variation_attributes[ $attribute['name'] ] = array_unique( $values ); } return $variation_attributes; } ``` } If I remember correctly I found out, that the comparison in line 73/74 has no compliances, so I started at this point. I'm not sure if there was one element translated and the other not or if it was just a thing of upper and lower case... In line 39 I added the translation for the attributes of array $values. And I inserted lines 56-60 and did the translation for each element of array $text_attributes. I think that were the 2 places, where I changed the code. I'm using WooCommerce 2.5.5 and WooCommerce & qTranslate-X 1.3. Regards, Suse

@johnclause
Copy link
Member

johnclause commented Apr 30, 2016

So, to be precise, one can modify file /wp-content/plugins/woocommerce/includes/class-wc-product-variable.php to achieve the same result.

Line 406

      $values[] = $child_variation_attributes[ $attribute_field_name ];

should be replaced with

      $values[] = __($child_variation_attributes[ $attribute_field_name ]);

which adds translation of the $values[].

Then, at line 419, after lines

} elseif ( ! $attribute['is_taxonomy'] ) {
$text_attributes = wc_get_text_attributes( $attribute['value'] );
$assigned_text_attributes = $values;
$values = array();

inserted a translation of $text_attributes:

// translation
$i=0;
foreach ($text_attributes as $attr)
{
$text_attributes[$i] = __($attr);
$i++;
}

@AleCss81, could you try that and see if your problem goes away?

This would give your temporary fix until we find a permanent solution.

@johnclause
Copy link
Member

@AleCss81: I simply followed my own instructions and it worked. I am not sure what step did you miss. I removed an attribute, saved product, added attribute with only two languages enabled, saved product, then re-save variations (do not need to delete them), and the drop down appeared.

The problem is that you cannot edit attribute name after it is created once. You may edit values and variations as you pleased, but not the attribute name.

If you need to change attribute name, then do as above, delete attribute, save product, add attribute again, save product, re-save variations.

Also, do not forget to put [:] at the end, and use [:] consistently, do not mix it with {:}, as you had it.

I hope you will be fine from now on.

@AleCss81
Copy link

AleCss81 commented May 2, 2016

Hi @johnclause it works, but if I change the variations to achieve the result expected (i.e White - Grey roses) the dropdown no works anymore. I can't leave "Any" - "Any". Now, it doesn't work anymore also if I leave Any - Any. Don't know why.

Do I have to implement the customization on file /class-wc-product-variable.php you have exlpained above?

Thanks

@johnclause
Copy link
Member

Wow, I did not try that. I did not try @dieSuse 's fix either. Yes, please, try their fix, and I meanwhile will try to work on the issue with complex variations.

@AleCss81
Copy link

AleCss81 commented May 2, 2016

Yeah, it works like a charm. Thanks @dieSuse and @johnclause for this excellent support.

@johnclause
Copy link
Member

BTW, @AleCss81, do you wish to update the wp and all plugins to the latest versions?

@johnclause
Copy link
Member

@dieSuse: where do you put your customized class, so that it does not get overwritten by the next Woo update?

I am meanwhile looking into a simpler solution and thinking about using filter 'trim', applied inside of wc_get_text_attributes to achieve translation at line 419.

BTW, at line 406, in my testing, $values come already translated. Is it a case in your setup with the latest of all?

@dieSuse
Copy link

dieSuse commented May 2, 2016

Hi,

yes, you are right: $values is already translated. I don't know anymore, why I used the translation method again. Maybe it was not in an earlier version? I did the stuff in January.

In my case, I wanted to have the variations on product overview already, because I don't have a single product page. Therefore I had to insert some code snippets in my functions.php where I get the variations from the product. This is where I use my class My_WC_Product_Variable directly by creating a new product with $product as parameter in the constructor.
This way My_WC_Product_Variable.php is just located in theme-folder/woocommerce.

  global $product;
  $myProduct = $product;
  if (class_exists('My_WC_Product_Variable'))
    $myProduct = new My_WC_Product_Variable($product);

If you have a fix, I'd like to use it as well. So just let me know.

Regards,
Suse.

@dieSuse
Copy link

dieSuse commented May 2, 2016

I don't know why, but the code is only displayed in a line when using ``.

@johnclause
Copy link
Member

Use ``` or press "<>" in the toolbar above the message ;)
I edited your message. I will let you know what I find out about possible filtering.

@johnclause
Copy link
Member

Hi, Could you both test the new version of the plugin: https://github.com/qTranslate-Team/woocommerce-qtranslate-x/archive/master.zip

The changes can be seen at qTranslate-Team/woocommerce-qtranslate-x@55429ca

Please, test the whole process including checkout and e-mails. Can you find something wrong?

Do not switch language at front end during one testing session: https://qtranslatexteam.wordpress.com/woocommerce-qtranslate-x-faq/#MiniCartCached

@AleCss81
Copy link

AleCss81 commented May 3, 2016

@johnclause this afternoon I will backup site and update wp and your plugin. I will not update some other plugins because I have made some customization on my own, but it's possible that the vendor changes classes names (it's already happened) and then I have to reproduce all my cutomization.

@johnclause
Copy link
Member

Yes, in such case I keep a list of all my customizations and keep re-applying them on each update until the authors put them permanently in the code. It does not take too long, since it usually not too many. How many do you have? I never customize WP code, not worth of messing up with it. There is a child theme framework where I can usually do all I want.

@AleCss81
Copy link

AleCss81 commented May 3, 2016

Yes, I use a child theme. The problem is with some plugins that changes their IDs and classes on every update. Even I keep a list of my customizations, I have to recollocate the new elements with different classes and IDs and re-apply the modifications. It's not a good job nor so quick.

With WP core no customzation at all ;)

@johnclause
Copy link
Member

@dieSuse: I hope you would be able to test too? If all ok, I would release it then.

@dieSuse
Copy link

dieSuse commented May 3, 2016

@johnclause: I just tried. It works fine.
Thanks for the fix!

(Regarding displaying the code: pressing "<>" inserts only ``. I didn't know, that I have to use it three times. Is that intend?)

@johnclause
Copy link
Member

Are we ready to release this version? Any problems?

@pandjarov
Copy link

Tried the version but still variations doesn't show if they are surrounded by [:en]Blue[:fr]Blu[:] for example :(

@ajitbhandari
Copy link

I made it work to change few lines for

Version 2.6.4
File: wp-content/plugins/woocommerce/includes/class-wc-product-variable.php
change to
Line 401 => $assigned_text_attributes = __($values);
Line 410 => $values[] = __($text_attribute);
Line 416 => $values[] = __($text_attribute);

@mk-ned
Copy link

mk-ned commented Oct 24, 2016

I made all changes described on this page, but none of them works for me except ajitbhandari's solution, besides it doesn't works on 1.3 only on 1.4. what am I interesting is that will not be the class-wc-product-variable.php overwritten after woocommerce updates? as I know it will be. so is there maybe any other solution?

@KVNHNF
Copy link

KVNHNF commented Jan 22, 2017

@johnclause

Hi John, awesome plugin!

I have the following setup:
-Woocommerce plugin
-Woocommerce + Qtranslate x plugin
-Qtranslate x plugin
-Woocommerce composite products plugin

Everything works fine except for "component short product description" which cannot translate even through manual integration. There is a blue line displaying on the left though, and this field translates fine for variable and simple products but not when the product is used as a component in WC composite products.

There, the field "short description" remains under this format: [eng]...[:], [fr]....[:]

I have spent hours trying to figure out a way, either by adding new classes, ids or filters to manual integration but none of it has worked so far...this field just won't translate

Do you have any idea how to fix it?

Many thanks for your help!

Best regards

@ajitbhandari
Copy link

@KVNHNF contact me on skype ajit.bhandari4 and show your problem ill try to guide you

thanks

@louismonteiro
Copy link

I have a hack done with javascript that works pretty good.

if you guys want it, send me an email: louismonteiro@gmail.com

@AymanEG
Copy link

AymanEG commented Apr 21, 2017

Hello guys , any update for this issue with for the last woocommerce version ?! or you did not update woocommerce yet !?

@wywarren
Copy link

For WooCommerce 3.x you still need to change the class-wc-product-variable.php but on line 207 for function get_variation_attributes() to return __($this->variation_attributes);. I will try to submit this to github. This is my first time doing a bug report submission fix.

@wywarren
Copy link

wywarren commented Apr 6, 2018

Got this as a reply:
https://screencast.com/t/LwWqJHByw

Seems like it could be still be fix on another class then where the array is assembled to translate each value there. See if anyone else wants to pitch in to find it.

@wywarren
Copy link

wywarren commented Apr 6, 2018

Found another safer/more working solution and have suggested another fix:
woocommerce/woocommerce#19630

@wywarren
Copy link

wywarren commented Apr 16, 2018

Another thing to note is that when you load a product page and click the select drop down, the variations are actually listed for a split second, then it disappears. I'm assuming some form of ajax call is the culprit. If you view the source code for the select drop down, it's showing as well.

@wywarren
Copy link

Still existing in latest build even with woocommerce modules. Using the woocommerce module yields this other error:

Warning: implode(): Invalid arguments passed in /var/www/html/sandbox/wp-content/plugins/woocommerce/includes/wc-attribute-functions.php on line 41

Disabling it allows for single language attribute values to work again. Note that it also breaks WooCommerce REST API. With the modules activated, attribute names can be updated but not values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests