在 WooCommerce 中的产品 (Schema.org) 的结构化数据中添加 ean 代码 (gtin)
Add the ean code (gtin) in the structured data of the product (Schema.org) in WooCommerce
我正在使用此代码段在 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' ] ) ) {
$markup[ 'gtin8' ] = get_post_meta( $product->get_id(), 'ean', true );
}
return $markup;
}
这可行,但我需要为未设置自定义字段 ean 的产品设置“identifier_exists”标记。我如何修改我的代码片段以在标记中显示 ean 值(如果存在),并将 identifier_exists 属性 = false 添加到没有 ean 的产品?
尝试以下操作:
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
字段 必须根据其长度设置 。
Here 您将找到包含所有可用字段的完整文档.
The gtin
must be of type text (not numeric).
最后,gtin
字段是可选的。 如果您的产品没有 EAN 代码 (或任何其他标识符) 您可以简单地不设置 gtin
.
即使您将 identifier_exists
字段设置为 no 或 false,您仍然会看到 “未提供全局标识符(例如 gtin、mpn、isbn)(可选)” 警告。你可以忽略它。其实在documentation.
里面是没有报道的
你可以在这里做一些测试:https://search.google.com/test/rich-results
// 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;
}
代码已经过测试并且可以工作。将它添加到您的活动主题 functions.php.
相关回答:
我正在使用此代码段在 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' ] ) ) {
$markup[ 'gtin8' ] = get_post_meta( $product->get_id(), 'ean', true );
}
return $markup;
}
这可行,但我需要为未设置自定义字段 ean 的产品设置“identifier_exists”标记。我如何修改我的代码片段以在标记中显示 ean 值(如果存在),并将 identifier_exists 属性 = false 添加到没有 ean 的产品?
尝试以下操作:
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
字段 必须根据其长度设置 。
Here 您将找到包含所有可用字段的完整文档.
The
gtin
must be of type text (not numeric).
最后,gtin
字段是可选的。 如果您的产品没有 EAN 代码 (或任何其他标识符) 您可以简单地不设置 gtin
.
即使您将 identifier_exists
字段设置为 no 或 false,您仍然会看到 “未提供全局标识符(例如 gtin、mpn、isbn)(可选)” 警告。你可以忽略它。其实在documentation.
你可以在这里做一些测试:https://search.google.com/test/rich-results
// 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;
}
代码已经过测试并且可以工作。将它添加到您的活动主题 functions.php.
相关回答: