Home › Forums › WoodMart support forum › Code Snippet to add Savings Amount and Savings Percentage to all products on SAL
Code Snippet to add Savings Amount and Savings Percentage to all products on SAL
- This topic has 15 replies, 2 voices, and was last updated 1 year, 2 months ago by
Aizaz Imtiaz Awan.
-
AuthorPosts
-
December 25, 2023 at 9:03 am #524371
filipopParticipantHi, would you have a code snippet / php function you may know would add a Saved Amount and a Savings Percentage to all products SIMPLE and VARIABLE ?
I tried to add a code snippet to display these on the product page (product layout I created) but it does not show.
ThanksDecember 25, 2023 at 2:44 pm #524477
Aizaz Imtiaz AwanKeymasterHello,
Try to add the following PHP code in the function.php of the child theme.
// For simple products add_action( 'woocommerce_single_product_summary', 'simple_product_saving_amount', 11 ); function simple_product_saving_amount() { global $product; if( $product->is_type('simple') && $product->is_on_sale() ) { $regular_price = (float) wc_get_price_to_display( $product, array('price' => $product->get_regular_price() ) ); $active_price = (float) wc_get_price_to_display( $product, array('price' => $product->get_sale_price() ) ); $saved_amount = $regular_price - $active_price; $percentage = round( $saved_amount / $regular_price * 100 ); echo '<p id="saving_total_price">'. __("You Save") .': ' . wc_price($saved_amount) . ' ('.$percentage.'%)</p>'; } } // For product variations (on variable products) add_filter( 'woocommerce_available_variation', 'variable_product_saving_amount', 10, 3 ); function variable_product_saving_amount( $data, $product, $variation ) { if( $variation->is_on_sale() ) { $saved_amount = $data['display_regular_price'] - $data['display_price']; $percentage = round( $saved_amount / $data['display_regular_price'] * 100 ); $data['price_html'] .= '<p id="saving_total_price">'. __("You Save") .': ' . wc_price($saved_amount) . ' ('.$percentage.'%)</p>'; } return $data; }
Best Regards.
December 26, 2023 at 1:12 pm #524617
filipopParticipantThank you for this. So far it worked but I will be monitoring this to make sure it covers all kinds of products.
Here is the screenshot
Attachments:
You must be logged in to view attached files.December 26, 2023 at 1:16 pm #524619
filipopParticipantCan I ask please … if I select a variation with a different price — that will also change in the price displayed ?
December 26, 2023 at 2:37 pm #524633
filipopParticipantThe problem we have now is the code you sent only displays the sale price, regular price, saved amount, savings percentage of the variation or one with the lowest price … if I click on other variations the price does not change … can you pls fix this. For all products if I select a different variation – the price should change
If I change the price of one variation .. it ends up displaying twice as in the screenshot … pls logon to fix the code. It looks like it is sending all to an array and calculating the minimum priced variation … please fix thanks
Attachments:
You must be logged in to view attached files.December 26, 2023 at 2:39 pm #524635
filipopParticipantIs this the array ? $data
$regular_price = $data[‘display_regular_price’];December 26, 2023 at 3:13 pm #524642
filipopParticipantAlso – when a product is not on Sale …. its price does not show at all can you also fix this in the code thanks
Attachments:
You must be logged in to view attached files.December 26, 2023 at 4:11 pm #524660
Aizaz Imtiaz AwanKeymasterHello,
Remove the previous code and add the following PHP code in the function.php of the child theme. This code is for both simple products and variable products.
// For product variations add_filter( 'woocommerce_available_variation', 'custom_variation_price_saving_percentage', 10, 3 ); function custom_variation_price_saving_percentage( $data, $product, $variation ) { $active_price = $data['display_price']; $regular_price = $data['display_regular_price']; if( $active_price !== $regular_price ) { $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; } // For simple products 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('simple') ){ // 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 Percentage" calculation and formatting $precision = 1; // Max number of decimals $saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), $precision ) . '%'; // Append to the formated html price $price .= sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $saving_percentage ); } return $price; }
Best Regards.
December 26, 2023 at 4:50 pm #524670
filipopParticipantThanks
Can you help me pls re-arrange the price and savings like this
(1) SALE Price (2) Crossed-REGULAR Price (3) Savings PERCENTAGE (4) Savings AMOUNTI wanted to move all that above description and move the (4) Savings AMOUNT to the RED graphic (on top of).
Attachments:
You must be logged in to view attached files.December 26, 2023 at 4:51 pm #524672
filipopParticipantAlso when the product that has variations is NOT on sale – right now the price does not show … can you help with this please maybe we need snippet to fix this thanks
December 26, 2023 at 5:30 pm #524679
filipopParticipantI added this to display price of variable product – that is not on SALE
else if ( !$product->is_on_sale() && ! is_admin() && $product->is_type(‘variable’) )
{
$data[‘price_html’] .= ‘<p id=”total_price”>‘. wc_price($regular_price) .’</p>’;
}December 26, 2023 at 5:31 pm #524680
filipopParticipantI now just need to move the price above description
December 27, 2023 at 9:41 am #524790
Aizaz Imtiaz AwanKeymasterHello,
Unfortunately, Such modification requires complicated Woocommerce code customization which is not covered by our support.
Hope you can understand our limitations!
Best Regards.
December 27, 2023 at 1:37 pm #524895
filipopParticipantWould you know of a php snippet that can achieve this
December 27, 2023 at 1:49 pm #524896
filipopParticipantDo you also know how the price under the products (product archive grid) disappeared please
Attachments:
You must be logged in to view attached files.December 27, 2023 at 4:36 pm #524946
Aizaz Imtiaz AwanKeymasterHello,
It is an issue with the snippets> Navigate to Snippets > All Snippets and check the Snippets.
https://ibb.co/XfBcPHVSorry, but additional code customizations like that are out of our theme support.
Best Regards.
-
AuthorPosts
- You must be logged in to create new topics. Login / Register