这就是重点。我需要这样做,以便当您通过Paypal创建订单时,订单中的商品数量是从商品本身的剩余部分中获取的。我完成了该功能,并且100%正常工作。
add_action ('save_post_shop_order', 'create_invoice_for_wc_order', 10,3);
function create_invoice_for_wc_order ($ order_id) {
$ order = wc_get_order ($ order_id);
// Iterating through each "line" items in the order
foreach ($ order-> get_items () as $ item_id => $ item_data) {
// Get an instance of the corresponding WC_Product object
$ product = $ item_data-> get_product ();
$ product_name = $ product-> get_name (); // Get the product name
$ item_quantity = $ item_data-> get_quantity (); // Get the item quantity
$ all_qua = $ product-> get_stock_quantity ();
$ diff = $ all_qua - $ item_quantity;
wc_update_product_stock ($ product, $ diff);
$ item_total = $ item_data-> get_total (); // Get the item line total
// Displaying this data (to check)
echo 'Product name:'. $ product_name. ' | | Quantity: '. $ Item_quantity.' | | All_Quantity: '. $ All_qua.' | | Item total: '. number_format ($ item_total, 2);
// $ txt = fopen ("C: \\ ospanel \\ domains \\ joke.fuu \\ wp-content \\ themes \\ storefront \\ hhh.txt", "w");
// fwrite ($ txt, $ new_status);
}
}
但是!它要挂在哪个挂钩上,以便它可以创建帖子? save_post仅适用于更新,wp_insert_post-也适用。
我也尝试过与woocommerce挂钩,例如woocommerce_new_order,也无济于事。