// Add variation price next to attribute dropdown options
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'custom_variation_price_in_dropdown', 10, 2 );
function custom_variation_price_in_dropdown( $html, $args ) {
global $product;
if ( empty( $args['options'] ) || ! $product instanceof WC_Product ) {
return $html;
}
$options = $args['options'];
$attribute = $args['attribute'];
$html = '';
return $html;
}
// Helper function to get the variation price HTML
function get_the_variation_price_html( $product, $attribute_name, $option_value ) {
if ( ! $product->is_type( 'variable' ) ) {
return '';
}
foreach ( $product->get_available_variations() as $variation ) {
$attributes = $variation['attributes'];
if ( isset( $attributes[ 'attribute_' . $attribute_name ] ) && $attributes[ 'attribute_' . $attribute_name ] === $option_value ) {
$variation_product = wc_get_product( $variation['variation_id'] );
if ( $variation_product ) {
return $variation_product->get_price_html();
}
}
}
return '';
}
Cart - Mocerra
Skip to content
NEWSLETTER SIGNUP
Subscribe to our newsletter and unlock 10% off your next purchase.
P.S. It stacks with our Buy 2, Save 20% storewide offer!