【问题标题】:Displaying prices without tax only in certain product categories仅在某些产品类别中显示不含税价格
【发布时间】:2017-08-03 16:40:11
【问题描述】:

产品类别是“机器”

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

我希望能够专门展示此类别中的产品,而无需在店面征税。

例如,我在产品上已经做到了:

1,342 英镑

1123 英镑不含税

但在商店商店中,我需要主要价格来显示该特定类别的不含税总额。

【问题讨论】:

    标签: php wordpress woocommerce categories product


    【解决方案1】:

    使用此代码:

    /**
     * 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 );
    

    【讨论】:

    • 谢谢您的回答,但这不起作用,我也不希望类别免税,我只是希望它显示不含税的价格而不是商店上的价格
    【解决方案2】:

    解决了

    解决方案

    修改后的子主题以包含

    Woocommerce\includes\wc-template-functions.php

    &

    woocommerce\loop\price.php

    代码添加到 Woocommerce\includes\wc-template-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>
    

    【讨论】:

    • 请注意,但 这不是一个好的解决方案,因为您要覆盖核心文件:Woocommerce\includes\wc-template-functions.php ...正确的方法是使用钩子'wc_price'。此外,在您的解决方案中,您的目标不是“机器”产品类别。
    【解决方案3】:

    仅显示您的“机器”产品类别的不含税价格,您应该使用挂钩在 wc_price 过滤器挂钩中的自定义函数,这样: p>

    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+


    现在,要在“机器”商店类别档案页面中显示该价格,您需要替换:

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

    作者:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-03
      • 2014-06-07
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 2013-04-08
      • 2021-12-24
      相关资源
      最近更新 更多