Explore Variations with Prices for Woocommerce products
Explore our WordPress category, where you’ll find loads of helpful articles and tutorials for everyone, whether you’re just starting out or a pro developer. Learn about different WordPress themes and plugins that can be applied in various projects according to their specific requirements.
This function, display_variation_price, is designed to display variation prices for products in a specific category (‘drinks_on’ category).
// Hook the display_variation_price function to the WooCommerce action ‘woocommerce_after_shop_loop_item’ with a priority of 5
add_action( ‘woocommerce_after_shop_loop_item’, ‘display_variation_price’, 5 );
// Define the display_variation_price function
function display_variation_price() {
global $product;
// Check if the product belongs to the ‘drinks_on’ category
if ( has_term( ‘drinks_on’, ‘product_cat’, $product->get_id() ) ) {
// replace ‘category-slug’ with the slug of your desired category
}
// replace ‘category-slug’ with the slug of your desired category
// Check if the product is of type ‘variable’
if ( $product->is_type( ‘variable’ ) ) {
} // Get the available variations for the variable product
$variations = $product->get_available_variations();
// Loop through each variation
foreach ( $variations as $variation ) {
$variation_id = $variation[‘variation_id’];
}$variation_id = $variation[‘variation_id’];
// Create a new instance of the WC_Product_Variation class
$variation_obj = new WC_Product_Variation( $variation_id );
// Get the price of the variation
$price = $variation_obj->get_price();
// Display the variation price along with the attribute ‘attribute_quantity’
echo ‘<div class=”variation-price”>’ . $variation[‘attributes’][‘attribute_quantity’] . ‘ – ‘ . wc_price( $price ) . ‘</div>’;// replace ‘attribute_pa_color’ with the attribute name you want to display
}