WC_Product_Variable::has_purchasable_variations │ public │ WC 10.0.0

Check if there are variations that can be purchased for the current product.

Метод класса: WC_Product_Variable{}

Хуков нет.

Возвращает

bool.

Использование

$WC_Product_Variable = new WC_Product_Variable();
$WC_Product_Variable->has_purchasable_variations();

Список изменений

С версии 10.0.0 Введена.

Код WC_Product_Variable::has_purchasable_variations() WC 11.3.0

public function has_purchasable_variations() {
	/**
	 * If you are evaluating performance, introducing a transient is not a viable solution.
	 *
	 * - The transient improves the 95th percentile (P95) load time of the product page by approximately 10%, but does not affect the median.
	 * - The transient breaks backward compatibility. The woocommerce_is_purchasable filter from \WC_Product::is_purchasable is used by
	 *   extensions to control product purchasability based on user role, membership, geolocation, or login status.
	 *
	 * Meta-based optimization (skipping out-of-stock variations) was evaluated and dismissed due to impact to complexity ratio.
	 */
	$has_purchasable_variations = false;
	$variation_ids              = $this->get_children();
	if ( ! empty( $variation_ids ) ) {
		// Prime caches to reduce future queries.
		_prime_post_caches( $variation_ids );

		foreach ( $variation_ids as $variation_id ) {
			$variation = wc_get_product( $variation_id );
			if ( $variation && $variation->is_purchasable() && $variation->is_in_stock() ) {
				$has_purchasable_variations = true;
				break;
			}
		}
	}

	return $has_purchasable_variations;
}