在 WooCommerce 中产品(Schema.org)的结构化数据中添加 ean 代码(gtin) [英] Add the ean code (gtin) in the structured data of the product (Schema.org) in WooCommerce

查看:97
本文介绍了在 WooCommerce 中产品(Schema.org)的结构化数据中添加 ean 代码(gtin)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码段来显示 Woocommerce 产品架构中 gtin 的 ean 值:

I am using this snippet to display ean value for gtin in Woocommerce's product schema:

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 值(如果存在),并将 identifier_exists 属性 = false 添加到没有 ean 的产品?

This works but I need to set "identifier_exists" markup to products that don't have the custom field ean set. How can I modify my snippet to show the ean value in the markup if it exists, and add the identifier_exists attribute = false to products that don't have an 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 文件中.

Code goes in functions.php file of the active child theme (or active theme).

这篇关于在 WooCommerce 中产品(Schema.org)的结构化数据中添加 ean 代码(gtin)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆