仅在某些产品类别中显示不含税的价格

时间:2017-08-03 16:40:12

标签: php wordpress woocommerce categories product

产品类别为“机器”

我的网站是:“https://www.floorcare-supplies.co.uk/product-category/machines/

我希望能够在此类别中显示产品,而无需在店面上征税。

我曾在产品上设法做到这一点,例如:

£1342

£1123 Ex vat

但是在商店商店,我需要主要价格才能显示该特定类别的免税总额。

3 个答案:

答案 0 :(得分:0)

使用此代码:

/**
 * Function that will check for category id and turn off VAT/tax 
 */
function wc_diff_rate_for_cat() {
    if (is_product_category ('machines')) {
        // set the machines category to have no VAT
        WC()->customer->is_vat_exempt = true;
    }
}
add_action( 'template_redirect', 'wc_diff_rate_for_cat', 1 );

/**
 * Function that filters the variable product hash based on category
 */
function wc_get_variation_prices_hash_filter( $hash, $item, $display ) {

    // check for the "machines" category
    if (is_product_category ('machines')) {

        // clear key 2, which is where taxes are
        $hash['2'] = array();
    }

    // return the hash
    return $hash;
}
add_filter( 'woocommerce_get_variation_prices_hash', 'wc_get_variation_prices_hash_filter', 1, 3 );

/**
 * Function that removes the price suffix (inc. Tax) from variable products based on category
 */
function wc_get_price_suffix_filter( $price_display_suffix, $item ) {

    if (is_product_category ('machines')) {

        // return blank if it matches
        return '';
    }

    // return if unmatched
    return $price_display_suffix;
}
add_filter( 'woocommerce_get_price_suffix', 'wc_get_price_suffix_filter', 10, 2 );

答案 1 :(得分:0)

解决了

解决方案

修改后的儿童主题包含

Woocommerce \包括\ WC-模板的functions.php

&安培;

woocommerce \环\ price.php

代码已添加到 Woocommerce \包括\ WC-模板的functions.php

/**
 * Get the product price for the loop.
 *
 * @subpackage  Loop
 */
function woocommerce_template_loop_price() {
    wc_get_template( 'loop/pricemod.php' );

}

然后修改pricemod.php

global $product;
$vat = "Ex vat ";
?>
<p class="price"><?php echo $product->get_price_html(); ?></p>
<p class="priceex"><?php echo woocommerce_price($product->get_price_excluding_tax()). ' ' .  $vat; ?></p>

答案 2 :(得分:0)

仅为您的&#34;计算机显示不含税的价格&#34;产品类别,您应该使用隐藏在 wc_price 过滤器挂钩中的自定义函数,这样:

add_filter( 'wc_price', 'product_category_price_excl_tax', 10, 3 );
function product_category_price_excl_tax(  $price_html, $price, $args ){
    global $post;

    // Price excluding taxes for "Machines" product category with corresponding label
    if( has_term( 'Machines', 'product_cat', $post->ID ) ){

        $decimal_separator  = wc_get_price_decimal_separator();
        $thousand_separator = wc_get_price_thousand_separator();
        $decimals           = wc_get_price_decimals();
        $price_format       = get_woocommerce_price_format();
        $number_format      = number_format( $price, $decimals, $decimal_separator, $thousand_separator );
        $negative           = $price < 0;
        $currency = get_woocommerce_currency();

        // Get an instance of the WC_Product object
        $product = wc_get_product($post->ID);

        // Get the product price excluding taxes
        $price = wc_get_price_excluding_tax( $product, $price );

        $price = apply_filters( 'raw_woocommerce_price', floatval( $negative ? $price * -1 : $price ) );
        $price = apply_filters( 'formatted_woocommerce_price', $number_format, $price, $decimals, $decimal_separator, $thousand_separator );

        if ( apply_filters( 'woocommerce_price_trim_zeros', false ) && $decimals > 0 )
            $price = wc_trim_zeros( $price );

        $formatted_price = ( $negative ? '-' : '' ) . sprintf( $price_format, '<span class="woocommerce-Price-currencySymbol">' . get_woocommerce_currency_symbol( $currency ) . '</span>', $price );
        $price_html      = '<span class="woocommerce-Price-amount amount">' . $formatted_price . '</span>';

        if ( wc_tax_enabled() )
            $price_html .= ' <small class="woocommerce-Price-taxLabel tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
    }
    return $price_html;
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

此代码已经过测试,适用于WooCommerce版本3 +

现在,要在&#34; Machines&#34;中显示的价格商店类别档案页面,您将需要替换:

    if( has_term( 'Machines', 'product_cat', $post->ID ) ){

由:

    if( has_term( 'Machines', 'product_cat', $post->ID ) && is_product_category ('machines') ){