给Google"喂"数据(上)

Woocommerce 开发小组比较倔强,明明谷歌已经提示要改善数据了,但是默认的Woo还是不改;这次为了能达到谷歌的要求,不得不对源码下手了,虽然知道改源码的风险很大,但是还是要改,涉及到推广的事情就必须要去做,尽管可以通过hook来实现,但是本人认为改动太大了。下面是我就Missing field "aggregateRating"和Missing field "review"两项做的改动;

1、class-wc-structured-data.php修改

找到文件中下面的位置

1
2
3
4
5
6
7
8
……
$markup['offers'] = array( apply_filters( 'woocommerce_structured_data_product_offer', $markup_offer, $product ) );
}

if ( $product->get_rating_count() && wc_review_ratings_enabled() ) {
$markup['aggregateRating'] = array(
'@type' => 'AggregateRating',
……

修改为

1
2
3
4
5
6
7
8
……
$markup['offers'] = array( apply_filters( 'woocommerce_structured_data_product_offer', $markup_offer, $product ) );
}

if ( !($product->get_rating_count() && wc_review_ratings_enabled()) ) {
$markup['aggregateRating'] = array(
'@type' => 'AggregateRating',
……

从程序上可以看出来,要是rating_count值为0并且关闭了review_ratings,数据是不会显示,修改后强行让程序给谷歌”喂”它想要的数值。
操作完上面的可以打开aggregateRating相应的数据了,但是到谷歌里面去看提示数据不对,因为谷歌要求ratingValue大于0小于等于5的。
好!做假做全套,接下来咱们给谷歌喂想要的假数据!

2、在functions.php或者插件中加入下面的程序段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//更新、保存产品信息时写入到数据库中
add_action( 'woocommerce_new_product', 'on_product_save', 10, 1 );
add_action( 'woocommerce_update_product', 'on_product_save', 10, 1 );
function on_product_save( $product_id ) {
$product = wc_get_product( $product_id );
// do something with this product
$ratingvalue = get_post_meta(get_the_ID(), 'ratingvalue', true);
$reviewcount = get_post_meta(get_the_ID(), 'reviewcount', true);
if ( $ratingvalue ) {
$ratingvalue = 4.6;
}
if ( $reviewcount ) {
$reviewcount = 38;
}
update_post_meta( $product->get_id(), '_wc_average_rating', $ratingvalue );
update_post_meta( $product->get_id(), '_wc_review_count', $reviewcount );
}

那么ratingvalue和reviewCount的数值从哪里来?请看上期的文章[Woocommerce 自定义SEO]

Author

MelonCloud

Posted on

2024-11-05

Updated on

2024-11-07

Licensed under

Comments