Php 向WooCommerce中的每个配送方式添加单个图标
我试图添加一个自定义图标到每个航运选项,但没有任何效果 我试过这个答案: 到目前为止,我的代码如下所示:Php 向WooCommerce中的每个配送方式添加单个图标,php,html,wordpress,woocommerce,shipping-method,Php,Html,Wordpress,Woocommerce,Shipping Method,我试图添加一个自定义图标到每个航运选项,但没有任何效果 我试过这个答案: 到目前为止,我的代码如下所示: 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, $met
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方面存在问题
感谢您的帮助。您非常接近……您需要在IF语句中使用
$method->id==“flat\u rate:2”
而不是$method->method\u id==“shipping\u method\u 0\u flat\u rate2”
此外,对于图标,还需要包含完整的”;
}
返回$label;
}
代码进入活动子主题(或活动主题)的functions.php文件。测试并运行。是否检查了if语句是否正确,是否有一个值设置为$label?我只是想确认一下,这不是身体状况的问题。谢谢。如果你不介意的话,供将来参考。我如何知道$label对象中包含了什么?如果您能为我提供一些资源,我将不胜感激。例如,您如何知道应该为$label分配什么?我提供了URL,这是错误的。你知道它应该在标签里。如果不在这里询问,我在哪里可以找到这些知识?我应该读什么呢?
$label
参数不是一个对象,而是显示在购物车和结帐页面上的每个可用装运方法的标签名称(字符串)……另一方面,$method
参数是WC\u shipping\u Rate
对象实例。以下是WC\u Shipping\u Rate
Class.@mgatner要了解如何更改这种情况,请首先尝试查看与WooCommerce相关的模板…然后在cart/cart Shipping.php
template文件中,您将使用函数WC\u cart\u totals\u Shipping\u method\u label()找到Shipping methods标签的显示
…然后搜索该函数,您会发现它的源代码中有正确的过滤器挂钩可供使用。
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;
}