• Resolved danielmoorehbd

    (@danielmoorehbd)


    Hi,

    This issue keeps happening on our site and there is no behaviour changes to trigger…

    We have a sale on, but for some reason, overnight, the “Sale!” badge has disappeared on some, but not all, products in the sale, despite this, the sale price is still showing…

    I’ve noticed this is only happenng on Variable products. https://imgur.com/a/PVzkVqJ

    Kind Regards

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

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter danielmoorehbd

    (@danielmoorehbd)

    @wprock That’s a Simple product.

    On the category page, it no longer displays the Sale badge on Variable products, last night, all products on the category page I linked above had a badge, this morning, only the simple products do. Odd as nothing was changed to trigger this.

    mother.of.code

    (@imazed)

    The Mother of Code

    Hey @danielmoorehbd! Does the same thing happen when you activate a default theme, such as Twenty Seventeen or Storefront? I suspect something in your active theme might be causing this issue.

    Thread Starter danielmoorehbd

    (@danielmoorehbd)

    Hi @imazed,

    Yes it does.

    To get around the issue, I’ve added this into /theme-folder/woocommerce/loop/sale-flash.php:

        if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
        global $post, $product;
        if ( ! $product->is_in_stock() ) return;
        $sale_price = get_post_meta( $product->id, '_price', true);
        $regular_price = get_post_meta( $product->id, '_regular_price', true);
        if (empty($regular_price)){ //then this is a variable product
    //LINE BELOW CAUSING FATAL ERROR ON SEARCH PAGES
            $available_variations = $product->get_available_variations();
            $variation_id=$available_variations[0]['variation_id'];
            $variation= new WC_Product_Variation( $variation_id );
            $regular_price = $variation ->regular_price;
            $sale_price = $variation ->sale_price;
        }
        $sale = ceil(( ($regular_price - $sale_price) / $regular_price ) * 100);
    ?>
    <?php if ( !empty( $regular_price ) && !empty( $sale_price ) && $regular_price > $sale_price ) : ?>
        <?php echo
            apply_filters( 'woocommerce_sale_flash', '<span class="onsale">-' . $sale . '%</span>', $post, $product );
        ?>
    <?php endif;

    Not the best methord in the world… in fact, it triggers a notice about calling up Woo functions directly! A quick solution was needed…

    Try this once

    <?php
    
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
       
    global $post, $product;
    
    if ( ! $product->is_in_stock() ) return;
    
    $sale_price = get_post_meta( $product->id, '_price', true);
    $regular_price = get_post_meta( $product->id, '_regular_price', true);
    
    if ( empty(  $regular_price ) ) { //then this is a variable product
        //LINE BELOW CAUSING FATAL ERROR ON SEARCH PAGES
        $available_variations = $product->get_available_variations();
        $variation_id=$available_variations[0]['variation_id'];
        $variation= new WC_Product_Variation( $variation_id );
        $regular_price = $variation->regular_price;
        $sale_price = $variation->sale_price;
    }
    
    $sale = ceil(( ($regular_price - $sale_price) / $regular_price ) * 100);
    
    if ( !empty( $regular_price ) && !empty( $sale_price ) && $regular_price > $sale_price ) : 
    ?>
        <?php echo apply_filters( 'woocommerce_sale_flash', '<span class="onsale">-' . $sale . '%</span>', $post, $product ); ?>
    <?php endif;
    Rynald0s.a11n

    (@rynald0s)

    Automattic Happiness Engineer

    Howdy!

    We haven’t heard back from you in a while, so I’m going to go ahead and mark this thread as resolved. If you have any other questions please start a new thread.

    Cheers!

    Hi guys,

    I just wanted to note here that I experienced the same issue that danielmoorehbd had, I switched my theme to the default 2020 theme to double check it wasn’t something I’d introduced but it was the same result there. I have updated my template files with the code that wprock provided (thank you very much) and it has fixed the issue. This does seem to be a real issue/bug though, maybe someone at woocommerce should know? Rynald0s, are you able to pass this on?

    Many thanks.

    Rynald0s.a11n

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @clk87!

    We aren’t able to replicate the same behavior using the most recent version of the Storefront theme against v4.0.1 of the WooCommerce core plugin.

    Are you using the same version of core?

    Cheers!

    Hi Rynald0s,

    Thanks for checking, yea I’m using the same version v4.0.1, maybe it’s something to do with the plugins I have running or something.

    Cheers

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘WooCommerce “Sale!” sticker has disappeared!’ is closed to new replies.