【问题标题】:Change wc_cart_totals_shipping_method_label function in Woocommerce在 Woocommerce 中更改 wc_cart_totals_shipping_method_label 函数
【发布时间】:2019-08-23 04:32:25
【问题描述】:

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 核心文件的情况下对此功能进行必要的更改?

【问题讨论】:

    标签: php wordpress woocommerce cart shipping-method


    【解决方案1】:

    正如您在此函数中看到的,您可以使用woocommerce_cart_shipping_method_full_label 过滤钩子来管理该更改,这样:

    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 文件中。经过测试并且可以工作。

    【讨论】:

    • 谢谢它有效。让我感到困惑的部分是,如果需要将自定义代码添加到现有函数中,我一直认为“操作”是您添加的内容。
    • @DanishMuneer 您可以借助 WordPress 中的操作“挂钩”功能。这样做的好处是,当您更新例如 WooCommerce 时,您的代码仍然存在,因为它已写入您的 functions.php 文件,而该文件永远不会被更新覆盖 - 而不是插件。 Loic 这样做的方式是 100% 正确且最先进的。因此,如果它对您有所帮助,您必须检查答案是否正确。
    • @Mr.Jo 这是我的代码所做的,它使用过滤器挂钩,而不是覆盖核心文件,这也是这个问题的目的。
    猜你喜欢
    • 2018-12-16
    • 2016-05-27
    • 2021-02-05
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 2018-10-02
    相关资源
    最近更新 更多