Dave
Dave

Reputation: 351

Adding custom text to the variation price in Woocommerce

I thought this would have been easy, but I am stuck. All I am trying to do is add the word each after the variation price on a product page. The solution I have found adds it on the category page and in two places on the product page.

The code is:

 /*  Adds a text Each - after price */
function change_product_price( $price ) {
    $price .= ' each';
    return $price;
}
add_filter( 'woocommerce_get_price_html', 'change_product_price' );

enter image description here

From the picture above, I only need the each added to the price above the add to cart button, but not the other pacles like the crossed out section in the photo above.

Thank you for any guidance you can provide.

Upvotes: 1

Views: 5142

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

The following code will add a suffix to the product variations price:

add_filter('woocommerce_available_variation', 'variation_price_custom_suffix', 10, 3 );
function variation_price_custom_suffix( $variation_data, $product, $variation ) {
    $variation_data['price_html'] .= ' <span class="price-suffix">' . __("each", "woocommerce") . '</span>';

    return $variation_data;
}

Code goes in function.php file of your active child theme (active theme). Tested and works.

Upvotes: 5

Related Questions