Php Woocommerce产品数据导出空数据
我有一个Wordpress Woocommerce构建,我正在将完成的订单导出到json端点。我在functions.php中有一段代码,一旦订单完成就会触发。所有订单数据都正常工作,但无论我做什么,都无法获得产品信息。在我的json数组中,数据始终显示为NULL 我尝试的每个产品数据循环都不会引入产品数据的任何部分,它只是在json数据中返回空白或NULL。我使用的是基本woocommerce模板,没有使用其他插件 我不能使用WP webhooks(我希望可以),因为我需要在导出中添加其他字段并更改字段名称 我使用的代码如下:Php Woocommerce产品数据导出空数据,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我有一个Wordpress Woocommerce构建,我正在将完成的订单导出到json端点。我在functions.php中有一段代码,一旦订单完成就会触发。所有订单数据都正常工作,但无论我做什么,都无法获得产品信息。在我的json数组中,数据始终显示为NULL 我尝试的每个产品数据循环都不会引入产品数据的任何部分,它只是在json数据中返回空白或NULL。我使用的是基本woocommerce模板,没有使用其他插件 我不能使用WP webhooks(我希望可以),因为我需要在导出中添加其他字段
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\u checkout\u order\u processed”钩子。我会尝试一下,谢谢你的帮助,你是个救命恩人。