CreateProduct.php
117 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Api\Mutations\Products; |
| 6 | |
| 7 | use Automattic\WooCommerce\Api\ApiException; |
| 8 | use Automattic\WooCommerce\Api\Attributes\Description; |
| 9 | use Automattic\WooCommerce\Api\Attributes\RequiredCapability; |
| 10 | use Automattic\WooCommerce\Api\Attributes\ReturnType; |
| 11 | use Automattic\WooCommerce\Api\InputTypes\Products\CreateProductInput; |
| 12 | use Automattic\WooCommerce\Api\Interfaces\Product; |
| 13 | use Automattic\WooCommerce\Api\Traits\RequiresManageWoocommerce; |
| 14 | use Automattic\WooCommerce\Api\Utils\Products\ProductMapper; |
| 15 | use Automattic\WooCommerce\Api\Utils\Products\ProductRepository; |
| 16 | |
| 17 | /** |
| 18 | * Mutation to create a new product. |
| 19 | * |
| 20 | * Demonstrates: DI via init(), inherited capability (trait), ApiException with extensions. |
| 21 | */ |
| 22 | #[Description( 'Create a new product.' )] |
| 23 | #[RequiredCapability( 'edit_products' )] |
| 24 | class CreateProduct { |
| 25 | use RequiresManageWoocommerce; |
| 26 | |
| 27 | /** |
| 28 | * The product repository. |
| 29 | * |
| 30 | * @var ProductRepository |
| 31 | */ |
| 32 | private ProductRepository $repository; |
| 33 | |
| 34 | /** |
| 35 | * Inject dependencies via the DI container. |
| 36 | * |
| 37 | * @internal |
| 38 | * |
| 39 | * @param ProductRepository $repository The product repository. |
| 40 | */ |
| 41 | final public function init( ProductRepository $repository ): void { |
| 42 | $this->repository = $repository; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Execute the mutation. |
| 47 | * |
| 48 | * @param CreateProductInput $input The product creation data. |
| 49 | * @return object |
| 50 | * @throws ApiException When validation fails. |
| 51 | */ |
| 52 | #[ReturnType( Product::class )] |
| 53 | public function execute( |
| 54 | #[Description( 'Data for the new product.' )] |
| 55 | CreateProductInput $input, |
| 56 | ): object { |
| 57 | // Best-effort duplicate-name check. There is an inherent TOCTOU race |
| 58 | // here: two nearly-simultaneous requests with the same name can both |
| 59 | // pass this check and both succeed in creating the product, because |
| 60 | // wp_posts.post_title is not a unique column in the schema and WP |
| 61 | // offers no portable atomic "reserve name" primitive. Locking via |
| 62 | // wp_cache_add() would help only on sites with a persistent object |
| 63 | // cache (Redis/Memcached), so we do not rely on it here. If strict |
| 64 | // uniqueness is ever required, callers should enforce it at a |
| 65 | // higher layer (e.g. a mutex around the REST handler) rather than |
| 66 | // assume the API guarantees it. |
| 67 | $existing = new \WP_Query( |
| 68 | array( |
| 69 | 'post_type' => 'product', |
| 70 | 'title' => $input->name, |
| 71 | 'post_status' => array( 'publish', 'draft', 'pending', 'private' ), |
| 72 | 'fields' => 'ids', |
| 73 | ) |
| 74 | ); |
| 75 | |
| 76 | if ( $existing->found_posts > 0 ) { |
| 77 | throw new ApiException( |
| 78 | 'A product with this name already exists.', |
| 79 | 'VALIDATION_ERROR', |
| 80 | array( 'field' => 'name' ), |
| 81 | 422, |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | $wc_product = new \WC_Product(); |
| 86 | $wc_product->set_name( $input->name ); |
| 87 | |
| 88 | foreach ( array( 'slug', 'sku', 'description', 'short_description', 'manage_stock', 'stock_quantity' ) as $field ) { |
| 89 | if ( null !== $input->$field ) { |
| 90 | $wc_product->{"set_{$field}"}( $input->$field ); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | foreach ( array( 'regular_price', 'sale_price' ) as $field ) { |
| 95 | if ( null !== $input->$field ) { |
| 96 | $wc_product->{"set_{$field}"}( (string) $input->$field ); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if ( null !== $input->status ) { |
| 101 | $wc_product->set_status( $input->status->value ); |
| 102 | } |
| 103 | |
| 104 | if ( null !== $input->dimensions ) { |
| 105 | foreach ( array( 'length', 'width', 'height', 'weight' ) as $field ) { |
| 106 | if ( null !== $input->dimensions->$field ) { |
| 107 | $wc_product->{"set_{$field}"}( (string) $input->dimensions->$field ); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | $this->repository->save( $wc_product ); |
| 113 | |
| 114 | return ProductMapper::from_wc_product( $wc_product ); |
| 115 | } |
| 116 | } |
| 117 |