我不確定創建訂單后管理員何時更新送貨/賬單地址需要設定哪個掛鉤/操作。
所以我在這里想要實作的是:
- 在 WooCommerce 訂單部分,當管理員更新送貨/賬單地址時,它會觸發一個操作。
- 此操作基本上對我的自定義腳本進行了一次 curl 呼叫,并讓我知道訂單的地址已被管理員更改。
- 我會在我的腳本中做一些魔術。
我在下面找到了,但我不認為它更多來自管理方面。
// define the woocommerce_admin_order_data_after_shipping_address callback
function action_woocommerce_admin_order_data_after_shipping_address(
$delta_wccs_custom_checkout_details_pro_shipping, $int, $int ) {
// make action magic happen here...
};
// add the action
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'action_woocommerce_admin_order_data_after_shipping_address', 10, 3 );
請讓我知道是否有人知道在訂單發貨/帳單地址更改時觸發的正確操作。
uj5u.com熱心網友回復:
鉤子是在woocommerce_admin_order_data_after_shipping_address
訂單編輯頁面(后端)上顯示額外的內容
$order_item
要在保存到資料庫之前或之后觸發操作,請使用:
/**
* Trigger action before saving to the DB. Allows you to adjust object props before save.
*
* @param WC_Data $this The object being saved.
* @param WC_Data_Store_WP $data_store THe data store persisting the data.
*/
function action_woocommerce_before_order_item_object_save( $order_item, $data_store ) {
// Get type
$data_type = $order_item->get_type();
// Before billing changes
if ( $data_type == 'billing' ) {
// Do..
}
// Before shipping changes
if ( $data_type == 'shipping' ) {
// Do..
}
}
add_action( 'woocommerce_before_order_item_object_save', 'action_woocommerce_before_order_item_object_save', 10, 2 );
/**
* Trigger action after saving to the DB.
*
* @param WC_Data $this The object being saved.
* @param WC_Data_Store_WP $data_store THe data store persisting the data.
*/
function action_woocommerce_after_order_item_object_save( $order_item, $data_store ) {
// Get type
$data_type = $order_item->get_type();
// After billing changes
if ( $data_type == 'billing' ) {
// Do..
}
// After shipping changes
if ( $data_type == 'shipping' ) {
// Do..
}
}
add_action( 'woocommerce_after_order_item_object_save', 'action_woocommerce_after_order_item_object_save', 10, 2 );
或者
woocommerce_before_order_object_save
使用可能更合適的幾乎相同的鉤子,因為通過$order->get_changes()
您可以觸發/記錄/比較哪些$order
資料已更改
function action_woocommerce_before_order_object_save( $order, $data_store ) {
// Get changes
$changes = $order->get_changes();
// Billing OR shipping
if ( isset( $changes['billing'] ) || isset( $changes['shipping'] ) ) {
// Do..
}
// OR even more specific (e.g.: shipping first name field was changed)
if ( isset( $changes['shipping_first_name'] ) ) {
// Do..
}
}
add_action( 'woocommerce_before_order_object_save', 'action_woocommerce_before_order_object_save', 10, 2 );
編輯:這是一個已知問題,這些鉤子在不打算被呼叫時被多次呼叫
請參閱:https ://github.com/woocommerce/woocommerce/issues/25771
作為一種解決方法,添加:
if ( did_action( 'replace_by_the_desired_hook_name' ) >= 2 ) return;
作為回呼函式的第一行
uj5u.com熱心網友回復:
// Define the woocommerce_admin_order_data_after_shipping_address callback .
function action_woocommerce_admin_order_data_after_shipping_address( $order ) {
// This hook will only fire in backend when viewing the order edit screen. Not for orders placed from checkout
};
// add the action
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'action_woocommerce_admin_order_data_after_shipping_address', 10, 1 );
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/468312.html
標籤:WordPress woocommerce 后端 钩woocommerce 订单