douxian6086 2018-06-14 23:33
浏览 44
已采纳

删除Woocommerce 3中的订单商品后,未保存重新计算的总计

I'm using Wordpress 4.9.6 and WooCommerce 3.4.2 on a Woocommerce site and where I have a custom feature that creates invoices for event registrations.

Right now I'm working on a script that will just remove one item from an existing order. The items remove fine, but the total doesn't recalculate. I can view the order in wp-admin and recalculate the totals from there fine, but I need it to happen in this script.

I can't for the life of me figure out what's keeping this from working...

My actual code:

$order = wc_get_order($event_registration['OrderID']);

if ( count($order->get_items()) > 1 ) {
  if ( !wc_delete_order_item($event_registration['OrderItemID']) ) {
    return array('error' => 'Problem removing order item');
  }

  $updated_order = wc_get_order($event_registration['OrderID']);

  if ( count($updated_order->get_items()) > 0) {
    $updated_order->calculate_totals();
    PluginWoo::update_invoice($updated_order->get_id());

    return true;
  } else {
    if ( !wp_delete_post( $event_registration['OrderID'], true ) ) {
      return array('error' => 'Problem deleting order after deleting items');
    }
    return true;
  }
} else {
  if ( !wp_delete_post( $event_registration['OrderID'], true ) ) {
    return array('error' => 'Problem deleting order');
  }
  return true;
}

Any help on this is appreciated.

  • 写回答

1条回答 默认 最新

  • duanbohan2015 2018-06-15 00:16
    关注

    If it's like wc_delete_order_item() function is running asynchronously and not completing before calculate_totals() method, you could try to use WC_Data delete() method instead.

    As your code is not really testable, I am just wondering. So you could try something like:

    $order = wc_get_order( $event_registration['OrderID'] );
    $items = $order->get_items();
    if ( count($items) > 1 ) {
        if( isset( $items[$event_registration['OrderItemID']] ) )
            $items[$event_registration['OrderItemID']]->delete(); // Deleting
        else
            return array('error' => 'Problem removing order item'); // Returning error
    }
    
    $updated_order = wc_get_order( $event_registration['OrderID'] );
    $items = $updated_order->get_items();
    if ( count($items) > 0 ) {
        $updated_order->calculate_totals();
    
        PluginWoo::update_invoice($updated_order->get_id());
        return true;
    } else {
        if( isset( $items[$event_registration['OrderItemID']] ) )
            $items[$event_registration['OrderItemID']]->delete(); // Deleting
        else
            return array('error' => 'Problem deleting order after deleting items');
    
        return true;
    }
    

    It could works…

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题