WooCommerce-通过主题覆盖wc-formatting-functions.php文件无效

WooCommerce-通过主题覆盖wc-formatting-functions.php文件无效,php,wordpress,woocommerce,categories,product,Php,Wordpress,Woocommerce,Categories,Product,当我在WooCommerce插件文件夹中更改核心文件时,我试图将我的代码包含在wc formatting functions.php文件中,并且该文件正常工作 现在,我在我的主题文件夹中创建了woocommerce文件夹,并在其中复制了wc格式化函数.php文件,试图覆盖,但它不起作用 我试图在函数中插入此代码: if(is_product_category('mattress')){ echo "mattress category"; } else if(is_product_cate

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

现在,我在我的主题文件夹中创建了
woocommerce
文件夹,并在其中复制了
wc格式化函数.php
文件,试图覆盖,但它不起作用

我试图在函数中插入此代码:

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>';
$formatted\u price=($negative?'-':'')。sprintf($price_format,''.get_-woocmerce_-currency_-symbol($currency)。''$price);
$return=''$格式为“价格”;

我做错了什么?

作为
wc格式化函数。php
WooCommerce核心文件,而不是模板文件,您将无法使其在主题文件夹中的
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; 
}; 
由于
是\u product\u category()
条件目标类别页面存档,您可以覆盖相关的woocommerce模板,具体取决于您希望在何处使用代码

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

但是,如果您想使用条件来定位某个类别,则必须使用例如
has\u term('matd','product\u cat')

参考资料:


TheAztec非常感谢您的精彩解释。现在我正在尝试创建挂钩,以便可以根据类别显示图像。我尝试使用has_术语(“床垫”、“产品猫”),但它在类别页面上没有显示任何内容。TheAztec您能看看这个问题吗,我遇到了这个问题。请…@webmentor.com好的,让我看看……我会让你知道的。