WPML

Overview of the Issue

When using the YITH WooCommerce Advanced Reviews plugin with WPML, a translated product shows none of the reviews of its original product. The reviews tab shows 0 reviews and no rating. Review boxes set for a product, a category or a tag also do not apply to their translations.

Workaround

Add this code to your child theme’s functions.php file, or in a small plugin:

PHP
add_action( 'pre_get_posts', function ( $q ) {
    if ( ! array_intersect( (array) $q->get( 'post_type' ), [ 'ywar_reviews', 'ywar_review_boxes' ] ) ) { return; }
    $mq = $q->get( 'meta_query' ); if ( ! is_array( $mq ) ) { return; }
    $def = apply_filters( 'wpml_default_language', null );
    $walk = function ( $mq ) use ( &$walk, $def ) {
        foreach ( $mq as $i => $c ) {
            if ( ! is_array( $c ) ) { continue; }
            if ( ! isset( $c['key'] ) ) { $mq[ $i ] = $walk( $c ); continue; }
            if ( '_ywar_product_id' === $c['key'] && isset( $c['value'] ) && is_numeric( $c['value'] ) ) {
                $trid = apply_filters( 'wpml_element_trid', null, (int) $c['value'], 'post_product' );
                $ids = $trid ? array_values( array_map( 'intval', wp_list_pluck( (array) apply_filters( 'wpml_get_element_translations', null, $trid, 'post_product' ), 'element_id' ) ) ) : [];
                if ( $ids ) { $mq[ $i ]['value'] = $ids; $mq[ $i ]['compare'] = 'IN'; }
            } elseif ( in_array( $c['key'], [ 'ywar_review_box_product_ids', 'ywar_review_box_category_ids', 'ywar_review_box_tag_ids' ], true ) && preg_match( '/^:"(d+)";$/', (string) $c['value'], $m ) ) {
                $type = 'ywar_review_box_product_ids' === $c['key'] ? 'product' : ( 'ywar_review_box_category_ids' === $c['key'] ? 'product_cat' : 'product_tag' );
                $mq[ $i ]['value'] = ':"' . (int) apply_filters( 'wpml_object_id', (int) $m[1], $type, true, $def ) . '";';
            }
        }
        return $mq;
    };
    $q->set( 'meta_query', $walk( $mq ) );
} );

Translated products then show the reviews and the rating of every language version of the product. The same code makes review boxes set for a product, a category or a tag apply to their translations.

All known issues

Need Help with This Issue?

Ask a support question and we’ll look into it with you.