Pro*_*777 3 php wordpress orders woocommerce hook-woocommerce
按照这篇文章的思路,当管理员使用我的支付网关通过管理面板创建订单时,我尝试连接到我自己的自定义支付网关。
我添加了以下代码:
add_action( 'woocommerce_process_shop_order_meta', array( $this, 'process_offline_order' ) );
add_action( 'woocommerce_admin_order_actions_end', array( $this, 'process_offline_order2' ) );
add_action( 'woocommerce_save_post_shop_order', array( $this, 'process_offline_order3' ) );
Run Code Online (Sandbox Code Playgroud)
我尝试将 xdebug 断点放入这些相应的方法中,但没有一个被击中。
经过一些研究和测试,我认为正确的挂钩是这个 WP 挂钩之一:
shop_order所以我使用了第一个,因为它对于“ ”帖子类型最方便:
add_action( 'save_post_shop_order', 'process_offline_order', 10, 3 );
function process_offline_order( $post_id, $post, $update ){
// Orders in backend only
if( ! is_admin() ) return;
// Get an instance of the WC_Order object (in a plugin)
$order = new WC_Order( $post_id );
// For testing purpose
$trigger_status = get_post_meta( $post_id, '_hook_is_triggered', true );
// 1. Fired the first time you hit create a new order (before saving it)
if( ! $update )
update_post_meta( $post_id, '_hook_is_triggered', 'Create new order' ); // Testing
if( $update ){
// 2. Fired when saving a new order
if( 'Create new order' == $trigger_status ){
update_post_meta( $post_id, '_hook_is_triggered', 'Save the new order' ); // Testing
}
// 3. Fired when Updating an order
else{
update_post_meta( $post_id, '_hook_is_triggered', 'Update order' ); // Testing
}
}
}
Run Code Online (Sandbox Code Playgroud)
您将能够使用此代码轻松进行测试。对我来说它工作得很好。
我也尝试过使用woocommerce_before_order_object_save有两个参数的钩子:
$order(WC_Order 对象)$data_store(通过WC_Data_Store类来存储的数据)但我并没有像我预期的那样工作。我在方法的源代码中找到了它。WC_Order save()
| 归档时间: |
|
| 查看次数: |
3262 次 |
| 最近记录: |