Add regular price before price in WooCommerce - php

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.

Related

Text before price on WooCom

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

WooCommerce cart items price suffix for specific product category

I need to add a suffix text to the price of a woocommerce product from a specific category while on WooCommerce cart.
Here is my code:
add_filter( 'woocommerce_cart_item_price', 'filter_woocommerce_cart_item_price', 10, 3 );
add_filter( 'woocommerce_cart_item_subtotal', 'filter_woocommerce_cart_item_price', 10, 3 );
function filter_woocommerce_cart_item_price( $wc, $cart_item, $cart_item_key ) {
if(!in_array('67',$cart_item['data']->get_category_ids()))
{
return $wc;
}
else{
return $wc . ' Monthly';
}
};
The problem is it only works on simple products. Doesn't seem to affect the prices of variable products (I tested on 3 different sites).
Any idea what I'm missing?
To make it work for product variations items in cart too, use instead the following:
add_filter( 'woocommerce_cart_item_price', 'cart_item_amounts_prefix', 10, 3 );
add_filter( 'woocommerce_cart_item_subtotal', 'cart_item_amounts_prefix', 10, 3 );
function cart_item_amounts_prefix( $amount, $cart_item, $cart_item_key ) {
if( has_term( array(67), 'product_cat', $cart_item['product_id'] ) ){
$amount .= ' ' . __("Monthly", "woocommerce");
}
return $amount;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.

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.

Set product sale price programmatically in WooCommerce 3

I have a Woocommerce store set up with various product categories.
I want to apply a 20% discount to all products, which belong to the product category Cuckoo
For now all I'm trying to achieve is set a sale price in my functions.php
It tried as follows:
/*
* For a specific date, 20% off all products with product category as cuckoo clock.
*/
function cuckoo_minus_twenty($sale_price, $product) {
$sale_price = $product->get_price() * 0.8;
return $sale_price;
};
// add the action
add_filter( 'woocommerce_get_sale_price', 'cuckoo_minus_twenty', 10, 2 );
If I var_dump the result of $sale_price after the calculation I get the correct answer, however the price display on the front-end strikes out the regular price and displays the sale price as the regular price.
Is there a hook/filter I can use to achieve this?
I've also tried setting the sale price by doing:
$product->set_sale_price($sale_price);
to no avail.
The hook woocommerce_get_sale_price is deprecated since WooCommerce 3 and replaced by woocommerce_product_get_sale_price.
Also Product displayed prices are cached. When sale price is active, regular price is also active.
Try this instead:
// Generating dynamically the product "regular price"
add_filter( 'woocommerce_product_get_regular_price', 'custom_dynamic_regular_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_regular_price', 'custom_dynamic_regular_price', 10, 2 );
function custom_dynamic_regular_price( $regular_price, $product ) {
if( empty($regular_price) || $regular_price == 0 )
return $product->get_price();
else
return $regular_price;
}
// Generating dynamically the product "sale price"
add_filter( 'woocommerce_product_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
function custom_dynamic_sale_price( $sale_price, $product ) {
$rate = 0.8;
if( empty($sale_price) || $sale_price == 0 )
return $product->get_regular_price() * $rate;
else
return $sale_price;
};
// Displayed formatted regular price + sale price
add_filter( 'woocommerce_get_price_html', 'custom_dynamic_sale_price_html', 20, 2 );
function custom_dynamic_sale_price_html( $price_html, $product ) {
if( $product->is_type('variable') ) return $price_html;
$price_html = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ), wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ) . $product->get_price_suffix();
return $price_html;
}
Code goes in function.php file of your active child theme (active theme).
Tested and works on single product, shop, product category and tag archive pages.
The continuation in:
Wrong Woocommerce cart item price after setting programmatically product sale price
I realiced, you more or less need all of the following filters to make the HTML work out of the box.
add_filter( 'woocommerce_product_get_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_variation_prices_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_variation_prices_sale_price', 'custom_dynamic_sale_price', 10, 2 );
And you need to make sure you've changed the woocommerce_get_variation_prices_hash becasue of the stored transients if you want to display it correctly for variable products.
You may find the gist i've created for a client useful
https://gist.github.com/xandl/743fb6af60827eb95ad42b20b478b020
use woocomerce_get_sale_price filter.
add_filter('woocommerce_get_sale_price', 'my_custom_price', 99, 2);
add_filter('woocommerce_get_price', 'my_custom_price', 99, 2);
function my_custom_price( $price, $product )
{
//your logic for calculating the new price here
$price = $product->get_regular_price() * 0.8;
//Return the new price (this is the price that will be used everywhere in the store)
return $price;
}

Categories