Automattic\WooCommerce\Api\Mutations\Products

CreateProduct::executepublicWC 1.0

[ReturnType( Product::class )]

Method of the class: CreateProduct{}

No Hooks.

Returns

null. Nothing (null).

Usage

$CreateProduct = new CreateProduct();
$CreateProduct->execute( #[Description( foo )] CreateProductInput $input,  ): object;

[Description( foo )] CreateProductInput $input (required)

: .

(required)
.

CreateProduct::execute() code WC 10.9.1

public function execute(
	#[Description( 'Data for the new product.' )]
	CreateProductInput $input,
): object {
	// Best-effort duplicate-name check. There is an inherent TOCTOU race
	// here: two nearly-simultaneous requests with the same name can both
	// pass this check and both succeed in creating the product, because
	// wp_posts.post_title is not a unique column in the schema and WP
	// offers no portable atomic "reserve name" primitive. Locking via
	// wp_cache_add() would help only on sites with a persistent object
	// cache (Redis/Memcached), so we do not rely on it here. If strict
	// uniqueness is ever required, callers should enforce it at a
	// higher layer (e.g. a mutex around the REST handler) rather than
	// assume the API guarantees it.
	$existing = new \WP_Query(
		array(
			'post_type'   => 'product',
			'title'       => $input->name,
			'post_status' => array( 'publish', 'draft', 'pending', 'private' ),
			'fields'      => 'ids',
		)
	);

	if ( $existing->found_posts > 0 ) {
		throw new ApiException(
			'A product with this name already exists.',
			'VALIDATION_ERROR',
			array( 'field' => 'name' ),
			422,
		);
	}

	$wc_product = new \WC_Product();
	$wc_product->set_name( $input->name );

	foreach ( array( 'slug', 'sku', 'description', 'short_description', 'manage_stock', 'stock_quantity' ) as $field ) {
		if ( null !== $input->$field ) {
			$wc_product->{"set_{$field}"}( $input->$field );
		}
	}

	foreach ( array( 'regular_price', 'sale_price' ) as $field ) {
		if ( null !== $input->$field ) {
			$wc_product->{"set_{$field}"}( (string) $input->$field );
		}
	}

	if ( null !== $input->status ) {
		$wc_product->set_status( $input->status->value );
	}

	if ( null !== $input->dimensions ) {
		foreach ( array( 'length', 'width', 'height', 'weight' ) as $field ) {
			if ( null !== $input->dimensions->$field ) {
				$wc_product->{"set_{$field}"}( (string) $input->dimensions->$field );
			}
		}
	}

	$this->repository->save( $wc_product );

	return ProductMapper::from_wc_product( $wc_product );
}