WPML

问题概述

与 WPML 一起使用 YITH WooCommerce Advanced Reviews 插件时,翻译后的产品不会显示其原始产品的任何评价。评价选项卡显示 0 条评价,且没有评分。为产品、分类或标签设置的评价框也不会应用于它们的翻译。

临时解决方法

将此代码添加到子主题的 functions.php 文件中,或添加到小型插件中:

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 ) );
} );

然后,翻译后的产品将显示该产品所有语言版本的评论和评分。相同的代码可使为产品、分类或标签设置的评论框应用于其翻译。

所有已知问题

需要有关此问题的帮助?

提出支持问题,我们将与您一起调查。