【问题标题】:Woocommerce Change shipping method title on checkout based on shipping class selectedWoocommerce 根据选择的运输类别在结账时更改运输方式标题
【发布时间】:2018-12-04 05:05:05
【问题描述】:

我想根据产品的配送等级更改商店结账时显示的配送方式标题。

例如

运输方式标题目前是统一费率,我有 2 件产品:

  • 如果正在购买产品 A,我需要它具有“易碎运输”
  • 如果正在购买产品 B,我需要它具有“标准配送”

遗憾的是,我必须使用类来完成运输,因此其他方法不起作用。

任何帮助将不胜感激。

【问题讨论】:

    标签: php wordpress woocommerce cart shipping-method


    【解决方案1】:

    以下代码将根据您的“易碎”运输等级重命名您的统一运费:

    您可能必须在“运输选项”选项卡下的常规运输设置中“启用调试模式”,以暂时禁用运输缓存。

    代码:

    add_filter('woocommerce_package_rates', 'change_shipping_method_name_based_on_shipping_class', 50, 2);
    function change_shipping_method_name_based_on_shipping_class($rates, $package){
        // HERE set the shipping class for "Fragile"
        $shipping_class_id = 64;
        $found = false;
    
        // Check for the "Fragile" shipping class in cart items
        foreach( $package['contents'] as $cart_item ) {
            if( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ){
                $found = true;
                break;
            }
        }
    
        // Loop through shipping methods
        foreach ( $rates as $rate_key => $rate ) {
            // Change "Flat rate" Shipping method label name
            if ( 'flat_rate' === $rate->method_id ) {
                if( $found )
                    $rates[$rate_key]->label = __( 'Fragile shipping', 'woocommerce' );
                else
                    $rates[$rate_key]->label = __( 'Standard shipping', 'woocommerce' );
            }
        }
        return $rates;
    }
    

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

    别忘了在发货设置中重新启用“启用调试模式”选项。

    【讨论】:

    • 辛苦了,谢谢 Loic
    • @LoicTheAztec 有没有办法只针对某个运输方式 ID 执行此操作,这样如果我显示多种运输方式,它就不会更改标签。
    【解决方案2】:

    我认为这个plugin 可以帮助你并检查这个one

    【讨论】:

      猜你喜欢
      • 2014-07-05
      • 2018-08-13
      • 2020-05-20
      • 1970-01-01
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 2018-04-27
      • 2020-03-08
      相关资源
      最近更新 更多