WC_Abstract_Order::remove_order_itemspublicWC 1.0

Remove all line items (products, coupons, shipping, taxes) from the order.

The items are cleared from the in-memory order immediately, but the database deletion is deferred until the next call to save(). This keeps the checkout "resume order" flow atomic: if anything between here and save() throws, the previously persisted items remain intact in the database. As a consequence, the woocommerce_removed_order_items now fires from save_items() (after the actual DB delete completes) rather than synchronously from this method — listeners that observe the persisted state continue to see it as before, but listeners pairing pre/post on the same call stack will see the post-hook fire at save() time.

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

Хуки из метода

Возвращает

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

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

$WC_Abstract_Order = new WC_Abstract_Order();
$WC_Abstract_Order->remove_order_items( $type );
$type(строка|null)
Order item type.
По умолчанию: null (remove every type)

Код WC_Abstract_Order::remove_order_items() WC 11.0.1

public function remove_order_items( $type = null ) {

	// Guard against extension code passing non-string values. Anything not
	// a string or null is ignored so it never reaches the deferred queue or
	// the data store. Surface the misuse so it's traceable rather than silent.
	if ( null !== $type && ! is_string( $type ) ) {
		wc_doing_it_wrong(
			__METHOD__,
			/* translators: %s: PHP type that was passed instead of a string. */
			sprintf( esc_html__( 'remove_order_items() expects a string item type or null; received %s.', 'woocommerce' ), esc_html( gettype( $type ) ) ),
			'11.0.0'
		);
		return;
	}

	/**
	 * Trigger action before removing all order line items. Allows you to track order items.
	 *
	 * @param  WC_Order  $this  The current order object.
	 * @param  string|null $type Order item type. Default null.
	 *
	 * @since 7.8.0
	 */
	do_action( 'woocommerce_remove_order_items', $this, $type );

	// Unsaved orders (id 0) have no persisted items — there's nothing to defer for deletion.
	$has_persisted_items = $this->get_id() > 0;

	if ( ! empty( $type ) ) {
		if ( $has_persisted_items ) {
			$this->item_types_to_bulk_delete[] = $type;
		}

		$group = $this->type_to_group( $type );

		if ( $group ) {
			// Set to an empty array (rather than unset) so that subsequent get_items() calls
			// return the in-memory "removed" state without re-reading the still-present rows
			// from the data store.
			$this->items[ $group ] = array();
		}
	} else {
		if ( $has_persisted_items ) {
			$this->bulk_delete_all_items_pending = true;
			$this->item_types_to_bulk_delete     = array();
		}
		$type_to_group = $this->get_item_types_to_group();
		// Union with currently populated keys so any group already loaded into
		// $this->items (including by direct manipulation) is reset too. This
		// matches the historical "wipe everything" semantics that the original
		// $this->items = array() provided.
		$groups = array_unique(
			array_merge( array_values( $type_to_group ), array_keys( $this->items ) )
		);
		foreach ( $groups as $group ) {
			$this->items[ $group ] = array();
		}
	}
}