在 Woocommerce 3 中获取并传递订单商品和订单详细信息
Get and Pass Order items and Order details in Woocommerce 3
我们需要传递以下 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;
}
但它并没有给我们应有的预期结果。
感谢任何帮助。
现在找回你的失物 and ,正确的方法是:
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 文件。
已测试并有效。
You will have to see in the tracking documentation, what are for: CID
, OID
and TYPE
…
我们需要传递以下 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;
}
但它并没有给我们应有的预期结果。
感谢任何帮助。
现在找回你的失物
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 文件。 已测试并有效。
You will have to see in the tracking documentation, what are for:
CID
,OID
andTYPE
…