显示有和没有税和税额的Woocommerce产品价格

问题描述 投票:7回答:3

我正在使用WooCommerce for WordPress,我列出的项目不含税。

我需要在产品页面上单独显示价格(不含税),税和PRICE +税(如结帐页面)。

我找不到这样做的插件。

我怎样才能做到这一点?

php wordpress woocommerce price tax
3个回答
14
投票

WooCommerce v3.0.0及更高版本 从WooCommerce 3.0版开始,函数woocommerce_price()已弃用,get_price_including_tax()方法也是如此。相反,你应该使用wc_get_price_including_tax

<?php echo wc_price( wc_get_price_including_tax( $product ) ); ?>

在WooCommerce v3.0.0之前 您需要修改模板。不要修改核心WooCommerce模板,而是使用WooCommerce模板覆盖系统将其复制到您的主题。有关这方面的帮助,请参阅有关使用template override system的WooCommerce文档。

price.php模板中,您将在需要价格的地方添加这段代码,包括税(VAT):

<?php echo woocommerce_price( $product->get_price_including_tax() ); ?>

注意:您修改的price.php模板应位于wp-content/themes/[your theme folder]/woocommerce/single-product/price.php


8
投票

更新2018/2019(适用于Woocommerce 3+)

显示不含税的价格+税额+含税价格(在单独的行上):

首先阅读"How to override Woocommerce templates via your theme"

1)在single-product/price.php模板文件(单个产品页面)上。

将代码替换为:

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

global $product;

// Get the prices
$price_excl_tax = wc_get_price_excluding_tax( $product ); // price without VAT
$price_incl_tax = wc_get_price_including_tax( $product );  // price with VAT
$tax_amount     = $price_incl_tax - $price_excl_tax; // VAT amount

// Display the prices
?>
<p class="price-excl"><?php echo wc_price( $price_excl_tax ); ?></p>
<p class="tax-price"><?php  echo wc_price( $tax_amount ); ?></p>
<p class="price-incl"><?php echo wc_price( $price_incl_tax ); ?></p>

2)在loop/price.php模板文件(商店和存档页面)上。

将代码替换为:

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

global $product;

if ( $product->get_price_html() ) :
    // Get the prices
    $price_excl_tax = wc_get_price_excluding_tax( $product ); // price without VAT
    $price_incl_tax = wc_get_price_including_tax( $product );  // price with VAT
    $tax_amount     = $price_incl_tax - $price_excl_tax; // VAT amount

    // Display the prices
    ?>
    <span class="price price-excl"><?php echo wc_price( $price_excl_tax ); ?></span><br>
    <span class="price tax-price"><?php  echo wc_price( $tax_amount ); ?></span><br>
    <span class="price price-incl"><?php echo wc_price( $price_incl_tax ); ?></span>
<?php endif ?>

文档: •qazxsw poi •qazxsw poi产品价格功能 •qazxsw poi产品价格功能 •Template structure and how to override Woocommerce templates via your theme格式化价格函数 •wc_get_price_including_tax()产品价格功能


原始答案(在woocommerce 3之前):

在检查您的WooCommerce Tax常规设置是否符合您的需求之前。

正如wc_get_price_excluding_tax()建议的那样,您需要从woocommerce中复制活动子主题或主题中的wc_price()文件夹。然后重命名为wc_get_price_to_display()。在这个cale_b模板文件夹中,您将在templates子文件夹中找到price.php模板,以编辑与单个产品页面中的定价显示相关的模板。

woocommerce之后的woocommerce模板文件中,将代码替换为:

single-product

由于额外的价格未格式化,您可能需要使用一些woocommerce php函数将这些额外价格与其他元素混合:

single-product/price.php

参考:global $product;


6
投票

目前您不再需要更改模板。您可以在Woocommerce设置中进行设置:

  • Woocommerce:税收选项卡:在购物车和结账时显示商店/显示价格中的价格
© www.soinside.com 2019 - 2024. All rights reserved.