php - 在 Woocommerce 中更改 wc_cart_totals_shipping_method_label 函数

标签 php wordpress woocommerce cart shipping-method

includes 上的 Woocommerce 插件内部子文件夹,有一个文件wc-cart-functions.php .
我想更改功能wc_cart_totals_shipping_method_label() ,但我不允许将该函数复制到我的主题的functions.php。我相信我必须使用自定义操作/过滤器来更改此核心功能,但不知道该怎么做。

原功能:

function wc_cart_totals_shipping_method_label( $method ) {
    $label     = $method->get_label();
    $has_cost  = 0 < $method->cost;
    $hide_cost = ! $has_cost && in_array( $method->get_method_id(), array( 'free_shipping', 'local_pickup' ), true );

    if ( $has_cost && ! $hide_cost ) {
        if ( WC()->cart->display_prices_including_tax() ) {
            $label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() );
            if ( $method->get_shipping_tax() > 0 && ! wc_prices_include_tax() ) {
                $label .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
            }
        } else {
            $label .= ': ' . wc_price( $method->cost );
            if ( $method->get_shipping_tax() > 0 && wc_prices_include_tax() ) {
                $label .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
            }
        }
    }

    return apply_filters( 'woocommerce_cart_shipping_method_full_label', $label, $method );
}

函数应该是什么:
function wc_cart_totals_shipping_method_label( $method ) {
    $label     = $method->get_label();
    $has_cost  = 0 < $method->cost;
    $hide_cost = ! $has_cost && in_array( $method->get_method_id(), array( 'free_shipping', 'local_pickup' ), true );

    if ( $has_cost && ! $hide_cost ) {
        if ( WC()->cart->display_prices_including_tax() ) {
            $label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() );
            if ( $method->get_shipping_tax() > 0 && ! wc_prices_include_tax() ) {
                $label .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
            }
        } else {
            $label .= ': ' . wc_price( $method->cost );
            if ( $method->get_shipping_tax() > 0 && wc_prices_include_tax() ) {
                $label .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
            }
        }
    }
    /* Here's the code I want added */
    elseif ( ! $has_cost && ! $hide_cost ) {
        $label .= ': Free';
    }

    return apply_filters( 'woocommerce_cart_shipping_method_full_label', $label, $method );
}

我想做的事:
零成本的运输方式应在其标签上添加“免费”。现在,方法标题旁边没有显示任何内容。

如何在不覆盖 Woocommerce 核心文件的情况下对此功能进行必要的更改?

最佳答案

正如你在这个函数中看到的,你可以使用 woocommerce_cart_shipping_method_full_label过滤器钩子(Hook),以这种方式管理该更改:

add_filter( 'woocommerce_cart_shipping_method_full_label', 'change_cart_shipping_method_full_label', 10, 2 );
function change_cart_shipping_method_full_label( $label, $method ) {
    $has_cost  = 0 < $method->cost;
    $hide_cost = ! $has_cost && in_array( $method->get_method_id(), array( 'free_shipping', 'local_pickup' ), true );

    if ( ! $has_cost && ! $hide_cost ) {
        $label  = $method->get_label() . ': Free';
    }
    return $label;
}

代码位于您的事件子主题(或事件主题)的 function.php 文件中。测试和工作。

关于php - 在 Woocommerce 中更改 wc_cart_totals_shipping_method_label 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55464772/

相关文章:

php - 确定主键编号的标准方法是什么?

php - 如何通过 nginx/php 执行 bash 脚本

php - mysql计算两个日期时间之间的结果

sql - 为什么 Wordpress 有单独的 'usersmeta' 和 'users' SQL 表。为什么不把它们结合起来呢?

php - 将数据保存到 WooCommerce session

php - 打印(生成)以毫米为单位的 code128 条形码(或任何图像) (php)

php - Woocommerce 3 中的自定义结账字段和运输方式 ajax 交互

php - 根据产品类别替换 WooCommerce 占位符图像

php - 在 Woocommerce 中更改 "No Product"消息

css - FooterWrap 打破盒子