WC_Product_Variable::has_purchasable_variations
Check if there are variations that can be purchased for the current product.
Method of the class: WC_Product_Variable{}
No Hooks.
Returns
bool.
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() WC Product Variable::has purchasable variations code WC 11.1.2
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.
*/
$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;
}