WC_AJAX::add_order_shipping()public staticWC 1.0

Add order shipping cost via ajax.

Method of the class: WC_AJAX{}

No Hooks.

Return

null. Nothing (null).

Usage

$result = WC_AJAX::add_order_shipping();

WC_AJAX::add_order_shipping() code WC 8.6.1

public static function add_order_shipping() {
	check_ajax_referer( 'order-item', 'security' );

	if ( ! current_user_can( 'edit_shop_orders' ) ) {
		wp_die( -1 );
	}

	$response = array();

	try {
		$order_id = isset( $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : 0;
		$order    = wc_get_order( $order_id );

		if ( ! $order ) {
			throw new Exception( __( 'Invalid order', 'woocommerce' ) );
		}

		$order_taxes      = $order->get_taxes();
		$shipping_methods = WC()->shipping() ? WC()->shipping()->load_shipping_methods() : array();

		// Add new shipping.
		$item = new WC_Order_Item_Shipping();
		$item->set_shipping_rate( new WC_Shipping_Rate() );
		$item->set_order_id( $order_id );
		$item_id = $item->save();

		ob_start();
		include __DIR__ . '/admin/meta-boxes/views/html-order-shipping.php';
		$response['html'] = ob_get_clean();
	} catch ( Exception $e ) {
		wp_send_json_error( array( 'error' => $e->getMessage() ) );
	}

	// wp_send_json_success must be outside the try block not to break phpunit tests.
	wp_send_json_success( $response );
}