【问题标题】:Display Woocommerce product price with and without tax and tax amount显示含税和不含税的 Woocommerce 产品价格和税额
【发布时间】:2020-03-20 05:41:20
【问题描述】:

我正在为 WordPress 使用 WooCommerce,并且我列出了不含税的项目。

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

我一直没能找到可以做到这一点的插件。

我该怎么做?

【问题讨论】:

  • 类型错误:单独

标签: php wordpress woocommerce price tax


【解决方案1】:

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 模板中,您将在需要价格的地方添加这段代码,包括税(增值税):

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

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

【讨论】:

    【解决方案2】:

    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 ?>
    

    文档:
    Template structure and how to override Woocommerce templates via your theme
    wc_get_price_including_tax()产品价格功能
    wc_get_price_excluding_tax()产品价格功能
    wc_price() 格式化价格功能
    wc_get_price_to_display()产品价格功能


    原始答案(在 woocommerce 3 之前)

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

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

    global $product;之后的single-product/price.php模板文件中, 将代码替换为:

    ?>
    <div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
    <?php
        $price_excl = $product->get_price_excluding_tax(); // price without VAT
        $price_incl = $product->get_price_including_tax();  // price included VAT
        $tax_amount = $price_incl - $price_excl; // VAT price amount
    ?>
        <p class="price"><?php echo woocommerce_price( $price_excl ); /* without VAT */ ?></p> (formatted)
        <p class="price-vat"><?php echo woocommerce_price( $tax_amount); /* VAT */ ?></p>
        <p class="price-and-vat"><?php echo woocommerce_price( $price_incl); /* With VAT  */ ?></p> 
    
        <meta itemprop="price" content="<?php echo esc_attr( $product->get_price() ); ?>" />
        <meta itemprop="priceCurrency" content="<?php echo esc_attr( get_woocommerce_currency() ); ?>" />
        <link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />
    </div>
    

    由于附加价格未格式化,您可能需要使用以下 woocommerce php 函数将一些其他元素与此附加价格混合:

    get_price_suffix( ) // Get the suffix to display after prices > 0.
    $currency = esc_attr( get_woocommerce_currency( ) ) // Get the currency code.
    get_woocommerce_currency_symbol( $currency ) // Get the currency symbol.
    get_tax_class( ) // Returns the tax class.
    get_tax_status( ) // Returns the tax status.
    

    参考:WooCommerce WC_Product class

    【讨论】:

    • 这适用于价格相同的产品(可变或简单)。但是,它没有显示价格范围,例如 WooCommerce 通常显示的价格范围为 8.95 英镑 - 9.95 英镑,其中变化有不同的价格。是否有可能为 inc/exc 实现相同的目标?税,还包括文本以使客户清楚?例如:价格 exc。增值税:8.95 英镑 - 9.95 英镑 含税价格增值税:10.74 英镑 - 11.94 英镑
    【解决方案3】:

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

    • Woocommerce:税收选项卡:在商店中显示价格/在购物车和结帐期间显示价格

    【讨论】:

    • 没有同时显示两者的选项,所以仍然不是所有情况的最佳答案
    【解决方案4】:

    是的,您无需编辑模板即可显示增值税。转到 Woocommerce 设置 > 增值税 > 价格显示后缀。您可以添加:{price_ exclude_tax} 以显示不含增值税。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 2022-01-13
      • 2014-06-07
      • 1970-01-01
      • 2010-11-01
      • 2013-04-08
      • 1970-01-01
      相关资源
      最近更新 更多