EligibilityService.php
10 months ago
StockManagementHelper.php
10 months ago
UtmHelper.php
4 weeks ago
StockManagementHelper.php
57 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Internal\StockNotifications\Utilities; |
| 5 | |
| 6 | use Automattic\WooCommerce\Enums\ProductType; |
| 7 | use WC_Product; |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * Utility class for stock management related queries. |
| 13 | */ |
| 14 | class StockManagementHelper { |
| 15 | |
| 16 | /** |
| 17 | * Runtime cache for managed variations. |
| 18 | * |
| 19 | * @var array<int, array<int>> |
| 20 | */ |
| 21 | private array $managed_variations = array(); |
| 22 | |
| 23 | /** |
| 24 | * Get a list of variations that inherit stock management from the parent. |
| 25 | * |
| 26 | * If the product is a variable product, we need sync the children that don't manage stock. |
| 27 | * |
| 28 | * @param WC_Product $product The product to check. |
| 29 | * @return array<int> Array of variation IDs that inherit stock management from the parent. |
| 30 | */ |
| 31 | public function get_managed_variations( WC_Product $product ): array { |
| 32 | if ( ! $product->is_type( ProductType::VARIABLE ) ) { |
| 33 | return array(); |
| 34 | } |
| 35 | |
| 36 | $product_id = $product->get_id(); |
| 37 | if ( isset( $this->managed_variations[ $product_id ] ) ) { |
| 38 | return $this->managed_variations[ $product_id ]; |
| 39 | } |
| 40 | |
| 41 | $children = $product->get_children(); |
| 42 | if ( empty( $children ) ) { |
| 43 | return array(); |
| 44 | } |
| 45 | |
| 46 | global $wpdb; |
| 47 | |
| 48 | $format = array_fill( 0, count( $children ), '%d' ); |
| 49 | $query_in = '(' . implode( ',', $format ) . ')'; |
| 50 | $managed_children = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = '_manage_stock' AND meta_value != 'yes' AND post_id IN {$query_in}", $children ) ); // @codingStandardsIgnoreLine. |
| 51 | |
| 52 | $this->managed_variations[ $product_id ] = array_map( 'intval', $managed_children ); |
| 53 | |
| 54 | return $this->managed_variations[ $product_id ]; |
| 55 | } |
| 56 | } |
| 57 |