Text before price on WooCom - php

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;
}

Related

woocommerce automatically adding a word before price

The prices in my woocommerce store have the words "starting from" automatically added in front of them and I want to remove these words.
This happens on all products both variable and regular products. I need a code that will allow me to completely remove the words that come before the price.
screen shots:
screen shot - regular product
screen shot - variable product
Thanks.
To remove "starting from" prefix from displayed prices, try the following code in function.php file of your active child theme (or active theme).
add_filter( 'woocommerce_get_price_html', 'filter_get_price_html_callback', 10, 2 );
function filter_get_price_html_callback( $price, $product ) {
return str_replace( __('starting from'), '', $price );
}
Similarly for single product pages:
add_filter( 'woocommerce_get_price_html', 'filter_get_price_html_callback', 10, 2 );
function filter_get_price_html_callback( $price, $product ) {
if ( is_product() )
$price = str_replace( __('starting from'), '', $price );
return $price;
}
Note: Please replace 'starting from' according to your need.

Add a text to WooCommerce orders displayed total price

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;
}

Display custom price suffix on WooCommerce discounted products

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.

Add regular price before price in WooCommerce

I'm using the Multi-Currency plugin for WooCommerce. The prices are now converted to MKD Denars. Now I want to show a regular price (USD) before price in (MKD).
Here is my code in Function.php
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 ) {
$text = 'echo $product->get_regular_price;';
// returning the text before the price
return $text . ' ' . $price;
}
Can someone help me use the correct syntax?
You can't use echo inside quotes as it is interprated as as common text string.
So your function could look like:
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 ) {
global $product;
// display regular price before the price
return $product->get_regular_price . ' ' . $price;
}
You can also format the price using wc_price() function.

Add a custom text to Woocommerce product availability and display it for cart items

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.

Categories