WooCommerce在WC_Structured_Data中覆盖generate_product_data

问题描述 投票:0回答:1

目前我正在研究的一个网站显示了Ex Vat的价格,下面是inc Vat的价格。我有一个问题,即结构化数据显示的是前提价,这在谷歌商户中心显示为需要增值税价格的警告。

我在我的functions.php中添加了一个函数来覆盖它并得到包含税的价格,除了删除创建的旧结构化数据之外,这是有效的。

我的代码如下:

remove_action( 'woocommerce_single_product_summary', array( 'WC_Structured_Data', 'generate_product_data' ), 60 );
add_action( 'woocommerce_single_product_summary', 'dci_generate_product_data', 60 ); 

我最终得到的是两个产品的结构化数据,一个是原始的Vat价格,一个是我添加的inc Vat价格。所以看起来我的删除操作无法正常工作。有没有人知道为什么会这样!?

php woocommerce hook-woocommerce
1个回答
0
投票

我通过改变我的重写函数解决了这个问题:

WC()->structured_data->set_data( apply_filters( 'woocommerce_structured_data_product', $markup, $product ) );

至:

WC()->structured_data->set_data( apply_filters( 'woocommerce_structured_data_product', $markup, $product ), true );

这是替换,而不是在结构化数据对象上调用set_data时添加,而是给我想要的效果。

© www.soinside.com 2019 - 2024. All rights reserved.