In WooCommerce use the code below to add a custom label after product prices in simple and variables products:
add_filter('woocommerce_variation_price_html','prices_custom_labels', 10, 2 );
add_filter('woocommerce_price_html','prices_custom_labels', 10, 2 );
function prices_custom_labels( $price, $product ){
// Set HERE your custom labels names
$per_dozen = ' '. __('per dozen', 'woocommerce' );
$per_case = ' '. __('per case (20 dozens)', 'woocommerce' );
// 1) Variable products
if ($product->product_type != 'simple' && $product->variation_id ) {
// Getting the array of existing attributes values for a variation
$variation_attribute_value = $product->variation_data;
// Here we keep only the last value in this array
$last_variation_attribute_slug_value = ' ' . end($variation_attribute_value);
// Finding the word 'case' in the attribute value slug
$has_case = strstr($last_variation_attribute_slug_value, 'case');
// Setting the right displayed label depending on attribute value slug
if( $has_case )
$attribute_quantity_name_value = $per_case;
else
$attribute_quantity_name_value = $per_dozen;
// Here the output price + custom label
$price = '<ins class="highlight">'.woocommerce_price( $product- >regular_price ).$attribute_quantity_name_value.'</ins>';
}
// 2) Simple products
else
{
// Here the output price + custom default label
$price = '<ins class="highlight">'.woocommerce_price( $product- >regular_price ).$per_dozen.'</ins>';
}
return $price;
}
But in variable products, I have an issue with the appended custom label in the live displayed price. The code that I use only display after the live price "per dozen".
I will need to get the selected value on the custom "quantity" selector to add the right label after the price:
- If the selected value is "Dozen" I need to display after live price " per dozen",
- If the selected value is "Case (20 dozens)" I need to display after live price " per case (20 dozens)".
This screenshot is what I have actually for all cases:
Check this issue on my web site specific product page
So I would need to get the attribute "quantity" selected value to append the right label to the live price.
Any help? How can I do to get that working?
I have tried many code, and I can't get it working.
See Question&Answers more detail:os