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

时间:2018-03-06 04:42:14

标签: php wordpress woocommerce hook-woocommerce orders

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

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

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

这是我们的代码:

wbemtest

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

感谢任何帮助。

1 个答案:

答案 0 :(得分: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 ...

相关问题