【问题标题】:WooCommerce - Overriding wc-formatting-functions.php file via theme is not workingWooCommerce - 通过主题覆盖 wc-formatting-functions.php 文件不起作用
【发布时间】:2016-07-29 13:32:11
【问题描述】:

当我在 WooCommerce 插件文件夹中更改核心文件时,我试图将我的代码包含在 wc-formatting-functions.php 文件中,并且它正在工作。

现在我在我的主题文件夹中创建了 woocommerce 文件夹,并在其中复制了 wc-formatting-functions.php 文件,试图覆盖,但它不起作用。

我正在尝试将此代码插入 wc_price() 函数中:

if(is_product_category('mattress')){
    echo "mattress category";
}
else if(is_product_category('pillows')){
    echo "pillows category";
}
else if(is_product_category('protectors')){
    echo "protectors category";
}
else if(is_product_category('toppers')){
    echo "toppers category";
}
else if(is_product_category('foldable-mattress')){
   echo "foldable-mattress category";
}

就在 wc-formatting-functions.php 中的这些行之后:

$formatted_price = ( $negative ? '-' : '' ) . sprintf( $price_format, '<span class="woocommerce-Price-currencySymbol">' . get_woocommerce_currency_symbol( $currency ) . '</span>', $price );
$return = '<span class="woocommerce-Price-amount amount">' . $formatted_price . '</span>';

我做错了什么?

【问题讨论】:

    标签: php wordpress woocommerce categories product


    【解决方案1】:

    由于 wc-formatting-functions.phpWooCommerce 核心 文件而不是 模板 文件,您将无法使其在woocommerce 文件夹在您的主题文件夹中。

    无法通过主题将wc-formatting-functions.php核心文件覆盖为模板文件。

    根据你想在哪里使用你的代码,你可以尝试在相关的钩子中使用它,比如:

    add_filter( 'woocommerce_cart_product_price', 'filter_woocommerce_cart_product_price', 10, 2 ); 
    function filter_woocommerce_cart_product_price( $wc_price, $product ) { 
        // some code 
        return $wc_price; 
    }; 
    

    作为is_product_category() 条件目标类别页面存档,您可以覆盖相关的 woocommerce 模板,具体取决于您希望在何处使用代码。

    您甚至可以将代码插入活动主题的 function.php 文件中,嵌入到您可以重复使用的函数中。

    但是,如果您想使用条件定位某个类别,则必须改用例如 has_term( 'mattress', 'product_cat' )

    参考资料:

    【讨论】:

    • TheAztec 非常感谢您的精彩解释..现在我正在尝试创建挂钩,以便我可以根据类别显示图像。我正在尝试使用 has_term('mattress', 'product_cat') 但是它没有在类别页面上显示任何内容..
    • TheAztec 你能看看这个问题吗,我遇到了这个问题。请..stackoverflow.com/questions/38567523/…
    • @websmentor.com 好的,让我看看……我会在里面告诉你的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多