• Resolved Kalyan Brata Das

    (@ayanizeco)


    Hi,

    I know that using this function

    remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );

    I can remove the sales badge altogether but how I can remove this only on certain products even though they have the sales in place? Just need to remove this on some products.

    Thanks

    https://www.ads-software.com/plugins/woocommerce/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Add a custom function in it’s stead.

    add_action( 'woocommerce_before_shop_loop_item_title', 'custom_woocommerce_show_product_loop_sale_flash', 10 );
    
    function custom_woocommerce_show_product_loop_sale_flash() {
         global $post;
    
         if ( ! in_array( $post->ID, array( 1, 2, 3 ) ) ) {
              wc_get_template( 'loop/sale-flash.php' );
         }
    }
    Thread Starter Kalyan Brata Das

    (@ayanizeco)

    Thanks but how I can implement this conditionally getting the post id problematically and not manually placing them in array? Let me explain.

    I am allowing users to add custom badge via custom field value. Now when they have the value, that sales icon does not matter in a sense, so it must be removed. How I can achieve this. I am echoing the badge this way

    add_action('woocommerce_before_shop_loop_item', 'ac_wc_show_custom_fields', 20); 
    
        function ac_wc_show_custom_fields(){
            global $post;
            if (get_post_meta( $post->ID, 'ac_custom_badge', true )!==''){
                echo '<div class="ac-loop-cus-badge">' .get_post_meta( $post->ID, 'ac_custom_badge', true ) . '</div>';
    
            }
        }

    Now as you can see the custom badge value is being showing, I need to hide the default Sales badge. Please have a look at the image https://i.imgur.com/1OWmLQK.png

    I need to apply css or jquery conditionally but I am not getting this done. I tried with

    function ac_wc_show_custom_fields(){
                global $post;
                if (get_post_meta( $post->ID, 'ac_custom_badge', true )!==''){
                    echo '<div class="ac-loop-cus-badge">' .get_post_meta( $post->ID, 'ac_custom_badge', true ) . '</div>';
    echo '<style>.onsale{display:none;}</style>';
    
                }
            }

    But the css display:none is being applied on all the products sales badge making them hidden even though the user has not created the custom badge using the custom field.

    Please advise. Thanks

    Thread Starter Kalyan Brata Das

    (@ayanizeco)

    update..

    I also tried with this but no luck

    function hide_css(){
    	global $post, $product;
    	$key = 'ac_custom_badge';
    $themeta = get_post_meta($post->ID, $key, true);
    if($themeta=='') {
    echo '<style>.woocommerce ul.products li.product .onsale{display:none!important;}</style>';
    }
    
    }
    add_action('wp_head', 'hide_css', 99);

    ??

    Thread Starter Kalyan Brata Das

    (@ayanizeco)

    I also tried adding a new class using jQuery

    function hide_css(){
    	global $post, $product;
    	$key = 'ac_custom_badge';
    $themeta = get_post_meta($post->ID, $key);
    if($themeta !=='') {
    echo '<script type="text/javascript">
    
    jQuery(document).ready(function($){
    	$(".onsale").addClass("new-class");
    });
    </script>';
    }
    }
    add_action('wp_head', 'hide_css', 99);

    Once again the class is being added too all the onsale span making it impossible for me to apply some rule to that new .onsale.new-class class. If anything done there it applies to all the sales badges span however, I want to apply this only on that particular product sales badge where I created the custom field for the custom badge. Please help. I am not getting the conditionals right.

    Thanks

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    The code I provided would be to only output the sales badge for certain ids. No CSS is required for that snippet. Replace the 1,2,3 with the ids on which you want to hide the badge.

    You also don’t need to use the in_array I have a sample for. You could for example get the custom field value and see if its set, before loading the template.

    Thread Starter Kalyan Brata Das

    (@ayanizeco)

    Sorry for late reply. I tried with your suggestion and it still do now work. It ends up with dual span of the sales badge. See the image. Here is the function

    add_action( 'woocommerce_before_shop_loop_item_title', 'custom_woocommerce_show_product_loop_sale_flash', 10 );
    
    function custom_woocommerce_show_product_loop_sale_flash() {
         global $post;
    
         if (get_post_meta($post->ID, 'ac_custom_badge', true)) {
              wc_get_template( 'files/sale-flash.php' );
    
         }
    }

    See the HTML markups here https://i.imgur.com/ok9Bfoj.png

    Thread Starter Kalyan Brata Das

    (@ayanizeco)

    UPDATE..

    Finally I got it working. I had to remove the call first. I forgot that. Here is the final code

    remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash'  );
    
    add_action( 'woocommerce_before_shop_loop_item_title', 'custom_woocommerce_show_product_loop_sale_flash', 10 );
    
    function custom_woocommerce_show_product_loop_sale_flash() {
         global $post;
    
         if (empty(get_post_meta($post->ID, 'ac_custom_badge', true))) {
             wc_get_template( 'files/sale-flash.php' );
    
    		  		   }
    }

    Thanks for your time.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to remove sales badge on certain products’ is closed to new replies.