WC_AJAX::add_order_tax()public staticWC 1.0

Add order tax column via ajax.

Метод класса: WC_AJAX{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$result = WC_AJAX::add_order_tax();

Код WC_AJAX::add_order_tax() WC 8.7.0

public static function add_order_tax() {
	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' ) );
		}

		$rate_id = isset( $_POST['rate_id'] ) ? absint( $_POST['rate_id'] ) : '';

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

		$data = get_post_meta( $order_id );

		// Add new tax.
		$item = new WC_Order_Item_Tax();
		$item->set_rate( $rate_id );
		$item->set_order_id( $order_id );
		$item->save();

		ob_start();
		include __DIR__ . '/admin/meta-boxes/views/html-order-items.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 );
}