skip to Main Content

I want to display sale bubble in WooCommerce only for logged in users.
I have a function which hides sale-bubble for unlogged users but if I log in there is showing only value “1” instead of sale-bubble.
I know why, because I am returning a true, but I cant figure out how to return sale-bubble instead of true..

WooCommerce

add_filter('woocommerce_sale_flash', 'woo_custom_hide_sales_flash');
function woo_custom_hide_sales_flash()
{
    if ( is_user_logged_in() ) {
        return true;
    }
    return false;
}

3

Answers


  1. function sales_markup() {   
    
        if(!is_admin()) { 
    
        if(is_user_logged_in())    {  
         // Instead of outputting add the markup that you want to show  
         $output = '<div class="Sales_markup_here">
          </div>';
         return $output;
    
         }
      }
    }
    
    // use shortcode instead of action and then use shortcode anywhere you want to ouptut it
    add_shortcode( 'sales_markup', 'sales_markup' );
    

    Use the shortcode where you want to output the bubble. You can add the css in the global css file.

    Login or Signup to reply.
  2. You are not using this filter hook in the right way. Try the following:

    add_filter( 'woocommerce_sale_flash', 'filter_sales_flash_callback', 100, 3 );
    function filter_sales_flash_callback( $output_html, $post, $product )
    {
        if ( ! is_user_logged_in() ) {
            $output_html = false;
        }
        return $output_html;
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and works.

    Login or Signup to reply.
  3. function sales_badge() {   
        global $product;
       if ( ! $product->is_on_sale() ) return;
       if ( $product->is_type( 'simple' ) ) {
          $max_percentage = ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100;
       } elseif ( $product->is_type( 'variable' ) ) {
          $max_percentage = 0;
          foreach ( $product->get_children() as $child_id ) {
             $variation = wc_get_product( $child_id );
             $price = $variation->get_regular_price();
             $sale = $variation->get_sale_price();
             if ( $price != 0 && ! empty( $sale ) ) $percentage = ( $price - $sale ) / $price * 100;
             if ( $percentage > $max_percentage ) {
                $max_percentage = $percentage;
             }
          }
       }
       if ( $max_percentage > 0 ) { ?>
            <span class="prinjal-sale-badge"><?php echo round($max_percentage) . "%"; ?></span>    
           <?php
       }
    }
    
    // use shortcode instead of action and then use shortcode anywhere you want to ouptut it
    add_shortcode( 'custom_sales_badge', 'sales_badge' );
    

    my problem solve using this code you can place any ware you want to display sale badge

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search