Recalculated totals not saved after removing order items in Woocommerce 3

147
June 15, 2018, at 1:40 PM

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.

Answer 1

You forgot to use the WC_data save() method after calculated_totals() method, so in your 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();
        $updated_order->save(); // <===  ===  ===  ===  ===  === HERE
        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;
}

It should works this timeā€¦

READ ALSO
How to display comma in PHP array?

How to display comma in PHP array?

I am setting this array, and for the most part it works properly in my siteNOTE: This is a Joomla site and a VirtueMart PHP file for the email that the customer receives when ordering a product

114
Single Page Application with Headless Wordpress

Single Page Application with Headless Wordpress

I'm creating a Single Page Application (using nextjs) to deploy a React application that uses the WP Rest API to fetch content

82
Accessing nested php object data

Accessing nested php object data

I'm trying to get the product attributesI can dig into the objects like this but I get stuck here: How would I get the value 1500?

91
URL redirect rules adding firstvar=&amp;secondvar=

URL redirect rules adding firstvar=&secondvar=

I have a problem with my website's htaccess file and rewrite rule

59