Есть ли способ сделать это через базу данных? Могу ли я унаследовать отзывы, каким -то образом запрашивая их из базы данных, даже если продукты больше не находятся на панели WordPress? Я уже проконсультировался с документацией для этого плагина, но я ничего об этом не нашел.
Код: Выделить всё
add_filter( 'woocommerce_product_get_rating_counts', 'fusionar_valoraciones_heredadas', 10, 2 );
add_filter( 'woocommerce_product_get_review_count', 'fusionar_valoraciones_heredadas', 10, 2 );
add_filter( 'woocommerce_product_get_average_rating', 'fusionar_valoraciones_heredadas', 10, 2 );
function fusionar_valoraciones_heredadas( $value, $product ) {
// Tabla de asignación: 'ID del producto nuevo' => ['IDs de los productos antiguos']
$productos_a_fusionar = [
24359 => [326, 327], // Nuevo producto con ID 23736 hereda de los productos 326 y 327
24224 => [559, 972],
24192 => [666, 667]
];
// Comprobamos si el ID del producto actual está en nuestra tabla de asignación
if ( array_key_exists( $product->get_id(), $productos_a_fusionar ) ) {
// Si es así, obtenemos los IDs antiguos para este producto
$ids_antiguos = $productos_a_fusionar[ $product->get_id() ];
$rating_count = [];
$review_count = 0;
foreach ( $ids_antiguos as $old_id ) {
$old_product = wc_get_product( $old_id );
if ( $old_product ) {
foreach ( $old_product->get_rating_counts() as $rating => $count ) {
if ( ! isset( $rating_count[ $rating ] ) ) {
$rating_count[ $rating ] = 0;
}
$rating_count[ $rating ] += $count;
}
$review_count += $old_product->get_review_count();
}
}
if ( current_filter() == 'woocommerce_product_get_rating_counts' ) {
return $rating_count;
}
if ( current_filter() == 'woocommerce_product_get_review_count' ) {
return $review_count;
}
if ( current_filter() == 'woocommerce_product_get_average_rating' ) {
$total = 0; $count = 0;
foreach ( $rating_count as $rating => $num ) {
$total += $rating * $num;
$count += $num;
}
return $count ? round( $total / $count, 2 ) : 0;
}
}
return $value;
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... e-products
Мобильная версия