1

Im trying to apply product review for my website with google merchants optin review code.

I succeeded doing the part of the country, date, id, and email..

Now I'm not succeeding get the EAN or GTIN numbers from the code to apply to the product reviews...

can you please help ?

Here is the code.. is already working al the above described, it only miss the connection to the gtin inside the woocommerce for each product...

I basically d'ont know how to get the gtin .

An example url with two products:

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');

Example page: https://gardentoy.com.br/finalizar-compra/order-received/2943/?key=wc_order_oOsii3Cuy6HWI

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

1

First you need to check in wp_postmeta table for the GTIN meta key that is used for a product (any product ID).

I have revisited your code as there was some mistakes since WooCommerce 3… Try the following:

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
}

It should work.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks so much for trying to help it seems it may be just like that but is not working yet take a look if you can see the error in this link please https://gardentoy.com.br/finalizar-compra/order-received/2945/?key=wc_order_xzJ7WnWYrjz5D i think the meta key is EAN not sure – Fabio Tortorella Mar 10 '21 at 21:52
  • @FabioTortorella Remember that I can't access the "order received" page from someone else… My code works, the only thing that you need is to get the correct meta key for the product EAN (as explained). – LoicTheAztec Mar 10 '21 at 22:44
  • thanks so much i found the meta key it was _wpm_gtin_code i just changed this and it worked perfectly... thanks so much... hope it helps other people having this dificulty... i only found half answers and finaly this one is final! Big Hug from Brazil my friend! – Fabio Tortorella Mar 11 '21 at 12:21