我尝试为每个运输选项添加自定义图标,但无济于事。
我尝试了这个答案: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/