在 Woocommerce 中用 foreach 合并两个产品星级
Merging two product star ratings with foreach in Woocommerce
可能有更好的方式来陈述这个问题,但我正在用我所知道的来工作。
在 Woocommerce 上,我试图为每颗星 (1-5) 以及两种产品获得 _wc_rating_count;然后我想结合主题;例如,我将两个产品的评论合并在一个页面上的一张图中。
我是这样做的:
$product_id = array( 2146, 2166 );
$ratings = array(5, 4, 3, 2, 1);
foreach ($product_id as $pid) :
$product = wc_get_product($pid);
foreach ($ratings as $rating) :
$rating_count = $product->get_rating_count($rating);
$percentage = ($rating_count / $count) * 100 . "%";
//echo $rating_count . '<br/>';
endforeach;
endforeach;
问题是,我得到一个包含 10 个项目的数组(2 个产品 x 5 个星级(5,4,3,2,1),我需要合并这些值。
11
1
0
0
2
8
0
0
0
1
我需要它的地方
19
1
0
0
3
你知道我如何从嵌入式 foreach 中获取两个数组并根据 $ratings 数组合并它们吗?
感谢您的帮助:)
所以这是你想要实现的目标:
$product_ids = array( 2146, 2166 );
$ratings = array(5, 4, 3, 2, 1);
$rate_countings = array();
foreach ($product_ids as $pid){
$product = wc_get_product($pid);
foreach ($ratings as $rating){
$rating_count = $product->get_rating_count($rating);
//$percentage = ($rating_count / $count) * 100 . "%";
if( !isset($rate_countings[$rating]) ){
$rate_countings[$rating] = 0;
}
$rate_countings[$rating] += $rating_count;
}
}
var_dump($rate_countings);
可能有更好的方式来陈述这个问题,但我正在用我所知道的来工作。
在 Woocommerce 上,我试图为每颗星 (1-5) 以及两种产品获得 _wc_rating_count;然后我想结合主题;例如,我将两个产品的评论合并在一个页面上的一张图中。
我是这样做的:
$product_id = array( 2146, 2166 );
$ratings = array(5, 4, 3, 2, 1);
foreach ($product_id as $pid) :
$product = wc_get_product($pid);
foreach ($ratings as $rating) :
$rating_count = $product->get_rating_count($rating);
$percentage = ($rating_count / $count) * 100 . "%";
//echo $rating_count . '<br/>';
endforeach;
endforeach;
问题是,我得到一个包含 10 个项目的数组(2 个产品 x 5 个星级(5,4,3,2,1),我需要合并这些值。
11
1
0
0
2
8
0
0
0
1
我需要它的地方
19
1
0
0
3
你知道我如何从嵌入式 foreach 中获取两个数组并根据 $ratings 数组合并它们吗?
感谢您的帮助:)
所以这是你想要实现的目标:
$product_ids = array( 2146, 2166 );
$ratings = array(5, 4, 3, 2, 1);
$rate_countings = array();
foreach ($product_ids as $pid){
$product = wc_get_product($pid);
foreach ($ratings as $rating){
$rating_count = $product->get_rating_count($rating);
//$percentage = ($rating_count / $count) * 100 . "%";
if( !isset($rate_countings[$rating]) ){
$rate_countings[$rating] = 0;
}
$rate_countings[$rating] += $rating_count;
}
}
var_dump($rate_countings);