Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在WooCommerce中的产品(Schema.org)的结构化数据中添加ean代码(gtin)_Php_Wordpress_Woocommerce_Schema_Product - Fatal编程技术网

Php 在WooCommerce中的产品(Schema.org)的结构化数据中添加ean代码(gtin)

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'

我使用此代码段在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;
}
这是可行的,但我需要将“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
字段设置为nofalse,您仍然会看到“未提供全局标识符(例如gtin、mpn、isbn)(可选)”警告。你可以忽略它。事实上,这份报告没有报道

您可以在此处进行一些测试:

该代码已经过测试,可以正常工作。将其添加到活动主题的functions.php中

相关答复:


我只有8位数的EAN,我还需要另一个吗?此外,在代码运行时,Google仍然显示错误,没有设置gtin、ean或mpn,即使标识符_已定义为“否”。对不起,我想删除我的第二条注释。我已更新了答案。gtin是一个可选字段。但这是指进口到谷歌商务中心(谷歌购物)的产品。我们在这里讨论的是结构化产品数据(用于谷歌搜索结果)。对于商户中心,如您评论中链接中所述,您必须将
标识符\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;
}