1

We need pass these following WooCommerce parameters but we can't and have got errors.

  • Unique Order ID
  • Product SKUs purchased
  • Quantity of each product purchased
  • Price of each product purchased
  • Currency of the transaction
  • Discount Amount (whole-order and item-specific)
  • Coupon Code used

the main issues are getting all products Quantities, Quantity of each product purchased, Product SKU

Here is our code:

add_action( 'woocommerce_thankyou', 'the_tracking' );

function the_tracking( $order_id ) {

    global  $woocommerce;

    $order = wc_get_order( $order_id );
    $total = $order->get_total();
    $currency = get_woocommerce_currency();
    $subtotal = $woocommerce->cart->subtotal;
    $coupons = $order->get_used_coupons();
    $coupon_code = '';
    $discount = $order->get_total_discount();
    foreach ($coupons as $coupon){
        $coupon_code = $coupon;
    }
    $tracking = 'OrderID='.$order.'&ITEMx=[ItemSku]&AMTx=[AmountofItem]&QTYx=[Quantity]&CID=1529328&OID=[OID]&TYPE=385769&AMOUNT='. $total .'&DISCOUNT='. $discount .'&CURRENCY='. $currency .'&COUPON='. $coupon_code .'';
    echo $tracking;
 }

But it doesn't give us the expected results as we should need.

Any help is appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
meno
  • 142
  • 3
  • 17

1 Answers1

5

Now to get your missing order "line item" details and order details, here is the correct way to do it:

add_action( 'woocommerce_thankyou', 'the_tracking' );
function the_tracking( $order_id ) {
    $count    = 0;
    $order    = wc_get_order( $order_id );
    // Starting tracking line
    $tracking = 'OrderID='. $order_id;

    foreach( $order->get_items() as $item_id => $item ){
        // Get an instance of the WC_Product object
        $product = $item->get_product();

        $product_sku       = $product->get_sku(); // Item product SKU
        $item_qty          = $item->get_quantity(); // Item Quantity
        $item_subtotal_tax = $item->get_subtotal_tax(); // Item subtotal tax
        $item_subtotal     = $item->get_subtotal(); // Item subtotal
        $item_total_tax    = $item->get_total_tax(); // Item Total tax discounted
        $item_total        = $item->get_total(); // Item Total discounted

        // Tracking line for each item (continuation)
        $tracking .= "&ITEM{$count}={$product_sku}&AMT{$count}={$item_total}&QTY{$count}={$item_qty}";

        $count++; // increment the item count
    }
    // Tracking line (continuation) ====> ??? CID, OID and TYPE
    $tracking .= "&CID=1529328&OID=[OID]&TYPE=385769";

    // An order can have no used coupons or also many used coupons
    $coupons  = $order->get_used_coupons();
    $coupons  = count($coupons) > 0 ? implode(',', $coupons) : '';

    $discount = $order->get_total_discount();
    $currency = $order->get_currency();
    $subtotal = $order->get_subtotal();
    $total    = $order->get_total();

    // Tracking line (end)
    $tracking .= "&AMOUNT={$total}&DISCOUNT={$discount}&CURRENCY={$currency}&COUPON={$coupons}";

    echo $tracking;
 }

Code goes in function.php file of your active child theme (or active theme). Tested and works.

You will have to see in the tracking documentation, what are for: CID, OID and TYPE

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399