【问题标题】:How to change Woocommerce shipping labels in cart, checkout and also on orders如何更改购物车、结帐和订单中的 Woocommerce 运输标签
【发布时间】: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


【解决方案1】:

更新 2

根据提供的代码和信息,更改订单上的特定运输标签,使用运费 ID 来定位所需的运输方式:

add_action( 'woocommerce_checkout_create_order_shipping_item', 'action_wc_checkout_create_order_shipping_item', 10, 4 );
function action_wc_checkout_create_order_shipping_item( $item, $package_key, $package, $order ) {
    // Targeting "flat_rate:5" by its instance ID
    if( $item->get_instance_id() == 5 ) {
        $item->set_method_title( "Shipping with customization (4-6 days)" );
    }
}

代码进入活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

相关: Change specific shipping method title on WooCommerce orders after checkout

【讨论】:

  • 我正在测试,但现在它似乎不起作用......我也会尝试相关问题,如果解决了我会在这里发布。
猜你喜欢
  • 2016-03-20
  • 2018-07-09
  • 2011-05-13
  • 1970-01-01
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
  • 2018-10-01
  • 1970-01-01
相关资源
最近更新 更多