Woo REST API output sale price wrong [duplicate] - wordpress

This question already has answers here:
Set product sale price programmatically in WooCommerce 3
(3 answers)
Closed 2 years ago.
I'm in big trouble, because on live site I just discovered that our API put strange output and due to that can not sync products with our ERP and CRM. I think the problem is in this piece of code but I can not figure out how to keep it because it works on frotnend but exclude it from API Json output?
//PROCENT SPARET PÅ SIMPLE PRODUCT
add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );
function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) {
// Getting the clean numeric prices (without html and currency)
$_reg_price = floatval( strip_tags($regular_price) );
$_sale_price = floatval( strip_tags($sale_price) );
// Percentage calculation and text
$percentage = round( ( $_reg_price - $_sale_price ) / $_reg_price * 100 ).'%';
$percentage_txt = '(' . __('-', 'woocommerce' ) . $percentage . ')';
$formatted_regular_price = is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price;
$formatted_sale_price = is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price;
echo '<del>' . $formatted_regular_price . '</del> <ins>' . $formatted_sale_price ."<span class='percentage_save'>". $percentage_txt . "</span>".'</ins>';
}
And problem in output is visible here
Does anybody have idea how to quickly fix that? Thanks in advance

/* you can user this hook function for above issue */
add_filter('woocommerce_product_get_sale_price', 'custom_price_func', 10, 2 );
function custom_price_func( $price, $product){
/* write your stuff here */
return $price;
}

Related

How to show the discount percentage in actual in woocommerce?

How to show the discount percentage in actual?
I have installed the YITH wooCommerce badge management plugin. but the discount % comes in round figure 50%
I want to show the actual discount % like this one 49.75
Regular = $8.50
Sale = 4.27
Discount in % 49.75
Kindly someone guide me on how to resolve this issue.
add this snippet to your functions.php
add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );
function change_displayed_sale_price_html( $price, $product ) {
// Only on sale products on frontend and excluding min/max price on variable products.
if ( $product->is_on_sale() && ! is_admin() && ! $product->is_type( 'variable' ) ) {
// Get product prices
$regular_price = (float) $product->get_regular_price(); // Regular price
$sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)
// "Saving price" calculation and formatting.
$saving_price = wc_price( $regular_price - $sale_price );
// "Saving Percentage" calculation and formatting.
$precision = 1; // Max number of decimals
$saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';
// Append to the formated html price.
$price .= sprintf( __( '<p class="saved-sale">Save: %1$s <em>(%2$s)</em></p>', 'woocommerce' ), $saving_price, $saving_percentage );
}
return $price;
}
You're ready. Enjoy!

Display 'you savex%' in woocommerce [duplicate]

This question already has an answer here:
Display the default discounted price and percentage on Woocommerce products
(1 answer)
Closed 2 years ago.
I have a Wocommerce store that has variable and simple products.
I don't want to show '% you save' save in simple products.
I am using "Display the default discounted price and percentage on Woocommerce products".
How to block discount % in simple products?
This should work for your case. This will exclude products that are 'simple' .
add_filter( 'woocommerce_available_variation', 'custom_variation_price_saving_percentage', 10, 3 );
function custom_variation_price_saving_percentage( $data, $product, $variation ) {
global $product;
$active_price = $data['display_price'];
$regular_price = $data['display_regular_price'];
if( $active_price !== $regular_price && !$product->is_type('simple') {
$saving_percentage = round( 100 - ( $active_price / $regular_price * 100 ), 1 ) . '%';
$data['price_html'] .= sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $saving_percentage );
}
return $data;
}

Multiple currencies in woocommerce not showing

I am newbie in Wordpress Plugin Development. I'm facing some issues. One currency payment displayed so how multiple currency payment will display.
Below code is showing one currency.
// Show product prices in fiat + crypto
function mcc_woocommerce_get_price_html( $text, $product )
{
// Is MCC installed?
if ( ! function_exists( 'mycryptocheckout' ) )
return $text;
// Retrieve all of our currencies.
$currencies = MyCryptoCheckout()->currencies();
// Change this to your preferred currency symbol
$show_currency = 'BCH';
$currency = $currencies->get( $show_currency );
// Is this currency known?
if ( ! $currency )
return $text;
$new_price = $currency->convert( get_woocommerce_currency(), $product->get_price() );
return $text . ' / ' . $new_price . ' ' . $show_currency;
}
add_filter( 'woocommerce_get_price_html', 'mcc_woocommerce_get_price_html', 100, 2 );

WooCommerce "Division By Zero" Error for Discount Percentage Text

I'm working on this code and while I've done my best to make it work, I cannot fix the "divided by zero" error that occurs on the archive (shop page) for grouped products.
This is the error message:
Warning: Division by zero in
That makes the percentage text show up like this: Save: -$18 (-INF%)
The error refers to this line:
$saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';
Here's the full code:
add_filter( 'woocommerce_get_price_html', 'display_sale_price_and_percentage_html', 10, 2 );
function display_sale_price_and_percentage_html( $price, $product ) {
// sale products on frontend excluding variable products
if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')) {
// product prices
$regular_price = (float) $product->get_regular_price(); // Regular price
$sale_price = (float) $product->get_price();
// price calculation and formatting
$saving_price = wc_price( $regular_price - $sale_price );
// percentage calculation and formatting
$precision = 1; // decimals
$saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';
// display the formatted html price including amount and precentage using a span tag which means displaying it on the same row, if you want this on a new row, change the tag into a paragraph
$price .= sprintf( __('<span class="saved-sale"> Save: %s <em>(%s)</em></span>', 'woocommerce' ), $saving_price, $saving_percentage );
}
return $price;
}
The error is shown on grouped products. It works fine on simple products. My goal is to make this work for all product types (simple, grouped, external and variable).
I need all the help I can get.
I believe it's because grouped products don't have a $regular_price. You should add some conditions to check if $sale_price and $regular_price are non-zero. You could also check that you aren't on a grouped product, but checking for 0 will also prevent divide by zero errors anywhere you have a free product.
add_filter( 'woocommerce_get_price_html', 'display_sale_price_and_percentage_html', 10, 2 );
function display_sale_price_and_percentage_html( $price, $product ) {
// sale products on frontend excluding variable products
if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')) {
// product prices
$regular_price = (float) $product->get_regular_price();
$sale_price = (float) $product->get_price();
if( $regular_price > 0 && $sale_price > 0 ) {
// price calculation and formatting
$saving_price = wc_price( $regular_price - $sale_price );
// percentage calculation and formatting
$precision = 1; // decimals
$saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';
// display the formatted html price including amount and precentage using a span tag which means displaying it on the same row, if you want this on a new row, change the tag into a paragraph
$price .= sprintf( __('<span class="saved-sale"> Save: %s <em>(%s)</em></span>', 'your-textdomain' ), $saving_price, $saving_percentage );
}
}
return $price;
}

How to override Woocommerce wc_format_sale_price function properly?

I am struggling with this task and I could really use some help. First, before somebody marks this as an off-topic, I already did read all the questions and answers here and other sites as well. No luck.
I am trying to edit the HTML output of the wc_format_sale_price function located in wc-formatting-functions.php.
Original code is:
function wc_format_sale_price( $regular_price, $sale_price ) {
$price = '<del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del> <ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</ins>';
return apply_filters( 'woocommerce_format_sale_price', $price, $regular_price, $sale_price );
As you can see the prices are encapsulated in HTML elements <del> and <ins>.
I did tried to change the HTML directly and it works perfectly.
function wc_format_sale_price( $regular_price, $sale_price ) {
$price = '<div id="priceBefore" style="font-size: 16px;" class="old-price">' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</div> <div id="priceAfter" style="font-size: 24px;" class="price">' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</div>';
return apply_filters( 'woocommerce_format_sale_price', $price, $regular_price, $sale_price );
The thing is I don't want to change WC core files because it is a bad practice and the changes will be removed every time the shop owner updates the WC plugin.
After some research I am sure that this should be done using filters in my theme's functions.php file but all the tutorials and articles about this functionality are quite messy. I did tried to follow few of them and I ended up with blank page, duplicate prices and stuff like that.
I understand that filters and actions are alpha and omega of Wordpress/Woocommerce theme development but my attempts to make them work were all just failures.
I actually found out how to solve this issue.
I did some more research and I found out this answer on Stack Overflow: https://stackoverflow.com/a/45112008/6361752 where the user called LoicTheAztec pointed out that woocommerce_format_sale_price hook accepts three arguments. So i added $price as the third argument to my filter function and now it works.
Final solution which I put into my theme's functions.php file looks like this:
add_filter('woocommerce_format_sale_price', 'ss_format_sale_price', 100, 3);
function ss_format_sale_price( $price, $regular_price, $sale_price ) {
$output_ss_price = '<div id="priceBefore" style="font-size: 16px;" class="old-price">' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</div> <div id="priceAfter" style="font-size: 24px;" class="price">' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</div>';
return $output_ss_price;
}
I am posting this answer just to make sure no more time is wasted on such a simple thing.
There is only one more thing I would like to know. How is it possible that the original function uses just two arguments and works flawlessly when my filter function needs to accept three arguments to work properly? Any ideas?

Resources