• freddyeee

    (@freddyeee)


    Hi

    I used this code on the functions.php to show “You save(x%)” message under the price in every product page and it mostly works, but in some product pages doesn’t work like this one https://www.runayaq.com/producto/runayaq-mujer-polo-negritos-de-huanuco/
    what could be the problem ?

    add_action('woocommerce_get_price_html', 'add_you_save_text_woocommerce', 100, 2);
    
    function add_you_save_text_woocommerce( $price_html, $product ){
        if( !is_product() || $product->is_type('variable') ) {
            return $price_html;
        }
        $regular_price  = $product->get_regular_price();
        $active_price   = $product->get_price();
        if( !empty($active_price) && $active_price < $regular_price ) {
            $amount_saved = $regular_price - $active_price;
            $percentage   = intval( ( ( $regular_price - $active_price ) / $regular_price ) * 100 );
            ob_start();
            ?>
            <style>
                .you_save_text_woocommerce {
                    font-size: 16px !important;
                    color: black !important;
                    font-weight: bold !important;
                }
            </style>
            <br>
            <p class="you_save_text_woocommerce">
                <b>Ahorras: <?php echo wp_kses_post( wc_price( $amount_saved ) ) . " (". $percentage."%)"; ?></b>
            </p>
            <?php
            $you_save_html = ob_get_clean();
            $price_html = '<p class="regular-price">' . $price_html . '</p>' . $you_save_html;
        }
        return $price_html;
    }
    

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • michael-levelup

    (@michaeltarongoy)

    Hi there,

    The custom code works on my test website but doesn’t work on some products also.

    That is because the code will not work on variable products. When you choose a variation, it doesn’t get processed by PHP but by Javascript.

    You would have to create an AJAX custom code to handle variable products.

    Let us know how we could help you further.

    Kind Regards,
    Michael

    Thread Starter freddyeee

    (@freddyeee)

    but that code works on some variable products, it works in shirts with attibutes of 2 colors and sizes like this one: https://www.runayaq.com/producto/runayaq-mujer-polo-huaylarsh/

    but not in a product with attributes of just 1 color and sizes.

    https://www.runayaq.com/producto/runayaq-mujer-polo-valle-del-mantaro/

    Eze

    (@chieze)

    Hi @freddyeee,

    Based on the information you provided, it seems like the issue might be related to the product’s attributes. In some cases, when a product has attributes, the code used to display the “You save(x%)” message might not work properly.

    One possible solution could be to modify the code to handle the product’s attributes more gracefully. For example, you might need to adjust the code to check for the presence of attributes and handle them differently depending on the type of attribute (e.g., colour, size, etc.).

    Here is an updated code snippet that should handle both simple and variable products with attributes:

    add_action( 'woocommerce_single_product_summary', 'display_savings_percentage', 12 ); 
    
    function display_savings_percentage() { 
    global $product;     
    
    $regular_price = $product->get_regular_price(); 
    $sale_price = $product->get_sale_price();          
    
    if ( ! $regular_price || ! $sale_price ) {         
    return;     
    }     
    
    if ( $product->is_type( 'variable' ) ) {         
    $min_price = $product->get_variation_regular_price( 'min', true );         $max_price = $product->get_variation_regular_price( 'max', true );         $min_sale_price = $product->get_variation_sale_price( 'min', true );         $max_sale_price = $product->get_variation_sale_price( 'max', true );         
    
    if ( ! $min_price || ! $max_price || ! $min_sale_price || ! $max_sale_price ) {
    return;         
    }         
    
    $regular_price = ( $min_price === $max_price ) ? $min_price : $min_price . ' - ' . $max_price;
    $sale_price = ( $min_sale_price === $max_sale_price ) ? $min_sale_price : $min_sale_price . ' - ' . $max_sale_price;     
    }     
    
    $savings_percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ); 
    echo '<p class="savings-percentage">You save ' . $savings_percentage . '%</p>'; }

    This code should check if the product is a variable product, and if so, calculate the minimum and maximum regular and sale prices for all variations, and then display the appropriate message with the savings percentage.

    If the product is a simple product, the code should work as before and calculate the savings percentage based on the regular and sale prices.

    If the above solution doesn’t work, I will advise that you consider reaching out to WooCommerce for further assistance.

    I hope this helps.

    Cheers,
    Eze

    Thread Starter freddyeee

    (@freddyeee)

    Now the message doesn’t show in any product page with this last code.

    hannah

    (@hannahritner)

    Hi @freddyeee,

    Have you tried reaching out to WooCommerce regarding this?

    Kindly,

    Hannah

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Code issue on product page’ is closed to new replies.