WC_REST_Paypal_Standard_Controller::rebuild_cart_from_orderprivateWC 1.0

Rebuild the session cart.

Method of the class: WC_REST_Paypal_Standard_Controller{}

No Hooks.

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->rebuild_cart_from_order( $order );
$order(WC_Order) (required)
The order object.

WC_REST_Paypal_Standard_Controller::rebuild_cart_from_order() code WC 10.3.6

private function rebuild_cart_from_order( $order ) {
	wc_load_cart();
	WC()->cart->empty_cart();
	foreach ( $order->get_items() as $item ) {
		$product_id = $item->get_product_id();
		$product    = $item->get_product();

		if ( ! $product ) {
			continue;
		}

		if ( $product->is_type( 'variation' ) ) {
			$variation_id = $item->get_variation_id();
			WC()->cart->add_to_cart( $product_id, $item->get_quantity(), $variation_id );
			continue;
		}

		WC()->cart->add_to_cart( $product_id, $item->get_quantity() );
	}

	// Re-apply coupons present on the order so discounts/totals are accurate.
	if ( method_exists( $order, 'get_coupon_codes' ) ) {
		foreach ( (array) $order->get_coupon_codes() as $code ) {
			if ( $code ) {
				WC()->cart->apply_coupon( $code );
			}
		}
	}

	// Re-apply shipping methods present on the order so totals are accurate.
	$order_shipping_rate_id = $this->get_order_shipping_rate_id( $order );
	if ( ! empty( $order_shipping_rate_id ) ) {
		WC()->session->set( 'chosen_shipping_methods', array( $order_shipping_rate_id ) );
	}
}