2017-01-08 17:31
浏览 145


I need to do an action when an order is completed.

I Have tried this:

function mysite_woocommerce_payment_complete( $order_id ) {
error_log("callback fired");
add_action( 'woocommerce_payment_complete', 'mysite_woocommerce_payment_complete' );

But when I use the checkmark to mark the order as completed here in admin orders screen, mark order complete

...the hook doesn't fire. I tried woocommerce_order_status_changed too, it does the action when I place the order, but does nothing when i mark the order completed.

But when I mark the order completed, I get the email about completing it.


Edit: I tried woocommerce_order_status_changed too, this way:

function mysite_woocommerce_payment_complete($order_id, $old_status, $new_status ) {
    error_log("$old_status / $new_status 
add_action( 'woocommerce_order_status_changed', 'mysite_woocommerce_payment_complete', 99, 3 );

but it fires on purchasing (I selected bank transfer) and shows: "pending / on-hold", but not true - see edi2 doesn't fire on manual backend change from "on hold" to "completed". Neither by checkmark nor in single order interface.

Edit2 woocommerce_order_status_changed and woocommerce_order_status_completed works, it only outputed my testing "error" into debug.log, not to error_log for some reason. The woocommerce_payment_complete i used previously doesn't apply to methods like bank transfer, that's why that didn't work. Thanks to @helgatheviking for the quick and right answer

图片转代码服务由CSDN问答提供 功能建议



  function mysite_woocommerce_payment_complete($ order_id){
error_log(“callback fired”); 
} \  nadd_action('woocommerce_payment_complete','mysite_woocommerce_payment_complete'); 


...钩子不会触发。 我也尝试了 woocommerce_order_status_changed ,它在我放置时执行操作 订单,但在我标记订单完成时无效。



编辑: 我也尝试了 woocommerce_order_status_changed ,这样:

  function mysite_woocommerce_payment_complete($ order_id,$ old_status,$  new_status){
 error_log(“$ old_status / $ new_status 
 \  n 

但它会在购买时触发(我选择银行转帐)并显示:“挂起/暂停”,但不正确 - 请参阅edi2 不会触发手动后端更改 坚持“完成”。 无论是通过复选标记还是单一订单界面。

Edit2 woocommerce_order_status_changed woocommerce_order_status_completed 有效, 只是将我的测试“错误”输出到debug.log中,而不是出于某种原因而不是error_log。 我之前使用的 woocommerce_payment_complete 不适用于银行转帐等方法,这就是为什么不起作用的原因。 感谢@helgatheviking提供快速正确的答案

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzouxigu12345 2017-01-08 19:26

    Well the completed order email is triggered by the following:

    // Triggers for this email
    add_action( 'woocommerce_order_status_completed_notification', array( $this, 'trigger' ) );

    as seen here in source.

    All "transactional email actions" (ie: actions that trigger the sending of an email) get a _notification hook in addition to the normal hook, seen here.

    Thus woocommerce_order_status_completed_notification is an additional hook triggered on the woocommerce_order_status_completed hook if woocommerce_order_status_completed is in the woocommerce_email_actions array, which it is by default. To avoid any surprised from the emails, I recommend using the woocommerce_order_status_completed hook which is fired any time there is an order status change, including from within the admin, see this example:

    function mysite_woocommerce_payment_complete( $order_id ) {
        error_log("callback fired");
    add_action( 'woocommerce_order_status_completed', 'mysite_woocommerce_payment_complete' );
    打赏 评论

相关推荐 更多相似问题