Search code examples
phpwordpresswoocommerceiconsshipping-method

WooCommerce - How to insert icons before delivery method


Good day,

Could anyone please help me how to insert icons before delivery method?

I use this filter

add_filter( 'woocommerce_cart_shipping_method_full_label', 'filter_woocommerce_cart_shipping_method_full_label', 10, 2 ); 
function filter_woocommerce_cart_shipping_method_full_label( $label, $method ) { 
   // Use the condition here with $method to apply the image to a specific method.      

   if( $method->method_id === "napobocce" ) {
       $label = "<img src='https://image.flaticon.com/icons/svg/2922/2922830.svg' style='height: 30px; border-radius: 20px; margin-left: 5px; margin-right: 5px;' />".$label;
   } 
   if( $method->method_id == "zasilkovna" ) {
       $label = "<img src='https://image.flaticon.com/icons/svg/2922/2922830.svg' style='height: 30px; border-radius: 20px; margin-left: 5px; margin-right: 5px;' />".$label;       
   }
   if( $method->method_id == "doprava>ceska-posta>16" ) {
       $label = "<img src='https://image.flaticon.com/icons/svg/2922/2922830.svg' style='height: 30px; border-radius: 20px; margin-left: 5px; margin-right: 5px;' />".$label;       
   }
   if( $method->method_id == "doprava>geis>16" ) {
       $label = "<img src='https://image.flaticon.com/icons/svg/2922/2922830.svg' style='height: 30px; border-radius: 20px; margin-left: 5px; margin-right: 5px;' />".$label;       
   }
   return $label; 
}

This works normally for local pickup and Zásilkovna, but not for the last two.

I found the id I have with them at the value "value" in the input. Would anyone know how to help me?

Or how to do this using CSS only?


Solution

  • To found out the correct $method->method_id you could use

    echo 'DEBUG: method id = '. $method->method_id;
    

    So you get

    function filter_woocommerce_cart_shipping_method_full_label( $label, $method ) {
        // Remove afterwards
        echo 'DEBUG: method id = '. $method->method_id;
        
        // Use the condition here with $method to apply the image to a specific method.      
        if( $method->method_id === "local_pickup" ) {
            $label = "<img src='https://image.flaticon.com/icons/svg/2922/2922830.svg' style='height: 30px; border-radius: 20px; margin-left: 5px; margin-right: 5px;' />" . $label;
        } elseif( $method->method_id == "zasilkovna" ) {
            $label = "<img src='https://image.flaticon.com/icons/svg/2922/2922830.svg' style='height: 30px; border-radius: 20px; margin-left: 5px; margin-right: 5px;' />" . $label;       
        }
        
        return $label; 
    }
    add_filter( 'woocommerce_cart_shipping_method_full_label', 'filter_woocommerce_cart_shipping_method_full_label', 10, 2 );