duanjian7617 2017-12-13 23:16
浏览 84
已采纳

WooCommerce Hook woocommerce_cancelled_order

Good Day,

This is my first time using stackoverflow, nice to meet you all.

Anywho, I'm writing a plugin for WooCommerce and I am automatically refunding an order when it is cancelled. My code works fine when I manually execute it in a separate file without the hook, however, with my hook it seems to not execute. I'm doing the following:

add_action('woocommerce_cancelled_order','change_status_to_refund', 10, 1);
function change_status_to_refund( $order_id ) {
wp_redirect( home_url() );
   $order = new WC_Order( $order_id );

if( 'refunded' == $order->get_status() ) {
     return false;
 }


if(!($order->is_paid())) {
    return false;
}


$noRefundLimit = 24 * 60; //in minutes until booking
$customer_orders = get_posts( array(
 'numberposts' => 1,
 'post_parent' => $order_id,
 'post_type'   => 'wc_booking', // WC orders post type
 'post_status' => 'paid, complete' // Only paid, completed bookings
 ) );
 $bookingId = current($customer_orders)->ID;

 $bookingStart = current(get_post_meta($bookingId, "_booking_start"));
 $time = (new DateTime($bookingStart, new 
 DateTimeZone("America/Los_Angeles")))->getTimestamp();
 $nowTime = (new DateTime())->getTimestamp();
 $difference = round(($time - $nowTime)/60);//in minutes

 if($difference >= $noRefundLimit) {
     $refundPercentage = 1; //how much will we give back? fraction of 1.
     // Get Items
     $order_items   = $order->get_items();

     // Refund Amount
       $refund_amount = 0;
     // Prepare line items which we are refunding
     $line_items = array();

     if ( $order_items ) {
       foreach( $order_items as $item_id => $item ) {
        $refund_amount += $item->get_total();
       }
     }
     $refund_amount = ($refund_amount * $refundPercentage);
     $refund_reason = "Order Cancelled";
     $refund = wc_create_refund( array(
       'amount'         => $refund_amount,
       'reason'         => $refund_reason,
       'order_id'       => $order_id,
       'line_items'     => $line_items,
       'refund_payment' => true
       ));

       var_dump($refund);

       $order->update_status('wc-refunded', 'Order Cancelled And Completely 
Refunded');
       $order->save();
}

I have the first redirect line added for the sole purpose of testing if it redirects, which it doesn't! Any idea why this hook isn't firing?

  • 写回答

2条回答 默认 最新

  • douyangcheng4965 2017-12-14 21:15
    关注

    Thank you for your comment. It seems that the woocommerce_order_status_cancelled hook is good but the parameter is $order_id. After a tremendous amount of debugging I have the following:

    add_action( 'woocommerce_order_status_cancelled', 'change_status_to_refund', 
    21, 1 );
    function change_status_to_refund( $order_id ) {
    
    $order = new WC_Order( $order_id );      
    
    $noRefundLimit = 24 * 60; //in minutes until booking
    
    global $wpdb;
    $querystr = "
    SELECT $wpdb->posts.* 
    FROM $wpdb->posts 
    WHERE $wpdb->posts.post_parent = $order_id
    ";
    
    $pageposts = $wpdb->get_results($querystr, OBJECT);
    
     $bookingId = current($pageposts)->ID;
    
     $bookingStart = current(get_post_meta($bookingId, "_booking_start"));
     $time = (new DateTime($bookingStart, new 
     DateTimeZone("America/Los_Angeles")))->getTimestamp();
     $nowTime = (new DateTime())->getTimestamp();
     $difference = round(($time - $nowTime)/60);//in minutes
    
     if($difference >= $noRefundLimit) {
         $refundPercentage = 1; //how much will we give back? fraction of 1.
         // Get Items
         $order_items   = $order->get_items();
    
         // Refund Amount
           $refund_amount = 0;
         // Prepare line items which we are refunding
         $line_items = array();
    
         if ( $order_items ) {
           foreach( $order_items as $item_id => $item ) {
            $refund_amount += $item->get_total();
           }
         }
         $refund_amount = ($refund_amount * $refundPercentage);
         $refund_reason = "Order Cancelled";
         $refund = wc_create_refund( array(
           'amount'         => $refund_amount,
           'reason'         => $refund_reason,
           'order_id'       => $order_id,
           'line_items'     => $line_items,
           'refund_payment' => true
           ));
    
           $order->update_status('wc-refunded', 'Order Cancelled And Completely 
            Refunded');
         }
    }
    

    I had a few problems. $order->is_paid() returns false even though the order is paid (I think this is because the status changed from paid to cancelled/refunded) so my code wasn't even getting that far. Then get_posts wasn't working as expected. I was forced to use $wpdb. After that my code worked.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)