Via the following hook I added the text to the total prices:
add_filter( 'woocommerce_cart_totals_order_total_html', 'custom_total_message_html', 10, 1 );
function custom_total_message_html( $value ) {
$text_to_add_before_price = 'excl. BTW '; //change text in quotes to your preferred text
return $text_to_add_before_price . $value;
}
Only I see that on the thank-you page it is not shown in the table. Is it possible to add text only for the price in the row 'total'?
In the example it is: € 20.35 but this should be € 20.35 excl. VAT. With the current hook this is only done in the shopping cart and the checkout page
For orders (and email notifications) use the following to add a custom text before order total line:
add_filter( 'woocommerce_get_order_item_totals', 'custom_order_total_line_html', 1000, 3 );
function custom_order_total_line_html( $total_rows, $order, $tax_display ){
$total_rows['order_total']['value'] = __('excl. BTW ') . ' ' . $total_rows['order_total']['value'];
return $total_rows;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
To add it after instead use:
add_filter( 'woocommerce_get_order_item_totals', 'custom_order_total_line_html', 1000, 3 );
function custom_order_total_line_html( $total_rows, $order, $tax_display ){
$total_rows['order_total']['value'] .= ' ' . __('excl. BTW ');
return $total_rows;
}
Related
I want to add text before the price on the WooCommerce product catalogue only, which I have working using this code in functions.php:
// Add text before price
function bd_rrp_price_html( $price, $product ) {
$return_string = 'Rent from: ' . $price;
return $return_string;
}
add_filter( 'woocommerce_get_price_html', 'bd_rrp_price_html', 100, 2 );`
However, the above function is also adding the text before the price on the product detail page, which I do not want...
What would I need to change in the function to get it so it only displays on the product catalogue?
Thanks.
You have just to inverted the price and the text:
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
function cw_change_product_price_display( $price ) {
// Your additional text in a translatable string
$text = __('TEXT');
// returning the text before the price
return $text . ' ' . $price;
}
I have a WooCommerce store and am using the 'Advanced Dynamic Pricing for Woocommerce' plugin. I need to use this plugin because I have discount structures set up
Percentage discounts have been applied to all products in store. The original price is crossed out with the new price next to it.
enter image description here
I want to display a suffix next to the new price that says 'inc VAT' to indicate that the price is inclusive of VAT.
I have tried this code which seems to work on products that don't have discounts applied but not on discounted products.
add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );
function custom_price_suffix( $price, $product ){
$price = $price . 'inc VAT' ;
return apply_filters( 'woocommerce_get_price', $price );}
Anyone know how I can achieve this?
The generated HTML code looks like this:
<div class="woosg-price">
<div class="woosg-price-ori">
<del>
<span class="woocommerce-Price-amount amount">
<span class="woocommerce-Price-currencySymbol">£</span>262.00</span>
</del>
<ins><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-
currencySymbol">£</span>117.90</span></ins>
</div>
<div class="woosg-price-new"></div>
</div>
You can use woocommerce_get_price_suffix dedicated hook, targeting on sale products like:
add_filter( 'woocommerce_get_price_suffix', 'custom_price_suffix', 999, 4 );
function custom_price_suffix( $html, $product, $price, $qty ){
if ( $product->is_on_sale() ) {
return ' ' . __('inc VAT', 'woocommerce');
}
return $html;
}
or maybe this instead (because of Advanced Dynamic Pricing for Woocommerce plugin):
add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 999, 2 );
function custom_price_suffix( $price_html, $product ){
if ( $product->is_on_sale() ) {
$price_html .= ' ' . __('inc VAT', 'woocommerce');
}
return $price_html;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Related:
Show Price Suffix only on all WooCommerce Product loops
Show a price suffix only on all WooCommerce single products
You Can add Suffix using the below filter.
add_filter( 'woocommerce_get_price_suffix', 'swt_add_price_suffix', 99, 4 );
function swt_add_price_suffix( $html, $product, $price, $qty ){
$html .= ' inc VAT';
return $html;
}
Or You can use below code as well.
add_filter( 'woocommerce_get_price_html', 'swt_add_price_suffix', 99, 2 );
function swt_add_price_suffix( $price, $product ){
$price = $price.' inc VAT';
return $price;
}
You can add suffix from the WooCommerce settings using the below steps.
Goto WooCommerce -> Settings -> General.
Mark checked "Enable tax rates and calculations" checkbox.
Open the Tax Tab.
Add the suffix text in "Price display suffix" text field.
Save the settings.
I have a custom function that checks if a checkbox is checked and if so, it adds 'with vat relief' next to the price. If it isn't checked, it adds 'inc vat' next to the price. That works fine and my code is:
add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
function conditional_price_suffix( $price, $product ) {
$isTaxRelefe = get_post_meta($product->id, 'disability_exemption', true);
if ($isTaxRelefe == 'yes')
$price .= ' ' . __('with vat relief');
else $price .= ' ' . __('inc vat');
return $price;
}
What I need to do now is add another function targeting the checkout page that says if the checkbox is checked show some text underneath the product title but I'm struggling. My initial thought was to edit the /checkout/review-order so I added an if else statement to output something next to the product title is. I added:
$isTaxRelefe = get_post_meta($product->id, 'disability_exemption', true);
if ($isTaxRelefe == 'yes') {
$content .= 'VAT RELIEF AVAILABLE';
}
but this does nothing, I have tried various variations, changing to echo statements etc. but no luck. I'm sure I am just writing this incorrectly. Can anyone advise? What I'm not very up on are WordPress functions as in if I could write one to target the checkout page only, Im not sure how it determines where to output your. an if else statement seemed like the obvious choice but not having any luck.
Your code is a bit outdated and you should use $product->get_id() since Woocommerce 3 in your first function instead of $product->id in the get_post_meta() function.
You can also use instead the WC_Data method get_meta() from the product object directly.
Below is your revisited code with the additional hooked function that will display conditionally "VAT RELIEF AVAILABLE" text under the product title in checkout page: (without overriding the template review-order.php)
add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
function conditional_price_suffix( $price, $product ) {
if ( $product->get_meta('disability_exemption') === 'yes')
$price .= ' ' . __('with vat relief');
else
$price .= ' ' . __('inc vat');
return $price;
}
add_filter( 'woocommerce_checkout_cart_item_quantity', 'custom_text_below_checkout_product_title', 20, 3 );
function custom_text_below_checkout_product_title( $quantity_html, $cart_item, $cart_item_key ){
if ( $cart_item['data']->get_meta('disability_exemption') === 'yes' )
$quantity_html .= '<br>' . __('VAT RELIEF AVAILABLE');
return $quantity_html;
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
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' );
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.
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.
I'm trying to find a way to place custom text before availability info (out of stock, in stock etc.) on woocommerce single product page.
I am using something like that:
add_filter( 'woocommerce_get_availability', 'change_product_availability_display' );
add_filter( 'unknown filter', 'change_product_availability_display' );
function change_product_availability_display( $availability ) {
// Additional text
$text = __('Availability:');
// returning the text before the availability
return $text . ' ' . $availability;
}
Its's based on: Add a custom text before the price display in WooCommerce.
In case of price filter named in my code "unknown filter" was woocommerce_cart_item_price. I've looked for this kind of filter for availability/stock item, but can't find it.
Maybe someone could review this code and help me to find this "unknown_filter" or have other idea how I can put custom text before availability info?
I have revisited your code as your are trying to merge a string with an array. Also I have added a 2nd hooked function that will allow to display your "labeled" availability to items in cart and checkout.
The code:
add_filter( 'woocommerce_get_availability', 'add_label_to_availability_display' );
function add_label_to_availability_display( $availability ) {
if( is_product() || is_cart() || is_checkout() ){
$label = __( 'Availability', 'woocommerce' ) . ': ';
$availability['availability'] = $label . $availability['availability'];
}
return $availability;
}
add_filter( 'woocommerce_cart_item_name', 'add_availability_below_cart_item_name', 10, 3);
function add_availability_below_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
$availability = $cart_item['data']->get_availability();
return $item_name . '<br>' . $availability['availability'];
}
Code goes in function.php file of your active child theme (active theme).
Tested and works.