在Woocommerce 3中获取并传递订单商品和订单详情

问题描述 投票:1回答:1

我们需要传递以下WooCommerce参数,但我们不能并且有错误。

  • 唯一订单ID
  • 购买产品SKU
  • 购买的每种产品的数量
  • 购买的每种产品的价格
  • 交易货币
  • 折扣金额(整个订单和特定项目)
  • 使用的优惠码

主要问题是获得所有产品的数量,购买的每种产品的数量,产品SKU

这是我们的代码:

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;
 }

但它并没有像我们需要的那样给我们预期的结果。

任何帮助表示赞赏。

php wordpress woocommerce hook-woocommerce orders
1个回答
1
投票

现在,为了得到你失踪的order "line item" detailsorder details,这是正确的方法:

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;
 }

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

你必须在跟踪文档中看到的是:CIDOIDTYPE ...

© www.soinside.com 2019 - 2024. All rights reserved.