WC_AJAX::remove_order_item()public staticWC 1.0

Remove an order item.

Method of the class: WC_AJAX{}

Hooks from the method

Return

null. Nothing (null).

Usage

$result = WC_AJAX::remove_order_item();

WC_AJAX::remove_order_item() code WC 8.6.1

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

	if ( ! current_user_can( 'edit_shop_orders' ) || ! isset( $_POST['order_id'], $_POST['order_item_ids'] ) ) {
		wp_die( -1 );
	}

	$response = array();

	try {
		$order_id = absint( $_POST['order_id'] );
		$order    = wc_get_order( $order_id );

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

		if ( ! isset( $_POST['order_item_ids'] ) ) {
			throw new Exception( __( 'Invalid items', 'woocommerce' ) );
		}

		$order_item_ids     = wp_unslash( $_POST['order_item_ids'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
		$items              = ( ! empty( $_POST['items'] ) ) ? wp_unslash( $_POST['items'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
		$calculate_tax_args = array(
			'country'  => isset( $_POST['country'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['country'] ) ) ) : '',
			'state'    => isset( $_POST['state'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['state'] ) ) ) : '',
			'postcode' => isset( $_POST['postcode'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['postcode'] ) ) ) : '',
			'city'     => isset( $_POST['city'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['city'] ) ) ) : '',
		);

		if ( is_numeric( $order_item_ids ) ) {
			$order_item_ids = array( $order_item_ids );
		}

		// If we passed through items it means we need to save first before deleting.
		if ( ! empty( $items ) ) {
			$save_items = array();
			parse_str( $items, $save_items );
			wc_save_order_items( $order->get_id(), $save_items );
		}

		if ( ! empty( $order_item_ids ) ) {

			foreach ( $order_item_ids as $item_id ) {
				$item_id = absint( $item_id );
				$item    = $order->get_item( $item_id );

				if ( ! $item ) {
					continue;
				}

				// Before deleting the item, adjust any stock values already reduced.
				if ( $item->is_type( 'line_item' ) ) {
					$changed_stock = wc_maybe_adjust_line_item_product_stock( $item, 0 );

					if ( $changed_stock && ! is_wp_error( $changed_stock ) ) {
						/* translators: %1$s: item name %2$s: stock change */
						$order->add_order_note( sprintf( __( 'Deleted %1$s and adjusted stock (%2$s)', 'woocommerce' ), $item->get_name(), $changed_stock['from'] . '→' . $changed_stock['to'] ), false, true );
					} else {
						/* translators: %s item name. */
						$order->add_order_note( sprintf( __( 'Deleted %s', 'woocommerce' ), $item->get_name() ), false, true );
					}
				}

				wc_delete_order_item( $item_id );
			}
		}

		$order = wc_get_order( $order_id );
		$order->calculate_taxes( $calculate_tax_args );
		$order->calculate_totals( false );

		/**
		 * Fires after order items are removed.
		 *
		 * @since 5.2.0
		 *
		 * @param int $item_id WC item ID.
		 * @param WC_Order_Item|false $item As returned by $order->get_item( $item_id ).
		 * @param bool|array|WP_Error $changed_store Result of wc_maybe_adjust_line_item_product_stock().
		 * @param bool|WC_Order|WC_Order_Refund $order As returned by wc_get_order().
		 */
		do_action( 'woocommerce_ajax_order_items_removed', $item_id ?? 0, $item ?? false, $changed_stock ?? false, $order );

		// Get HTML to return.
		ob_start();
		include __DIR__ . '/admin/meta-boxes/views/html-order-items.php';
		$items_html = ob_get_clean();

		ob_start();
		$notes = wc_get_order_notes( array( 'order_id' => $order_id ) );
		include __DIR__ . '/admin/meta-boxes/views/html-order-notes.php';
		$notes_html = ob_get_clean();

		wp_send_json_success(
			array(
				'html'       => $items_html,
				'notes_html' => $notes_html,
			)
		);
	} 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 );
}