繁体   English   中英

将 woocommerce 变体 GTIN 添加到结构化数据

[英]Add woocommerce variation GTIN to structured data

如果网店有可变产品,在某些情况下,这些变体具有唯一的 GTIN。 在我们的例子中,它是 EAN。 如何将这些不同的 GTINS 添加到结构化数据中? 我做了一个功能,在“offers”中插入多个“offer”,但是这里无法识别 GTIN。

这是功能:

//add structured data to the markup
function set_structured_data( $markup, $product ) {
        if ($product->is_type('simple')){
    $markup['brand'] = array('@type' => 'Brand', 'name' => get_post_meta( $product->get_id(), '_brand', true ) );
    $markup['gtin8'] =  get_post_meta( $product->get_id(), '_EAN', true );
        }
        if ($product->is_type('variable')){
            
            $available_variations = $product->get_available_variations();
            foreach ($available_variations as $variation) {
                $variation_id = $variation['variation_id'];
                $variproduct = wc_get_product($variation_id);
                $i++;
                $stock = $product->get_stock_status();
                
                $offers[] = array(
                        'type'                  => 'Offer',
                        'price'                 => $variproduct->get_price(),
                        'priceValidUntil'       => $priceuntil,
                        'priceSpecification'    => array( 
                            'price'                     => $variproduct->get_price(),
                            'priceCurrency'             => get_woocommerce_currency(),
                            'valueAddedTaxIncluded'     => 'http://schema.org/True'
                        ),
                        'priceCurrency'         => get_woocommerce_currency(),
                        'availability'          => 'http://schema.org/'.$stock.'',
                        'url'                   => get_the_permalink(),
                        'seller'                => array (
                        'type'                  => 'Organization',
                        'name'                  => 'HeatPerformance®',
                        'url'                   => get_the_permalink(),
              ));
        }
        $markup['offers'] =  $offers;
        $markup['brand'] = array('@type' => 'Brand', 'name' => get_post_meta( $product->get_id(), '_brand', true ) );
    }
    return $markup;
}
add_filter( 'woocommerce_structured_data_product',  'set_structured_data', 99, 2 );

有任何想法吗?

好的,经过几天的困惑,我在这里找到了解决方案的路径: https : //support.google.com/merchants/answer/6386198?hl=en

结果是我更正了woocommerce的标准化方法并使每个变体成为独特的产品。

因此,如果是这样的可变产品,请首先删除标记:

function set_structured_data( $markup, $product ) {
        if ($product->is_type('variable')) {
            $markup = array();
        }
        return $markup;
}
add_filter( 'woocommerce_structured_data_product',  'set_structured_data', 99, 2 );

然后再次构建 ld+json 脚本,然后是所需的方式:

function set_structured_data_variable() {
    global $product;
    if (isset($product)){
        if ($product->is_type('variable') && !empty($product)){
            $available_variations = $product->get_available_variations();
            $date = date("Y-m-d",strtotime(" + 3months"));
            $commenttext = '';
            $commentauthor = '';
            $commentdate = '';
            $comments = get_comments(array( 'post_id' => $product->get_id(), 'number' => '1' ));
    foreach($comments as $comment) {
        $commenttext = $comment->comment_content;
        $commentauthor = $comment->comment_author;
        $commentdate = $comment->comment_date;
    }
            foreach ($available_variations as $variation) {
                $variation_id = $variation['variation_id'];
                $variproduct = wc_get_product($variation_id);
                $i++;
                $gtin = 0000000000001;
                if (!empty(get_post_meta( $variation_id, '_EAN', true ))){
                    $gtin = get_post_meta( $variation_id, '_EAN', true );
                }
                $stock = $product->get_stock_status();
                $arrays[$i] = array(
                '@context'      => 'https://schema.org/',
                '@type'             => 'Product',
                'sku'           => $variproduct->get_sku(),
                'gtin13'        => $gtin,
                'image'         => get_the_post_thumbnail_url($product->get_id()),
                'name'          => implode(" / ", $variproduct->get_variation_attributes()),
                'description'   => wp_strip_all_tags(get_the_excerpt($product->get_id())),
                'brand'         => array('@type' => 'Brand', 'name' => get_post_meta( $product->get_id(), '_brand', true ) ),
                'review'        => array(
                                        '@type' => 'Review', 
                                        'reviewRating' => array ( 
                                            '@type' => 'Rating', 
                                            'ratingValue' => $product->get_review_count(),
                                            'bestRating' => '5',
                                            'worstRating' => '1',
                                            ),
                                        'author' => array(
                                        '@type' => 'person',
                                        'name' => $commentauthor,
                                        'reviewBody' => $commenttext,
                                        'datePublished' => $commentdate,
                                        )),
                'aggregateRating' => array (
                                        '@type' => 'AggregateRating',
                                        'ratingValue'=> $product->get_average_rating(),
                                        'reviewCount' => $product->get_rating_count(),
                                        ),

                'inProductGroupWithID' => $product->get_sku(),
                'offers' => array(
                        '@type'                 => 'Offer',
                        'price'                 => $variproduct->get_price(),
                        'priceValidUntil'       => $date,
                        'priceSpecification'    => array( 
                            'price'                     => $variproduct->get_price(),
                            'priceCurrency'             => get_woocommerce_currency(),
                            'valueAddedTaxIncluded'     => 'http://schema.org/True'
                        ),
                        'priceCurrency'         => get_woocommerce_currency(),
                        'availability'          => 'http://schema.org/'.$stock.'',
                        'url'                   => get_the_permalink(),
                        'seller'                => array (
                        'type'                  => 'Organization',
                        'name'                  => 'HeatPerformance®',
                        'url'                   => get_the_permalink(),
              )));
        }
        echo '<script type="application/ld+json" class="buronoort">[';
        $i = 0;
    foreach ($arrays as $array){
        $i++;
    echo json_encode($array);
    if ($i < array_key_last($arrays)){
    echo ',';
        }
    }
    echo ']</script>';
    }
    }
}
add_action('wp_head','set_structured_data_variable', 19);

在结构化数据的测试工具中进行了测试,并且可以正常工作。 然而,有点担心的是产品评论,我必须研究多次评论后会发生什么。 另一个问题是 EAN,我已将其标准设置为“0000000000001”,因为尚未输入所有 EANS。 所以这是一个辩论解决方案,但如果有人有更好的主意,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM