【发布时间】:2021-05-28 19:20:01
【问题描述】:
我正在尝试根据购物车中的任何产品是否有插件来更改运输标签。
为此,我使用 How can I modify a Woocommerce shipping label 答案代码,并根据我的需要进行了一些更改。
HTML:
<input type="hidden" name="shipping_method[0]" data-index="0" id="shipping_method_0_flat_rate5" value="flat_rate:5" class="shipping_method">
<label class="shipping__list_label" for="shipping_method_0_flat_rate5">Standard Shipping (2-3 days): <span class="woocommerce-Price-amount amount"><bdi>3,95<span class="woocommerce-Price-currencySymbol">€</span></bdi></span></label>
所以标签Standard Shipping (2-3 days) 必须显示Shipping with custom (4-6 days) 如果满足特定条件(在这种情况下: custom_text 字段不为空)。
functions.php:
//Change Standard Shipping Label if product is customized
add_filter( 'woocommerce_cart_shipping_method_full_label', 'change_shipping_label', 10, 2 );
function change_shipping_label( $full_label, $method ){
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( !empty( $cart_item['custom_text'] ) ) {
$full_label = str_replace( "Standard Shipping (2-3 days)", "Shipping with customization (4-6 days)", $full_label );
}
}
return $full_label;
}
这是成功的。如果满足条件,则标签会在结账前更改为带有定制的发货(4-6 天)。
但是,结帐后标签更改不适用(感谢页面和电子邮件,它显示原始“标准运费(2-3 天)”。
我可以在感谢页面和电子邮件上显示此标签更改吗?
【问题讨论】:
-
就是这样,它也会影响订单和电子邮件中的原始显示。运输方式是统一费率,标签显示预计交货时间(2-4 天)。我想要实现的是在条件为真时将其更改为(4-6 天),这段代码做得很好,除了你说的“订单和电子邮件”......不确定运输方式费率 ID 的实际含义,但我希望在这里找到:
-
没有增加预计交货时间,它只是唯一可用运输方式的标签名称:我称之为“标准运输(2-3 天)”。我已经更新了这个问题。我希望现在更清楚了。
-
运送方式是一种称为“标准运送(2-3 天)”的统一费率。是这个意思吗?
-
没错。我可以使用上面的函数将字符串更改为“自定义发货(4-6 天)”。更改显示在结帐页面中,但按顺序显示原始字符串:“标准配送(2-3 天)”。
-
我已经回答了……我希望它会起作用。
标签: php wordpress woocommerce orders shipping-method