0

i want add, on product listing, over image a text badge "Free delivery" based on specific shipping class "spedizione-gratuita". You can help me ?

I test this code but dont work

add_action( 'woocommerce_before_shop_loop_item_title', 'single_product_label', 10 );
function single_product_label() {
   global $product;
   $shipping_classes  = 'spedizione-gratuita';
    if (  $product->get_shipping_class() )  {

        echo '<div class="spedizione-gratuita"><span class="freedel">SPEDIZIONE GRATUITA</span></div>';
        
    }
  }

Tnks

1 Answers1

0

I believe this will achieve what you are trying to do. You almost had it, but the $product global has no value in the shop loop you need to use $post instead

add_action( 'woocommerce_before_shop_loop_item_title', 'single_product_label', 10 );
function single_product_label() {
   global $post;
   $_product = wc_get_product( $post->ID );
    
   $shipping_classes  = 'spedizione-gratuita';
   if ( $shipping_classes == $_product->get_shipping_class() )  {

        echo '<div class="spedizione-gratuita"><span class="freedel">SPEDIZIONE GRATUITA</span></div>';
        
   }
}
jtowell
  • 246
  • 1
  • 2
  • 8