Automattic\WooCommerce\StoreApi\Utilities

OrderController::update_order_from_cart()publicWC 1.0

Update an order using data from the current cart.

Method of the class: OrderController{}

No Hooks.

Return

null. Nothing (null).

Usage

$OrderController = new OrderController();
$OrderController->update_order_from_cart( $order, $update_totals );
$order(\WC_Order) (required)
The order object to update.
$update_totals(true|false)
Whether to update totals or not.
Default: true

OrderController::update_order_from_cart() code WC 9.4.2

public function update_order_from_cart( \WC_Order $order, $update_totals = true ) {
	/**
	 * This filter ensures that local pickup locations are still used for order taxes by forcing the address used to
	 * calculate tax for an order to match the current address of the customer.
	 *
	 * -    The method `$customer->get_taxable_address()` runs the filter `woocommerce_customer_taxable_address`.
	 * -    While we have a session, our `ShippingController::filter_taxable_address` function uses this hook to set
	 *      the customer address to the pickup location address if local pickup is the chosen method.
	 *
	 * Without this code in place, `$customer->get_taxable_address()` is not used when order taxes are calculated,
	 * resulting in the wrong taxes being applied with local pickup.
	 *
	 * The alternative would be to instead use `woocommerce_order_get_tax_location` to return the pickup location
	 * address directly, however since we have the customer filter in place we don't need to duplicate effort.
	 *
	 * @see \WC_Abstract_Order::get_tax_location()
	 */
	add_filter(
		'woocommerce_order_get_tax_location',
		function ( $location ) {

			if ( ! is_null( wc()->customer ) ) {

				$taxable_address = wc()->customer->get_taxable_address();

				$location = array(
					'country'  => $taxable_address[0],
					'state'    => $taxable_address[1],
					'postcode' => $taxable_address[2],
					'city'     => $taxable_address[3],
				);
			}

			return $location;
		}
	);

	// Ensure cart is current.
	if ( $update_totals ) {
		wc()->cart->calculate_totals();
	}

	// Update the current order to match the current cart.
	$this->update_line_items_from_cart( $order );
	$this->update_addresses_from_cart( $order );
	$order->set_currency( get_woocommerce_currency() );
	$order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
	$order->set_customer_id( get_current_user_id() );
	$order->set_customer_ip_address( \WC_Geolocation::get_ip_address() );
	$order->set_customer_user_agent( wc_get_user_agent() );
	$order->update_meta_data( 'is_vat_exempt', wc()->cart->get_customer()->get_is_vat_exempt() ? 'yes' : 'no' );
	$order->calculate_totals();
}