1

我正在尝试使用谷歌商家选择评论代码为我的网站应用产品评论。

我成功地完成了国家、日期、身份证和电子邮件的部分。

现在我没有成功从代码中获取 EAN 或 GTIN 号码以应用于产品评论......

你能帮忙吗?

这是代码..已经在上面描述的工作,它只是错过了每个产品的woocommerce内部gtin的连接......

我基本上不知道如何获得gtin

包含两个产品的示例网址:

function wh_CustomReadOrder($order_id) {
    //getting order object
    $order = wc_get_order($order_id);
    $email = $order->billing_email;
    $date_created = $order->get_date_created(); 
    $days = 7; // Add days 
    $estimated_delivery_date = date_i18n( 'Y-m-d', strtotime( $date_created ) + ( $days * 24 * 60 * 60 ) );
    $shipping_country = $order->get_shipping_country();
    $GTIN1 = $order->product_gtin;
    //$GTIN2 = $order->item_meta_lable;
    ?>
    <script src="https://apis.google.com/js/platform.js?onload=renderOptIn" async defer></script>
    <script>
        window.renderOptIn = function () {
            window.gapi.load('surveyoptin', function () {
                window.gapi.surveyoptin.render(
                        {
                            "merchant_id": 296683478,
                            "order_id": "<?php echo $order_id; ?>",
                            "email": "<?php echo $email; ?>",
                            "delivery_country": "<?php echo $shipping_country; ?>",
                            "estimated_delivery_date": "<?php echo $estimated_delivery_date; ?>",
                            "products": [{"gtin":"<?php echo $GTIN1; ?>"}, {"gtin":"<?php echo $GTIN2; ?>"}]
                           
                        }
                );
            });
        };
    </script>
    <?php
}
add_action('woocommerce_thankyou', 'wh_CustomReadOrder');

示例页面:https ://gardentoy.com.br/finalizar-compra/order-received/2943/?key=wc_order_oOsii3Cuy6HWI

4

1 回答 1

0

首先,您需要在 wp_postmeta 表中检查用于产品(任何产品 ID)的 GTIN 元密钥。

自 WooCommerce 3 以来出现了一些错误,我重新访问了您的代码……尝试以下操作:

add_action('woocommerce_thankyou', 'wh_custom_read_order');
function wh_custom_read_order($order_id) {
    $order = wc_get_order( $order_id ); // Get the order object
    $days  = 7; // Add days

    $billing_email = $order->get_billing_email();
    $date_created  = $order->get_date_created();
    $estimated_delivery_date = date_i18n( 'Y-m-d', $date_created->getTimestamp() + ( $days * 24 * 60 * 60 ) );
    $shipping_country = $order->get_shipping_country();

    $gtin_data = array(); // Initializing

    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        $product = $item->get_product();
        $gtin    = $product->get_meta('_wpm_gtin_code'); // Check that '_wpm_gtin_code' is the corect product meta key to get the GTIN

        $gtin_data[] = '{"gtin":"'.$gtin.'"}';
    }
    ?>
    <script src="https://apis.google.com/js/platform.js?onload=renderOptIn" async defer></script>
    <script>
        window.renderOptIn = function () {
            window.gapi.load('surveyoptin', function () {
                window.gapi.surveyoptin.render({
                    "merchant_id": 296683478,
                    "order_id": "<?php echo $order_id; ?>",
                    "email": "<?php echo $billing_email; ?>",
                    "delivery_country": "<?php echo $shipping_country; ?>",
                    "estimated_delivery_date": "<?php echo $estimated_delivery_date; ?>",
                    "products": [<?php echo implode( ', ', $gtin_data ); ?>]
                });
            });
        };
    </script>
    <?php
}

它应该工作。

于 2021-03-10T21:06:56.797 回答