在 Woocommerce 3 中获取并传递订单商品和订单详细信息

men*_*eno 1 php wordpress orders woocommerce hook-woocommerce

我们需要传递以下 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;
 }
Run Code Online (Sandbox Code Playgroud)

但它并没有给我们带来我们应该需要的预期结果。

任何帮助表示赞赏。

Loi*_*tec 5

现在要获取丢失的订单“行项目”详细信息订单详细信息,以下是正确的方法:

\n\n
add_action( \'woocommerce_thankyou\', \'the_tracking\' );\nfunction the_tracking( $order_id ) {\n    $count    = 0;\n    $order    = wc_get_order( $order_id );\n    // Starting tracking line\n    $tracking = \'OrderID=\'. $order_id;\n\n    foreach( $order->get_items() as $item_id => $item ){\n        // Get an instance of the WC_Product object\n        $product = $item->get_product();\n\n        $product_sku       = $product->get_sku(); // Item product SKU\n        $item_qty          = $item->get_quantity(); // Item Quantity\n        $item_subtotal_tax = $item->get_subtotal_tax(); // Item subtotal tax\n        $item_subtotal     = $item->get_subtotal(); // Item subtotal\n        $item_total_tax    = $item->get_total_tax(); // Item Total tax discounted\n        $item_total        = $item->get_total(); // Item Total discounted\n\n        // Tracking line for each item (continuation)\n        $tracking .= "&ITEM{$count}={$product_sku}&AMT{$count}={$item_total}&QTY{$count}={$item_qty}";\n\n        $count++; // increment the item count\n    }\n    // Tracking line (continuation) ====> ??? CID, OID and TYPE\n    $tracking .= "&CID=1529328&OID=[OID]&TYPE=385769";\n\n    // An order can have no used coupons or also many used coupons\n    $coupons  = $order->get_used_coupons();\n    $coupons  = count($coupons) > 0 ? implode(\',\', $coupons) : \'\';\n\n    $discount = $order->get_total_discount();\n    $currency = $order->get_currency();\n    $subtotal = $order->get_subtotal();\n    $total    = $order->get_total();\n\n    // Tracking line (end)\n    $tracking .= "&AMOUNT={$total}&DISCOUNT={$discount}&CURRENCY={$currency}&COUPON={$coupons}";\n\n    echo $tracking;\n }\n
Run Code Online (Sandbox Code Playgroud)\n\n

代码位于活动子主题(或活动主题)的 function.php 文件中。\n经过测试并有效。

\n\n
\n

您必须在跟踪文档CID中查看:和\xe2\x80\xa6OID的用途TYPE

\n
\n

  • 成功了@LoicTheAztec,你超级有才华,谢谢 (2认同)