BlockHooksTrait.php
4 weeks ago
BlockTemplateUtils.php
2 months ago
BlocksSharedState.php
3 months ago
BlocksWpQuery.php
2 years ago
CartCheckoutUtils.php
4 weeks ago
MiniCartUtils.php
1 year ago
ProductAvailabilityUtils.php
11 months ago
ProductDataUtils.php
1 year ago
ProductGalleryUtils.php
4 weeks ago
StyleAttributesUtils.php
1 year ago
Utils.php
2 years ago
ProductAvailabilityUtils.php
47 lines
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | namespace Automattic\WooCommerce\Blocks\Utils; |
| 4 | |
| 5 | use Automattic\WooCommerce\Blocks\Templates\ProductStockIndicator; |
| 6 | use Automattic\WooCommerce\Enums\ProductType; |
| 7 | |
| 8 | /** |
| 9 | * Utility functions for product availability. |
| 10 | */ |
| 11 | class ProductAvailabilityUtils { |
| 12 | |
| 13 | /** |
| 14 | * Get product availability information. |
| 15 | * |
| 16 | * @param \WC_Product $product Product object. |
| 17 | * @return string[] The product availability class and text. |
| 18 | */ |
| 19 | public static function get_product_availability( $product ) { |
| 20 | $product_availability = array( |
| 21 | 'availability' => '', |
| 22 | 'class' => '', |
| 23 | ); |
| 24 | |
| 25 | if ( ! $product ) { |
| 26 | return $product_availability; |
| 27 | } |
| 28 | |
| 29 | $product_availability = $product->get_availability(); |
| 30 | |
| 31 | // If the product is a variable product, make sure at least one of its |
| 32 | // variations is purchasable. |
| 33 | if ( |
| 34 | isset( $product_availability['class'] ) && |
| 35 | ( 'in-stock' === $product_availability['class'] || 'available-on-backorder' === $product_availability['class'] ) && |
| 36 | ProductType::VARIABLE === $product->get_type() |
| 37 | ) { |
| 38 | if ( ! $product->has_purchasable_variations() ) { |
| 39 | $product_availability['availability'] = __( 'Out of stock', 'woocommerce' ); |
| 40 | $product_availability['class'] = 'out-of-stock'; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return $product_availability; |
| 45 | } |
| 46 | } |
| 47 |