ProductRepository.php
34 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Api\Utils\Products; |
| 6 | |
| 7 | /** |
| 8 | * Repository for product persistence operations. |
| 9 | * |
| 10 | * Designed to be injected via the DI container into commands |
| 11 | * that need to load or save products. |
| 12 | */ |
| 13 | class ProductRepository { |
| 14 | /** |
| 15 | * Find a product by ID. |
| 16 | * |
| 17 | * @param int $id The product ID. |
| 18 | * @return ?\WC_Product The product, or null if not found. |
| 19 | */ |
| 20 | public function find( int $id ): ?\WC_Product { |
| 21 | $product = wc_get_product( $id ); |
| 22 | return $product instanceof \WC_Product ? $product : null; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Save a product. |
| 27 | * |
| 28 | * @param \WC_Product $product The product to save. |
| 29 | */ |
| 30 | public function save( \WC_Product $product ): void { |
| 31 | $product->save(); |
| 32 | } |
| 33 | } |
| 34 |