WC_Product_Variable::has_purchasable_variationspublicWC 10.0.0

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

Method of the class: WC_Product_Variable{}

No Hooks.

Returns

true|false.

Usage

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

Changelog

Since 10.0.0 Introduced.

WC_Product_Variable::has_purchasable_variations() code WC 10.7.0

public function has_purchasable_variations() {
	$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() ) {
			continue;
		}

		// We found at least one available variation, so return true.
		return true;
	}

	// There were either no variations, or they were hidden because of the "continues" above.
	return false;
}