php - 为 WooCommerce 中的每种运输方式添加单独的图标

标签 php html wordpress woocommerce shipping-method

我尝试为每个运输选项添加自定义图标,但无济于事。

我尝试了这个答案:Adding custom icons to the shipping options in Woocommerce Cart and Checkout .

到目前为止,我的代码如下所示:

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 == "shipping_method_0_flat_rate2" ) {
       $label = $label."https://example.com/wp-content/uploads/2020/05/icon.png";

   }
   return $label; 
}

但它就是行不通。我猜想我提供文件路径或方法 ID 的方式存在问题。

感谢任何帮助。

最佳答案

您非常接近...您需要使用$method->id === "flat_rate:2"而不是$method->method_id == "shipping_method_0_flat_rate2"在 IF 语句中。

您还需要在图标中包含完整的 <img>带有源链接的 html 标签...

所以在你的代码中:

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 ) {      
   // Targeting shipping method "Flat rate instance Id 2"
   if( $method->id === "flat_rate:2" ) {
       $label .= '<img src="https://example.com/wp-content/uploads/2020/05/icon.png" />';

   }

   return $label; 
}

代码位于事件子主题(或事件主题)的functions.php 文件中。经过测试并有效。

关于php - 为 WooCommerce 中的每种运输方式添加单独的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61646644/

相关文章:

php - Laravel 在 View 中列出路由

html - 我的链接颜色没有改变?

javascript - HTML5/JavaScript音频播放列表

css - 无法水平对齐 DIV 左/中/右

php - Wordpress 一页网站使用 wp_is_mobile() 加载缓慢

php - 插入使用同一行(相同的主键)mysql php pdo

wordpress - 如何从评论/评论中获取 woocommerce 用户帐单地址

html - 如何解决这个错误: Bad value original-source for attribute rel

wordpress - 如何允许Wordpress用户直接上传到youtube或vimeo?

php - Laravel 定义 Mutator