【问题标题】:WooCommerce override woocommerce_cart_product_priceWooCommerce 覆盖 woocommerce_cart_product_price
【发布时间】:2016-07-14 07:02:48
【问题描述】:

我想在我的子主题functions.php 文件中覆盖以下代码。因为我希望 product_price 显示“get_price_included_tax”而不是排除,但购物车的其余部分应保持不含税。

public function get_product_price( $_product ) {
if ( $this->tax_display_cart == 'excl' ) {
$product_price = $_product->get_price_excluding_tax();
} else {
$product_price = $_product->get_price_including_tax();
}
return apply_filters( 'woocommerce_cart_product_price', wc_price(     $product_price ), $_product );
}

需要切换“get_price_including_tax()”和“get_price_ exclude_tax()”,这似乎可以解决问题,但我不想编辑核心文件。

试过这个:

add_filter('woocommerce_cart_product_price', 'product_price_incl_tax');

function product_price_incl_tax($_product ) {
if ( $this->tax_display_cart == 'excl' ) {
$product_price = $_product->get_price_including_tax();
} else {
$product_price = $_product->get_price_excluding_tax();
}

return apply_filters( 'product_price_incl_tax', wc_price( $product_price ), $_product );
}

这给了我以下错误(错误删除了网址):

Fatal error: Using $this when not in object context in /public/sites/www.t-instyle.nl/paperbag/wp-content/themes/virtue_child/functions.php on line 7

如果我从代码中删除 $this->,我会收到以下错误:

Fatal error: Call to a member function get_price_excluding_tax() on string in /public/sites/www.t-instyle.nl/paperbag/wp-content/themes/virtue_child/functions.php on line 10

谁能帮助我正确的方向?不幸的是,我不是程序员,所以我基本上不知道自己在做什么。

非常感谢!

【问题讨论】:

  • 你有解决办法吗?

标签: function filter woocommerce hook cart


【解决方案1】:

不知道你是否已经解决了这个问题,但你列出的第一个错误:

Fatal error: Using $this when not in object context in /public/sites/www.t-instyle.nl/paperbag/wp-content/themes/virtue_child/functions.php on line 7

告诉您$this 不涉及任何内容。因此,您需要回溯以弄清楚 $this 应该是什么意思。尝试更改此行:

if ( $this->tax_display_cart == 'excl' ) {

到这里:

if ( $woocommerce->cart->tax_display_cart == 'excl' ) {

您可能还需要在此之前调用全局$woocommerce

function product_price_incl_tax($_product ) {
global $woocommerce;
if ( $woocommerce->cart->tax_display_cart == 'excl' ) {

这至少应该让你克服第一个错误,然后你就可以从那里看到事情是否正常。

【讨论】:

    猜你喜欢
    • 2016-10-20
    • 2014-02-04
    • 2017-05-06
    • 2012-08-04
    • 2020-08-03
    • 2018-02-16
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多