如何在产品循环中显示WooCommerce价格变化

时间:2019-12-02 13:41:06

标签: wordpress woocommerce

我在WooCommerce商店中显示的是不含增值税的价格。现在,我还想显示含增值税的价格。因此,我设置了以下代码:

    // Add price inc VAT to products loop
    function wbgoe_show_price_inc_VAT()
    {
        global $product;
        ?>
<span class="price">
    <?php echo wc_price(wc_get_price_including_tax($product)) . " <small class='woocommerce-price-suffix'>(" . __('inc. BTW', 'tendotools') . ")</small>"; ?>
</span>
<?php }

add_action('woocommerce_after_shop_loop_item_title', 'wbgoe_show_price_inc_VAT');


// Add price inc VAT to single product
function wbgoe_show_price_inc_VAT_sp()
{
    global $product; ?>
<p class="price">
    <?php echo wc_price(wc_get_price_including_tax($product)) . " <small class='woocommerce-price-suffix'>(" . __("inc. BTW", "tendotools") . ")</small>"; ?>
</p>
<?php }

add_action('woocommerce_single_product_summary', 'wbgoe_show_price_inc_VAT_sp');

我的问题是,对于可变产品,价格仅显示最低价格,而不是价格变化。 https://prnt.sc/q526ls

打印含增值税价格变动的正确代码是什么?

1 个答案:

答案 0 :(得分:0)

您可以使用类似的东西

// Add price inc VAT to products loop
function wbgoe_show_price_inc_VAT() {
    global $product;
    ?>
    <span class="price">
        <?php
        if ( $product->is_type( 'variable' ) ) {
            echo wc_price(wc_get_price_including_tax($product, array('price' => $product->get_variation_price( 'min' )))) . __(" – ", "tendotools"); 
            echo wc_price(wc_get_price_including_tax($product, array('price' => $product->get_variation_price( 'max' )))) . " <small class='woocommerce-price-suffix'>(" . __("inc. BTW", "tendotools") . ")</small>";          
        } else {
            echo wc_price(wc_get_price_including_tax($product)) . " <small class='woocommerce-price-suffix'>(" . __('inc. BTW', 'tendotools') . ")</small>";
        }
        ?>
    </span>
    <?php
}
add_action('woocommerce_after_shop_loop_item_title', 'wbgoe_show_price_inc_VAT', 10, 0);

// Add price inc VAT to single product
function wbgoe_show_price_inc_VAT_sp($array, $int = null ) {    
    global $product;
    ?>
    <p class="price">
        <?php
        if ( $product->is_type( 'variable' ) ) {
            echo wc_price(wc_get_price_including_tax($product, array('price' => $product->get_variation_price( 'min' )))) . __(" – ", "tendotools"); 
            echo wc_price(wc_get_price_including_tax($product, array('price' => $product->get_variation_price( 'max' )))) . " <small class='woocommerce-price-suffix'>(" . __("inc. BTW", "tendotools") . ")</small>"; 
        } else {
            echo wc_price(wc_get_price_including_tax($product)) . " <small class='woocommerce-price-suffix'>(" . __('inc. BTW', 'tendotools') . ")</small>";            
        }
        ?>
    </p>
    <?php
}
add_action('woocommerce_single_product_summary', 'wbgoe_show_price_inc_VAT_sp', 10, 2);