Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
630 views
in Technique[技术] by (71.8m points)

javascript - Order items in a JS tracking code on Order received page in Woocommerce

I am trying to integrate a tracking code in Woocommerce Thankyou page. I found just the order id to fill it out. But I don't know how to complete this for order items data.

Here is my actual code:

<script type="text/javascript">
ADMITAD = window.ADMITAD || {};

ADMITAD.Invoice = ADMITAD.Invoice || {};

ADMITAD.Invoice.broker = "adm";     // deduplication parameter (for Admitad by default)

ADMITAD.Invoice.category = "1";     // action code (defined during integration)


var orderedItem = [];               // temporary array for product items


// repeat for every product item in the cart

orderedItem.push({

  Product: {

  productID: 'product_id', // internal product ID (not more than 100 characters, the same as in your product feed)

  category: '1',               // tariff code (defined during integration)

  price: 'price',          // product price

  priceCurrency: "RON",        // currency code in the ISO-4217 alfa-3 format

 },

 orderQuantity: '{{quantity}}',   // product quantity

 additionalType: "sale"           // always sale

 });


ADMITAD.Invoice.referencesOrder = ADMITAD.Invoice.referencesOrder || [];
// adding items to the order

ADMITAD.Invoice.referencesOrder.push({

  orderNumber: "<?php echo $order->get_id(); ?>;", // internal order ID (not more than 100 characters)

  orderedItem: orderedItem

});

// Important! If order data is loaded via AJAX, uncomment this string. 

// ADMITAD.Tracking.processPositions();

</script>

Any help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The following revisited code adds the correct order items loop and uses "Order received" page (Thankyou) dedicated action hook:

add_action( 'woocommerce_thankyou', 'js_tracking_thank_you_page', 90, 1 );
function js_tracking_thank_you_page( $order_id ) {
    // Get the WC_Order instance Object
    $order = wc_get_order( $order_id );

    // Output
    ?>
    <script type="text/javascript">
    ADMITAD = window.ADMITAD || {};

    ADMITAD.Invoice = ADMITAD.Invoice || {};

    // deduplication parameter (for Admitad by default)
    ADMITAD.Invoice.broker = "adm";

    // action code (defined during integration)
    ADMITAD.Invoice.category = "1";

    // temporary array for product items
    var orderedItem = [];

    <?php
    // Loop through Order items
    foreach( $order->get_items() as $item ) :
        $product = $item->get_product();
    ?>
    orderedItem.push({

      Product: {
        // internal product ID (not more than 100 characters, the same as in your product feed)
        productID: '<?php echo $item->get_product_id(); ?>',

        // tariff code (defined during integration)
        category: '1',

        // product price
        price: '<?php echo $product->get_price(); ?>',

        // currency code in the ISO-4217 alfa-3 format
        priceCurrency: '<?php echo $order->get_currency(); ?>',
      },
      // product quantity
      orderQuantity: '<?php echo $item->get_quantity(); ?>',

      additionalType: "sale" // always sale

    });
    <?php endforeach; // End of Loop ?>

    // adding items to the order
    ADMITAD.Invoice.referencesOrder = ADMITAD.Invoice.referencesOrder || [];

    ADMITAD.Invoice.referencesOrder.push({
      // internal order ID (not more than 100 characters)
      orderNumber: "<?php echo $order->get_id(); ?>;",

      orderedItem: orderedItem

    });

    // Important! If order data is loaded via AJAX, uncomment this string.
    // ADMITAD.Tracking.processPositions();
    </script>
    <?php
}

It should works (Tested).

Related:


Addition - Live currency conversion

1) Install and active this free plugin: Euro FxRef Currency Converter

2) Enable auto currency conversion from 'RON' to 'EUR' (product price example).

replace:

// product price
price: '<?php echo $product->get_price(); ?>',

by the following:

// Converted product price (rounded with 2 decimals)
<?php $price = EuroFxRef::convert( $product->get_price(), 'RON', 'EUR' ); ?>
price: '<?php echo round( $price, 2 );  ?>',

Tested and works… This should do the trick.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...