Php 在WooCommerce中的产品(Schema.org)的结构化数据中添加ean代码(gtin)
我使用此代码段在Woocommerce的产品架构中显示gtin的ean值:Php 在WooCommerce中的产品(Schema.org)的结构化数据中添加ean代码(gtin),php,wordpress,woocommerce,schema,product,Php,Wordpress,Woocommerce,Schema,Product,我使用此代码段在Woocommerce的产品架构中显示gtin的ean值: add_filter( 'woocommerce_structured_data_product', 'filter_woocommerce_structured_data_product', 10, 2 ); function filter_woocommerce_structured_data_product( $markup, $product ) { if ( empty( $markup[ 'gtin8'
add_filter( 'woocommerce_structured_data_product', 'filter_woocommerce_structured_data_product', 10, 2 );
function filter_woocommerce_structured_data_product( $markup, $product ) {
if ( empty( $markup[ 'gtin8' ] ) ) {
$markup[ 'gtin8' ] = get_post_meta( $product->get_id(), 'ean', true );
}
return $markup;
}
这是可行的,但我需要将“identifier_exists”标记设置为未设置自定义字段的产品。如何修改代码段以在标记中显示ean值(如果存在),并向没有ean的产品添加标识符\u exists attribute=false?请尝试以下操作:
add_filter( 'woocommerce_structured_data_product', 'custom_schema', 99, 2 );
function custom_schema( $markup, $product ) {
$value = $product->get_meta( 'ean' );
$length = strlen($value);
if ( ! empty($value) ) {
$markup['identifier_exists'] = true;
$markup['gtin'.$length] = $value;
} else {
$markup['identifier_exists'] = false;
}
return $markup;
}
代码进入活动子主题(或活动主题)的functions.php文件。必须根据其长度设置
gtin
字段。您将找到包含所有可用字段的完整文档
gtin
必须为文本类型(非数字)
最后,gtin
字段是可选的如果您的产品没有EAN代码(或任何其他标识符),则无法设置gtin
即使将identifier\u exists
字段设置为no或false,您仍然会看到“未提供全局标识符(例如gtin、mpn、isbn)(可选)”警告。你可以忽略它。事实上,这份报告没有报道
您可以在此处进行一些测试:
该代码已经过测试,可以正常工作。将其添加到活动主题的functions.php中
相关答复:
标识符\u exists
字段设置为no
,将gtin
字段设置为空。结构化数据的真实性是。您的代码与上面的代码完全相同。不幸的是,谷歌仍然给出错误,不接受商户中心的“标识符存在”。
// set the gtin in the structured data of the product
add_filter( 'woocommerce_structured_data_product', 'custom_schema', 99, 2 );
function custom_schema( $markup, $product ) {
// get the product identifier
$ean = get_post_meta( $product->get_id(), 'ean', true );
// set gtin based on length
switch ( strlen($ean) ) {
case 8:
$markup['gtin8'] = (string)$ean;
break;
case 12:
$markup['gtin12'] = (string)$ean;
break;
case 13:
$markup['gtin13'] = (string)$ean;
break;
case 14:
$markup['gtin14'] = (string)$ean;
break;
default:
$markup['identifier_exists'] = 'no';
break;
}
return $markup;
}