【发布时间】:2021-05-17 06:42:34
【问题描述】:
我有一个 Wordpress Woocommerce 版本,我正在将完成的订单导出到一个 json 端点。我在functions.php中有代码,一旦订单完成就会触发。所有订单数据都在工作,但无论我做什么,我都无法获得产品信息。数据在我的 json 数组中始终显示为 NULL。
我尝试的每个产品数据循环都不会引入产品数据的任何部分,它只是在 json 数据中返回空白或 NULL。我正在使用基本的 woocommerce 模板,并且没有使用其他插件。
我无法使用 WP webhook(我希望可以),因为我需要添加其他字段并更改导出中的字段名称。
我使用的代码如下:
add_action( 'woocommerce_new_order', 'send_sample');
function send_sample($order_id){
$order = wc_get_order($order_id);
$order_number= $order->get_order_number();
$payment_method = $order->get_payment_method();
$firstname = $order->get_billing_first_name();
$LastName = $order->get_billing_last_name();
$OrderDate= $order->get_date_created();
$ordertotal=$order->get_total();
$phone = $order->get_billing_phone();
$address1 = $order->get_billing_address_1();
$address2 = $order->get_billing_address_2();
$city = $order->get_billing_city();
$products = array ();
foreach ($order->get_items() as $item_id => $item ) {
// Get an instance of corresponding the WC_Product object
$product = $item->get_product();
$active_price = $product->get_price(); // The product active raw price
$regular_price = $product->get_sale_price(); // The product raw sale price
$sale_price = $product->get_regular_price(); // The product raw regular price
$product_name = $item->get_name(); // Get the item name (product name)
$item_quantity = $item->get_quantity(); // Get the item quantity
$item_subtotal = $item->get_subtotal(); // Get the item line total non discounted
$item_subto_tax = $item->get_subtotal_tax(); // Get the item line total tax non discounted
$item_total = $item->get_total(); // Get the item line total discounted
$item_total_tax = $item->get_total_tax(); // Get the item line total tax discounted
$item_taxes = $item->get_taxes(); // Get the item taxes array
$item_tax_class = $item->get_tax_class(); // Get the item tax class
$item_tax_status= $item->get_tax_status(); // Get the item tax status
$item_downloads = $item->get_item_downloads(); // Get the item downloads
// Displaying this data (to check)
// echo 'Product name: '.$product_name.' | Quantity: '.$item_quantity.' | Item total: '. number_format( $item_total, 2 );
array_push($products,$product_name);
}
$bouhajir= array (
'password' => '1234',
'email' => 'ms@bt.com',
'fielddata' =>
array (
0 =>
array (
'hub' => 'DCLC',
'awbNo' => "'$order_number'",
'merchantCode' => 'xyz',
'merchantName' => 'xyz name',
'date' => "$OrderDate",
'jobType' => 'FWD',
'jobData' =>
array (
'city' => "$city",
'contact_number_change_test' => "$phone",
'name' => "$firstname"." "."$LastName",
'weight' => 0.8,
'slot' => '',
'collectables_amount' => "$ordertotal",
'pincode' => '',
'payment_choice' => "$payment_method",
'product_image' => '',
'address' => "$address1"." "."$address2",
'order_number' => "'$order_number'",
// 'contents' => "$item_name"." "."$quantity",
'packet_count' => 1,
'item_description' => 'this is just a dummy description',
'remarks' => 'handle with care',
'dl_no' => 'dddfdas',
),
'testdata' =>
array (
$products ),
),
),
);
$datasend= json_encode ($bouhajir);
###send code ommitted###
【问题讨论】:
-
使用这个钩子代替 woocommerce_order_status_completed
-
感谢您的回复,我尝试更改挂钩但仍然没有收到数据。你有什么其他的建议我可以试试。
-
抱歉,它正在工作。我刚刚注意到我的订单已设置为处理中,因此当我更换挂钩时挂钩没有触发。结帐时订单完成时是否会触发挂钩。
-
您可以使用“woocommerce_checkout_order_processed”钩子。
-
我会试一试,谢谢你的帮助,你是救命稻草。
标签: php wordpress woocommerce