Change the way WooCommerce discount sale prices are displayed - php

Good afternoon friends, I will be brief; I am trying to customize the display of discounted product prices as shown in the following image:
Example
Now, I have already obtained several codes and I am modifying them to achieve that result. It works well in most products, but in variable products it is something else. Variable products that have a discount stop working, In products that have variations it gives error when a variation has a discount, the product stops working. Could you please help me? I would appreciate!
I have attached the code I have.
/* Text before and after the discounted price */
add_filter('woocommerce_format_sale_price', 'filter_function_name', 10, 3);
function filter_function_name($price, $regular_price, $sale_price) {
$product = new WC_Product(get_the_ID());
if (!is_admin() && $product->is_type('simple')) {
$regular_price = 'Before: ' . wc_price($regular_price);
$sale_price = 'After: ' . wc_price($sale_price);
$price = $regular_price . ' - ' . $sale_price;
}
return $price;
};
/*---------- Showing the saving message -----------*/
function ts_you_save() {
global $product;
if ($product->is_type('simple') || $product->is_type('external') || $product->is_type('grouped')) {
$regular_price = get_post_meta($product->get_id(), '_regular_price', true);
$sale_price = get_post_meta($product->get_id(), '_sale_price', true);
if (!empty($sale_price)) {
$amount_saved = $regular_price - $sale_price;
$currency_symbol = get_woocommerce_currency_symbol();
$percentage = round((($regular_price - $sale_price) / $regular_price) * 100);
?>
<p style="font-size:24px;color:red;"><b>You Save: <b>$</b><?php echo number_format($amount_saved, 2, '.', '') . " (" . number_format($percentage, 0, '', '') . "%)"; ?></b></p>
<?php
}
}
}
add_action('woocommerce_single_product_summary', 'ts_you_save', 11);
Thank you very much for your time, I await your response :)

Related

Error displaying discount percentage on WooCommerce category archive and product single page

WooCommerce discount percentage exposure has been customized.
add_filter( 'woocommerce_get_price_html', 'custom_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'custom_price_format', 10, 2 );
function custom_price_format( $price, $product ) {
// Main Price
$regular_price = $product->is_type('variable') ? $product->get_variation_regular_price( 'min', true ) : $product->get_regular_price();
$sale_price = $product->is_type('variable') ? $product->get_variation_sale_price( 'min', true ) : $product->get_sale_price();
if ( $regular_price !== $sale_price && $product->is_on_sale()) {
// Percentage calculation and text
$percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%↓';
$percentage_txt = $percentage;
$price = '<ins>' . wc_price($sale_price) . '</ins> <span class="sale-perc">' . $percentage_txt . '</span> <del>' . wc_price($regular_price) . '</del> ';
}
return $price;
}
Discount percentage exposure is good on category archive pages and product single pages.
However, a fatal error occurs when accessing a single page for a product without a discount rate.
"There is a fatal error on this website."
Are there any parts of the code that are incorrect?
please answer about my question.
best regards

Calculate tax in onsale badge amount

I would need your help, if there is someone willing to help me, I would greatly appreciate it. In my website prices include a 10.0000% tax.
I want to calculate in my onsale badge my tax from woocommerce which is of 10.0000%. This is my code:
Badge: onsale badge from site
add_filter( 'woocommerce_sale_flash', 'add_amount_to_sale_badge', 20, 3 );
function add_amount_to_sale_badge( $html, $post, $product ) {
if( $product->is_type('variable')){
$amount_off = array();
$prices = $product->get_variation_prices();
foreach( $prices['price'] as $key => $price ){
if( $prices['regular_price'][$key] !== $price ){
$amount_off[] = $prices['regular_price'][$key] - $prices['sale_price'][$key];
}
}
$amount_off_s = "up to -$" . round(max($amount_off));
} else {
$regular_price = (float) $product->get_regular_price();
$sale_price = (float) $product->get_sale_price();
$amount_off_s = "-$" . ($regular_price - $sale_price);
}
return '<span class="onsale">' . esc_html__( 'Save', 'woocommerce' ) . ' ' . $amount_off_s . '</span>';
}
my knowledge in php is limited and for this reason I am unable to solve this problem.

Manipulate woocommerc price in cart

I try to manipulate the calculated price in cart but with no luck...
I hope someone can help me here.
I have found this article and implemented the code as written on the Page which is to 98% exactly what i searched for. I need to add a cart calculation if the price type is per 100g.
So this is the actual working code:
add_filter( 'woocommerce_get_price_html', 'wb_change_product_html', 10, 2 );
// Adding a custom field to the price markup
function wb_change_product_html( $price, $product ) {
//$wb_price_type = get_field('product_price_type');
$wb_price_type = get_post_meta( $product->get_id(), 'product_price_type', true);
if($wb_price_type) {
$price_html = '<span class="amount">' . $price . ' ' . $wb_price_type . '</span>';
}
else {
$price_html = '<span class="amount">' . $price . '</span>';
}
return $price_html;
}
add_filter( 'woocommerce_cart_item_price', 'wb_change_product_price_cart', 10, 3 );
// Adding a custom field to the price in the cart
function wb_change_product_price_cart( $price, $cart_item, $cart_item_key ) {
//$wb_price_type = get_field( 'product_price_type', $cart_item['product_id'] );
$wb_price_type = get_post_meta( $cart_item['product_id'], 'product_price_type', true );
if ($wb_price_type) {
$price = $price . ' ' . $wb_price_type;
}
else {
$price = $price;
}
return $price;
}
add_filter( 'woocommerce_checkout_cart_item_quantity', 'wb_checkout_review', 10, 3 );
// Adding a custom field to the price in the checkout items
function wb_checkout_review ( $quantity, $cart_item, $cart_item_key ) {
//$wb_price_type = get_field( 'product_price_type', $cart_item['product_id'] );
$wb_price_type = get_post_meta( $cart_item['product_id'], 'product_price_type', true);
if ( $wb_price_type ) {
$cart_item = ' ' . sprintf( '× %s ', $cart_item['quantity'] ) . $wb_price_type . '';
}
else {
$cart_item = ' ' . sprintf( '× %s', $cart_item['quantity'] ) . '';
}
return $cart_item;
}
Well i thought its really easy to calculate so i do something like this:
add_filter( 'woocommerce_cart_item_price', 'wb_change_product_price_cart', 10, 3 );
// Adding a custom field to the price in the cart
function wb_change_product_price_cart( $price, $cart_item, $cart_item_key ) {
//$wb_price_type = get_field( 'product_price_type', $cart_item['product_id'] );
$wb_price_type = get_post_meta( $cart_item['product_id'], 'product_price_type', true );
if ($wb_price_type) {
if ($wb_price_type == "per 100g") {
$price = $price / 100 . ' ' . $wb_price_type;
}
else {
$price = $price . ' ' . $wb_price_type;
}
}
else {
$price = $price;
}
return $price;
}
But this didnt work... i too try to manipulate the price directly but i get anytime 0 in the value of the price...
so i google a little bit more and found this article, which makes in the end exactly what i want. in the last print screen we can see that the price will displayed per kg (which is completely fine) and in the calculation it uses an other price (which i try to do with the code above).
i think that the problem is that $price has for example the value "20$" and this is a string and i cannot calculate withe a string. And it makes no sense to split this string there should be an other way.
For better understanding here a picture. in the upper part we see the working calculation (which is good) but this is not exactly what i need. in the part down we see that it will only calculate on the calculated price (right side). the left side of the downer part remain with the correct price.
i think that the problem is that $price has for example the value "20$" and this is a string and i cannot calculate withe a string. And it makes no sense to split this string there should be an other way.
This is true, it's actually a full html string. Try changing the woocommerce_cart_item_price filter to get the price out of string:
add_filter('woocommerce_cart_item_price', 'wb_change_product_price_cart', 10, 3);
// Adding a custom field to the price in the cart
function wb_change_product_price_cart($price, $cart_item, $cart_item_key)
{
$wb_price_type = get_post_meta($cart_item['product_id'], 'product_price_type', true);
if ($wb_price_type) {
if ($wb_price_type == "per 100g") {
preg_match("/([0-9]+\.[0-9]+)/", $price, $matches);
$_price = $matches[0];
$per100 = $_price / 100;
$price = $per100 . ' ' . $wb_price_type;
} else {
$price = $price . ' ' . $wb_price_type;
}
} else {
$price = $price;
}
return $price;
}

Woocommerce display price without tax on variable products

prices in my shop are displayed with tax and also I'm using {price_excluding_tax} suffix to display also price excluding tax. But there is a problem with variable products... The shop shows only prices including tax (and the old price in case of promotion) but it doesn't shows the price excluding tax. Prices for both variable products are the same. I tried to use this code:
https://tomjesch.com/display-woocommerce-products-with-and-without-tax/
But it doesn't work properly - it shows prices, but does not react to any price change in the woocommerce admin panel. So, it works fine only when adding a new product.
Thanks in advance...
My code:
function edit_price_display() {
$product = WC_Product( get_the_ID() );
$regular_price = $product ->regular_price;
$price = $product->price;
$price_incl_tax = $price + round( $price * ( 23 / 100 ), 2 );
$price_incl_tax = number_format( $price_incl_tax, 2, ",", "" );
$price = number_format( $price, 2, ",", "" );
$display_price = '<ins><span class="woocommerce-Price-amount amount">';
$display_price .= ' '.$price_incl_tax .'<span class="woocommerce-Price-currencySymbol"> zł</span>';
$display_price .= '</span></ins> ';
$display_price .= '<small class="woocommerce-price-suffix">(netto: <span class="woocommerce-Price-amount amount">'.$price .' <span class="woocommerce-Price-currencySymbol">zł</span></span>)</small>';
$display_price .= '';
$display_regular_price ='<del><span class="woocommerce-Price-amount amount">'.$regular_price .'<span class="woocommerce-Price-currencySymbol"> zł</span></span></del>';
if($product->is_on_sale()) {
echo $display_regular_price;
}
echo $display_price;
}
add_filter('woocommerce_variable_price_html', 'edit_price_display');
Bear in mind Woocommerce developers comment about that feature:
https://github.com/woocommerce/woocommerce/issues/14839
But if you need achive such reasult, you can do that in this way:
add_filter( 'woocommerce_get_price_html', 'my_price_prefix_suffix', 100, 2 );
function my_price_prefix_suffix( $price, $product ){
// To add suffix, go to /wp-admin/admin.php?page=wc-settings&tab=tax
if($product->is_type( 'variable' ) && $product->is_in_stock()){
$price = $price . '('.wc_price($product->get_variation_regular_price()* 1.23).' z Vat)';
}
return apply_filters( 'woocommerce_get_price', $price );
}
Code is tested an working as expected.

Woocommerce Show only one price for variable product on discount

Here is my WooCommerce website: sweetworldcandy.com
The issue is, in variable product price the least and the max
value is showing what I want is if the product is not on sale show the least value if it is on sale show the least value and the least value of offer price by adding a slash as delete tag
I shared this three images below for reference:
2020 Update (for Woocommerce 3+)
Replaced deprecated function woocommerce_price() by wc_price() since Woocommerce 3+:
add_filter('woocommerce_variable_sale_price_html', 'shop_variable_product_price', 10, 2);
add_filter('woocommerce_variable_price_html','shop_variable_product_price', 10, 2 );
function shop_variable_product_price( $price, $product ){
$variation_min_reg_price = $product->get_variation_regular_price('min', true);
$variation_min_sale_price = $product->get_variation_sale_price('min', true);
if ( $product->is_on_sale() && !empty($variation_min_sale_price)){
if ( !empty($variation_min_sale_price) )
$price = '<del class="strike">' . wc_price($variation_min_reg_price) . '</del>
<ins class="highlight">' . wc_price($variation_min_sale_price) . '</ins>';
} else {
if(!empty($variation_min_reg_price))
$price = '<ins class="highlight">'.wc_price( $variation_min_reg_price ).'</ins>';
else
$price = '<ins class="highlight">'.wc_price( $product->regular_price ).'</ins>';
}
return $price;
}
Original thread (before WooCommerce 3): The code below should do what you expect:
add_filter('woocommerce_variable_sale_price_html', 'shop_variable_product_price', 10, 2);
add_filter('woocommerce_variable_price_html','shop_variable_product_price', 10, 2 );
function shop_variable_product_price( $price, $product ){
$variation_min_reg_price = $product->get_variation_regular_price('min', true);
$variation_min_sale_price = $product->get_variation_sale_price('min', true);
if ( $product->is_on_sale() && !empty($variation_min_sale_price)){
if ( !empty($variation_min_sale_price) )
$price = '<del class="strike">' . woocommerce_price($variation_min_reg_price) . '</del>
<ins class="highlight">' . woocommerce_price($variation_min_sale_price) . '</ins>';
} else {
if(!empty($variation_min_reg_price))
$price = '<ins class="highlight">'.woocommerce_price( $variation_min_reg_price ).'</ins>';
else
$price = '<ins class="highlight">'.woocommerce_price( $product->regular_price ).'</ins>';
}
return $price;
}
The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.

Categories