This is the technical support forum for WPML - the multilingual WordPress plugin.
Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.
Tagged: Exception
This topic contains 13 replies, has 2 voices.
Last updated by cons-2 1 year, 10 months ago.
Assisted by: Dražen.
Author | Posts |
---|---|
January 31, 2023 at 12:41 pm #12929615 | |
cons-2 |
Hi. We're having trouble with a feature we built a while ago after a recent update (of WooCommerce Multilingual?). (WordPress, WooCommerce and all WPML-Plugins are up to date.) Our function retrieves ratings from all languages of a product and then lets us work with them. We usually process the get_comments-results further with wp_list_comments to convert reviews to html. $args = array ( 'post_type' => 'product', 'status'=> 'approve', 'post_status' => 'publish', 'comment__not_in' => $exclude_comment_ids, // exclude some comments, e.g. pinned reviews 'number' => $number, 'offset' => count($exclude_comment_ids), 'orderby' => 'comment_date', 'order' => 'DESC', 'post_id' => $product_id, 'date_query' => array( 'before' => $date_before, 'inclusive' => false, ) ); $comments = get_comments( $args ); // sometimes we need to return html: $comments_fix_order = array_reverse($comments); wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'woocommerce_comments' ) ), $comments_fix_order ); All reviews of all languages were displayed correctly in the past. So when I look at a product DE, all reviews have the DE flag. Expected behavior: We've been using this function for about a year. We haven't changed anything. What exactly are the arguments to run get_comments correct and make it compatible with the current versions of WPML? Input: Expected result: (Since it is a general question, I am not presenting any links or access information to our site. If it needs multiple steps and code tweaks, that's fine too. Just need an approach because the magic WPML applied under the hood isn't working anymore. I look forward to your answer. |
February 2, 2023 at 7:35 am #12944305 | |
Dražen Supporter
Languages: English (English ) Timezone: Europe/Zagreb (GMT+01:00) |
Hello, thanks for contacting us. In order to check this out further and consult with our 2nd tier, can you please recreate a simple example of the issue on our test site: - hidden link No need to create content or install any additional plugins, 1-2 products, and 1-2 reviews should be enough, please share all steps and code you used and how we can see the issue. Thanks, |
February 2, 2023 at 12:12 pm #12947033 | |
cons-2 |
Hi. I set up a page. You can find the link to the product in the footer - recent comments. Haven't added any custom code yet. What I have done Now the following error is visible: Translated reviews have the wrong flag. - "Markus" original lang: DE (I'll add my custom code later if necessary. |
February 2, 2023 at 12:24 pm #12947137 | |
Dražen Supporter
Languages: English (English ) Timezone: Europe/Zagreb (GMT+01:00) |
Hello, thanks for getting back and taking the time to reproduce. I have checked further on this and this is how it is expected to work once the review is translated, since WCML 4.12. This is because of search engine crawling and to keep the flag (language) consistent of the text review language. If the review is translated to EN, and in EN text it will show the EN flag and note (translated). If you wish you can proceed with reproducing your issue and we can continue to check further. Regards, |
February 2, 2023 at 5:02 pm #12950013 | |
cons-2 |
Thanks for the feedback. Let's get to the important topic. I made the following changes on your test site. I commented out the query limits and date filters. Here is a brief summary for readers: $args = array ( 'post_type' => 'product', 'status'=> 'approve', 'post_status' => 'publish', 'orderby' => 'comment_date', 'order' => 'DESC', 'post_id' => $product->get_id(), ); $comments = get_comments( $args ); // sometimes we need to return html: $comments_fix_order = array_reverse($comments); wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'woocommerce_comments' ) ), $comments_fix_order ); The code shown above results in reviews being displayed untranslated. See image captioned "Mode: custom" above reviews. Normally the query should come out the same as the standard WooCommerce query, I think. First and foremost, it would be important for us that the reviews are also displayed in translated form for individual get_comments() calls. How must the get_comments() call be adjusted so that the reviews are displayed correctly - as in the standard Woocommerce implementation? Why are the reviews shown untranslated at all? |
February 2, 2023 at 5:08 pm #12950069 | |
cons-2 |
Edit: With that I refer to the last review on the screenshots. |
February 3, 2023 at 7:47 am #12953139 | |
Dražen Supporter
Languages: English (English ) Timezone: Europe/Zagreb (GMT+01:00) |
Hello, thanks for such descriptive reply and taking the time to reproduce it. I have now shared this case with our 2nd tier to check and advise. We will update you soon. Regards, |
February 3, 2023 at 2:23 pm #12956461 | |
Dražen Supporter
Languages: English (English ) Timezone: Europe/Zagreb (GMT+01:00) |
Hello, our 2nd tier checked, I am sharing his thoughts on this. Let me know if this helps and if you need more information: - As far I can see we do not do any filtering on get_comments method, we translate the comments on `woocommerce_review_before` hook. Probably user is not calling that hook. Regards, |
February 3, 2023 at 4:13 pm #12957343 | |
cons-2 |
Hello. Thanks for the feedback. Checked where the "woocommerce_review_before" hook is called by default. By default the hook is called in woocommerce/templates/single-product/review.php. We have not changed the callback. So the hook "woocommerce_review_before" should be called normally. I also tested it on your test site. add_action('woocommerce_review_before', function(){ echo '<small><strong>woocommerce_review_before - added at the end of woocommerce.php plugin file - you can remove it if neccessary</strong></small>'; }); We can thus rule out that the hook is not called. In general: The only thing I do with my customization is fetch the reviews and pass them to wp_list_comments. I have not changed the WooCommerce display logic. The faulty translation of the reviews when called by get_comments() has only recently appeared. I'm curious what other ideas you have. If you can get it to work, we might have new insights. |
February 3, 2023 at 5:02 pm #12957713 | |
cons-2 |
I followed your function: This brought new insights. When I retrieve reviews with get_comments(), then the comment_post_ID your getReviewStringName() function retrieves is different than the default. Here is an example from our online shop (live page). Here is a modification in WPML code that I tried. - file: wp-content/plugins/woocommerce-multilingual/classes/Reviews/Translations/FrontEndHooks.php private static function getReviewStringName( $review ) { // lang code in which review was submitted by user // same as lang_code of product the user has commented on // not sure how to query it programmatically $comment_original_lang = 'fr'; // get product_id in language the user has submitted the review $post_id = apply_filters('wpml_object_id', Obj::prop( 'comment_post_ID', $review ), 'product', true, $comment_original_lang); return 'product-' . $post_id . '-review-' . Obj::prop( 'comment_ID', $review ); // WPML default code // return 'product-' . Obj::prop( 'comment_post_ID', $review ) . '-review-' . Obj::prop( 'comment_ID', $review ); } In my test on the live site I hardcoded FR because I don't know how to get the original comment language and it was okay for testing on a single review. The language used for the product that the user commented on would have to be used dynamically in your function getReviewStringName(). So user commented on FR, then insert FR product id. Independent of the current frontend display language. Feel free to rebuild and test. Please leave feedback. |
February 3, 2023 at 5:28 pm #12957809 | |
cons-2 |
Perhaps the question is why an individual query brings different results than when wp_list_comments() loads comments itself. - For wp_list_comments without get_comments() the comment_post_ID of the results varies It seems like some filter magic is implemented by default. I'll stop spamming and wait for your feedback. |
February 6, 2023 at 7:02 am #12965533 | |
Dražen Supporter
Languages: English (English ) Timezone: Europe/Zagreb (GMT+01:00) |
Hello, thanks. Sharing this with our 2nd tier, since this is a bit out of my knowledge and expertise. Will update you soon. Regards, |
February 7, 2023 at 6:40 am #12973947 | |
Dražen Supporter
Languages: English (English ) Timezone: Europe/Zagreb (GMT+01:00) |
Hello, thanks for your patience. We have checked and could found the problem. Currently, this issue is escalated to our developers. We will use this example to find some other ways (more appropriate) to hook our filters, but we can't give any guarantee that we'll fix it one day. You are obtaining the comments, but they are obtained untranslated. Then passing them to wp_list_comments but because the ID is the current product ID instead of the translation - we cannot find a translation and thus we return the original.The problem is rather complex, because we filter the comment_clauses so we add comments from the translations, but then we make the comment as if it belongs to the current product ID so that the comment is displayed. The workaround here for your to use is: To call wpml_translate_single_string // customization //$limit = 2; $args = array ( 'post_type' => 'product', 'status'=> 'approve', 'post_status' => 'publish', //'comment__not_in' => $exclude_comment_ids, // exclude some comments, e.g. pinned reviews //'number' => $limit, //'offset' => count($exclude_comment_ids), 'orderby' => 'comment_date', 'order' => 'DESC', 'post_id' => $product->get_id(), /*'date_query' => array( 'before' => $date_before, 'inclusive' => false, )*/ ); $comments = get_comments( $args ); // sometimes we need to return html: $comments_fix_order = array_reverse($comments); foreach ($comments_fix_order as $id => $comment){ $test = get_comment($comments_fix_order[$id]->comment_ID); //this is necessary so that we get the product ID to which this comment belong instead of the current product ID $comments_fix_order[$id]->comment_content = apply_filters( 'wpml_translate_single_string', $comments_fix_order[$id]->comment_content, 'wcml-reviews', 'product-'.$test->comment_post_ID. '-review-'. $comments_fix_order[$id]->comment_ID ); } wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'woocommerce_comments' ) ), $comments_fix_order ); } Hope this helps, if you wish I can leave this ticket open and in the status " Escalated to WPML developers team" so if any news in the future I will update here. Regards, |
February 7, 2023 at 11:25 am #12976977 | |
cons-2 |
Hello. Thanks for the feedback and the suggested solution. We applied your fix and everything is working again. It's interesting for me to get a notification if you roll out a fix globally with a plugin update. You can mark the thread as "solved" from my side. Here's a small adjustment I made. foreach ($comments as $id => $comment){ // get the product ID to which this comment belong instead of the current product ID $_o_comment = get_comment($comments[$id]->comment_ID); $_o_product_id = $_o_comment->comment_post_ID; // id the user has submitted to $_translated_content = apply_filters( 'wpml_translate_single_string', $comments[$id]->comment_content, 'wcml-reviews', 'product-'.$_o_product_id. '-review-'. $comments[$id]->comment_ID ); $comments[$id]->is_translated = $_translated_content !== $comments[$id]->comment_content; // fixes wcml open_lang_div: <div lang="" /> $comments[$id]->comment_post_ID = $_o_product_id; // fixes review title translation (another plugin) $comments[$id]->comment_content = $_translated_content; } |