Is there any way to check the product has variation something like Please help me.
<?php
If (product has variation) {
echo"This Product have Variations ";
} else {
echo "This Product does not have Variations ";
}
?>
Is there any way to check the product has variation something like Please help me.
<?php
If (product has variation) {
echo"This Product have Variations ";
} else {
echo "This Product does not have Variations ";
}
?>
if ($product->is_type('simple')) {
// Simple product
} elseif ($product->is_type('grouped')) {
// Grouped product
} elseif ($product->is_type( 'external')) {
// External/Affiliate product
} elseif ($product->is_type('variable')) {
// Variable product
}
To get all variation ids of a variable product
$product = wc_get_product($product_id);
$variations = $product->get_available_variations();
$variations_id = wp_list_pluck( $variations, 'variation_id' );
The above code will provide visible variation ids only. For example, If the price is not set for a variation, that variation will be hidden.
Alternatively use the below code to get all variations without considering visibility.
$product = wc_get_product($product_id);
$current_products = $product->get_children();
This should work:
Example: If you will replace the file woocommerce --> single-product --> meta.php with this code, you will see it works.