Automattic\WooCommerce\Internal\DataStores\Fulfillments

FulfillmentsDataStore::validate_itemsprivateWC 1.0

Method to validate the items in a fulfillment.

Method of the class: FulfillmentsDataStore{}

No Hooks.

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->validate_items( $data ): void;
$data(Fulfillment) (required)
The fulfillment object to validate.

FulfillmentsDataStore::validate_items() code WC 10.3.6

private function validate_items( Fulfillment $data ): void {
	$items = $data->get_meta( '_items', true );
	if ( empty( $items ) ) {
		throw new \Exception( esc_html__( 'The fulfillment should contain at least one item.', 'woocommerce' ) );
	}

	if ( ! is_array( $items ) ) {
		throw new \Exception( esc_html__( 'The fulfillment items should be an array.', 'woocommerce' ) );
	}

	foreach ( $data->get_items() as $item ) {
		if ( ! isset( $item['item_id'] )
			// The item ID and qty should be set.
			|| ! isset( $item['qty'] )
			// The item ID should be integers.
			|| ! is_int( $item['item_id'] )
			// Allow the qty to be a float too.
			|| ( ! is_int( $item['qty'] ) && ! is_float( $item['qty'] ) )
			// The item ID and qty should be greater than 0.
			|| $item['item_id'] <= 0
			|| $item['qty'] <= 0
			) {
			throw new \Exception( esc_html__( 'Invalid item.', 'woocommerce' ) );
		}
	}
}