我正在建设一个电子商务网站,我已经安装了以下插件(商务客户评论- https://wordpress.org/plugins/customer-reviews-woocommerce/)的审查和评级的订单后,用户完成了订单流程。
然而,我们处理的产品的性质(如面料、连衣裙、纱丽等)将会脱销,同样的产品将无法再次采购。因此,我想使用产品的‘标签’来显示旧订单的评论和评级(出于这个原因,我希望在订单行项目中有评论)。此外,新产品页面应该使用自己的标签从具有相同标签的旧订单中获取评论和评级。
在这个问题上,任何指导都是有帮助的!
发布于 2021-10-16 21:57:20
要解决这个问题,首先要做的是将与给定产品关联的所有标记放入一个数组中。然后,需要根据第一步生成的产品ids数组查询WP_Comments_Query。
下面是上述方法的一个片段。
function get_reviews_by_tags(){
global $product;
$productid = $product->get_id();
// get all product_tags of the current product in an array
$current_tags = get_the_terms( $productid, 'product_tag' );
//only start if we have some tags
if ( $current_tags && ! is_wp_error( $current_tags ) ) {
//get all related product ids mapped by tags array we created earlier
$relatedproductids_by_tags = get_posts( array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_tag',
'field' => 'term_id',
'terms' => $current_tags,
'operator' => 'IN'
)
),
));
// create a wp comment query object as wc uses comments for reviews
$reviews_args = array(
'post__in' => $relatedproductids_by_tags
);
$reviews_query = new WP_Comment_Query;
$reviews = $reviews_query->query( $reviews_args );
if ( !empty( $reviews ) ) {
foreach ( $reviews as $review ) {
echo '<p>' . $review->comment_content . '</p>';
}
} else {
echo 'No reviews found.';
}
}
add_action( 'woocommerce_after_single_product_summary', 'get_reviews_by_tags', 10, 2 );
}上面的代码没有考虑到你在问题中提到的插件所做的任何修改。另外,请注意,此代码用于获取和显示您的问题中提到的评论。这不是用来创建评论的。
https://stackoverflow.com/questions/69597897
复制相似问题