我正在通过API从Wordpress(Woocommerce)商店向运输公司发送订单详细信息。 我能够在每次下单后成功发送它,并且我应该得到包含一些详细信息(id,条形码,barcodeText)的回复。现在,我想将这些详细信息存储回woocommerce中以供订购。 当我用Postman测试它时,会收到以下响应:
<?xml version="1.0" encoding="utf-8"?>
<response>
<status>ok</status>
<result>
<id>1313870523</id>
<barcode>Z1313870523</barcode>
<barcodeText>Z 131 3870 523</barcodeText>
</result>
</response>
我猜我从Wordpress通过API发送响应时应该得到相同的响应... 到目前为止,这是我的代码,并且可以正常运行(只是无法在最后保存响应)...:
$hook_to = 'woocommerce_thankyou';
$what_to_hook = 'wl8OrderPlacedTriggerSomething';
$prioriy = 111;
$num_of_arg = 1;
add_action($hook_to, $what_to_hook, $prioriy, $num_of_arg);
function wl8OrderPlacedTriggerSomething($order_id){
// Order Setup Via WooCommerce
$order = new WC_Order( $order_id );
$items = $order->get_items();
$product_id = $item['product_id'];
$product = new WC_Product($item['product_id']);
foreach ($order->get_items() as $item_id => $item_data) {
// Get an instance of corresponding the WC_Product object
$product = $item_data->get_product();
$product_sku = $product->get_sku(); // Get the product name
$item_quantity = $item_data->get_quantity(); // Get the item quantity
}
$name = $order->shipping_first_name;
$surname = $order->shipping_last_name;
$email = $order->billing_email;
$phone = $order->billing_phone;
$value = $order->total;
$street = $order->shipping_address_1;
$houseNumber = $order->shipping_address_2;
$city = $order->shipping_city;
$zip = $order->shipping_postcode;
$company = $company = $item_quantity.$product_sku;
$gw = new SoapClient("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", array('trace' => 1));
$apiPassword = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
try {
$packet = $gw->createPacket($apiPassword, array(
'number' => $order_id,
'name' => $name,
'surname' => $surname,
'email' => $email,
'phone' => $phone,
'addressId' => 4162,
'cod' => $value,
'value' => $value,
'eshop' => "MyStore",
'street' => $street,
'houseNumber' => "$houseNumber",
'city' => $city,
'zip' => $zip,
'company' => $company
));
}
catch(SoapFault $e) {
var_dump($e->detail); // property detail contains error info
}
update_post_meta( $order_id, 'did-this-run','yes'); // just there as a checker and it works...
}
您是否可以使用update_post_meta($ order_id,'barcode',xxxxxxxx);之类的方法来帮助存储对woocommerce订单的响应?
谢谢!