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

Viewing 16 posts - 1 through 16 (of 16 total)
  • Author
    Posts
  • #524371

    filipop
    Participant

    Hi, 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.
    Thanks

    #524477

    Hello,

    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.

    #524617

    filipop
    Participant

    Thank 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.
    #524619

    filipop
    Participant

    Can I ask please … if I select a variation with a different price — that will also change in the price displayed ?

    #524633

    filipop
    Participant

    The 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.
    #524635

    filipop
    Participant

    Is this the array ? $data
    $regular_price = $data[‘display_regular_price’];

    #524642

    filipop
    Participant

    Also – 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.
    #524660

    Hello,

    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.

    #524670

    filipop
    Participant

    Thanks
    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 AMOUNT

    I 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.
    #524672

    filipop
    Participant

    Also 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

    #524679

    filipop
    Participant

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

    #524680

    filipop
    Participant

    I now just need to move the price above description

    #524790

    Hello,

    Unfortunately, Such modification requires complicated Woocommerce code customization which is not covered by our support.

    Hope you can understand our limitations!

    Best Regards.

    #524895

    filipop
    Participant

    Would you know of a php snippet that can achieve this

    #524896

    filipop
    Participant

    Do you also know how the price under the products (product archive grid) disappeared please

    Attachments:
    You must be logged in to view attached files.
    #524946

    Hello,

    It is an issue with the snippets> Navigate to Snippets > All Snippets and check the Snippets.
    https://ibb.co/XfBcPHV

    Sorry, but additional code customizations like that are out of our theme support.

    Best Regards.

Viewing 16 posts - 1 through 16 (of 16 total)