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;
}
Related
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!
I'm building a website where i'll sell some fine art prints and i would like to set an automated discount when a customer buy the first print of a serie. Each of my prints are limited to 15 pieces and i would like to set a 30% discount for the first sell (#1/15) of each series. Then, for the next prints (#2 to #15), price goes back to normal.
Edit :
I did progress on my problem ! I did some research and tried a lot of different things and i found how to set the custom price i wanted on my product page (regular price + discount price) with the rule to only apply it if the related item available stock is 15.
regular price + discount price
And here's the code is used :
// Generating the product "regular price"
add_filter( 'woocommerce_product_get_regular_price', 'dynamic_regular_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_regular_price', 'dynamic_regular_price', 10, 2 );
function dynamic_regular_price( $regular_price, $product ) {
$stock_qte = $product->get_stock_quantity();
if( empty($regular_price) || $regular_price == 0 )
return $product->get_price();
else
return $regular_price;
}
// Generating the product "sale price"
add_filter( 'woocommerce_product_get_sale_price', 'dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'dynamic_sale_price', 10, 2 );
function dynamic_sale_price( $sale_price, $product ) {
$stock_qte = $product->get_stock_quantity();
if( $stock_qte == '15')
return $product-> get_regular_price() * 0.7;
else
//return $product->get_regular_price();
return $regular_price;
};
// Displayed formatted regular price + sale price
add_filter( 'woocommerce_get_price_html', 'dynamic_sale_price_html', 20, 2 );
function dynamic_sale_price_html( $price_html, $product ) {
$stock_qte = $product->get_stock_quantity();
if( $stock_qte == '15')
$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();
else
$price_html = wc_price(wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ). $product->get_price_suffix());
return $price_html;
}
It's almost working but the code related to the cart values updates is not fully working. The cart sub-total is good and take count of the discount but the individual price of each product is not updated and shown as without discount. It's the same on the cart page.
cart (from mini cart)
cart (cart page)
add_action( 'woocommerce_before_calculate_totals', 'alter_price_cart', 9999 );
function alter_price_cart( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
// IF CUSTOMER NOT LOGGED IN, DONT APPLY DISCOUNT
//if ( ! wc_current_user_has_role( 'customer' ) ) return;
// LOOP THROUGH CART ITEMS & APPLY 30% DISCOUNT
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$price = $product->get_price();
$cart_item['data']->set_price( $price * 0.7 );
}
}
I'll also have to add the stock availability logic for the mini-cart / cart / checkout page but i'll do that in a second time.
I must have made some mistakes in the loop throught the cart but i cannot catch the issue :( Any idea about how to hook the price of each product and change the displayed value ?
Have a nice day,
Quentin
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;
}
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;
}
Using woocommerce, I have several categories but would like to add "per person" to the category "cookery classes".
I have written this function, which works on all products.
I can't seem to get it to work only on the category.
'/*---------Adds per person to price -------*/
add_filter( 'woocommerce_get_price_html', 'custom_price_html', 500, 2 );
function custom_price_html( $price, $product ) {
if (( int ) is_category( 'Cookery Classes','product_cat', $product->id ) )
$price .='per person';
$price .= '<span class="from"><BR>' . _x('per person', 'price_per_person','woocommerce') . ' </span>';
return $price;
}'
found and installed this plugin:
https://wordpress.org/plugins/woocommerce-jetpack/
Allows you to add custom labels to prices per product catagory