Woocommerce 中含税和不含税的显示和样式价格

时间:2021-04-02 11:56:58

标签: php css woocommerce tax

我有一个关于设置 php 脚本样式并更改其显示方式的问题,但我无法正常工作 我使用此脚本在 Woocommerce 中显示包含和不包含 TAX 的价格。

我想首先显示不包括 TAX 的价格,它需要使用类 amountex 进行样式设置($price 值也是如此) 在不含税的价格后,我想在新行中显示含税的价格,这必须用班级金额设置

我进行了很多更改,但显示的价格没有样式,如果不先显示税金,我无法获得价格。

非常欢迎建议

这是我做了一些改动的部分

        if ( isset($price_incl_tax_html) && isset($price_excl_tax_html) ) {
                        $price_html  = '<span class="amount">' . $price_incl_tax_html . ' Incl. BTW </span><br>';
                        $price_html .= '<span class="amountex">' . $price_excl_tax_html . ' Excl. BTW </span><br>';
                        $price_html .= $product->get_price_suffix();
        }
    }
    return $price_html;

这是完整的代码

add_filter('woocommerce_get_price_html', 'display_prices_incl_and_excl_taxes', 100, 2 );
function display_prices_incl_and_excl_taxes( $price_html, $product ) {
    global $woocommerce_loop; {

        // For simple products and products variations
        if( $product->is_type('simple') || $product->is_type('variation') ) {
            // On sale products
            if( $product->is_on_sale() ) {
                $regular_price_incl_tax = wc_get_price_including_tax( $product, array( 'price' => $product->get_regular_price() ) );
                $price_incl_tax_html    = wc_format_sale_price( $regular_price_incl_tax, wc_get_price_including_tax( $product ) );
                $regular_price_excl_tax = wc_get_price_excluding_tax( $product, array( 'price' => $product->get_regular_price() ) );
                $price_excl_tax_html    = wc_format_sale_price( $regular_price_excl_tax, wc_get_price_excluding_tax( $product ) );
            }
            // Not on sale
            else {
                $price_incl_tax_html = wc_price( wc_get_price_including_tax( $product ) );
                $price_excl_tax_html = wc_price( wc_get_price_excluding_tax( $product ) );

            }
        }
        // variable pproducts
        elseif( $product->is_type('variable') ) {
            $prices = $product->get_variation_prices( true );

            if ( ! empty( $prices['price'] ) ) {
                $act_keys = array_keys($prices['price']);
                $reg_keys = array_keys($prices['regular_price']);

                $min_price_incl_tax = wc_get_price_including_tax( wc_get_product(reset($act_keys)));
                $max_price_incl_tax = wc_get_price_including_tax( wc_get_product(end($act_keys)));

                $min_price_excl_tax = wc_get_price_excluding_tax( wc_get_product(reset($act_keys)));
                $max_price_excl_tax = wc_get_price_excluding_tax( wc_get_product(end($act_keys)));

                $min_reg_price_jncl_tax = wc_get_price_including_tax( wc_get_product(reset($reg_keys)));
                $max_reg_price_incl_tax = wc_get_price_including_tax( wc_get_product(end($reg_keys)));

                $min_reg_price_excl_tax = wc_get_price_excluding_tax( wc_get_product(reset($reg_keys)));
                $max_reg_price_excl_tax = wc_get_price_excluding_tax( wc_get_product(end($reg_keys)));

                if ( $min_price_excl_tax !== $max_price_excl_tax ) {
                    $price_incl_tax_html = wc_format_price_range( $min_price_incl_tax, $max_reg_price_incl_tax );
                    $price_excl_tax_html = wc_format_price_range( $min_price_excl_tax, $max_reg_price_excl_tax );
                }
                elseif ( $product->is_on_sale() && $min_reg_price_excl_tax === $max_reg_price_excl_tax ) {
                    $price_incl_tax_html = wc_format_sale_price( wc_price( $max_reg_price_incl_tax ), wc_price( $min_price_incl_tax ) );
                    $price_excl_tax_html = wc_format_sale_price( wc_price( $max_reg_price_excl_tax ), wc_price( $min_price_excl_tax ) );
                }
                else {
                    $price_incl_tax_html = wc_price( $min_price_incl_tax );
                    $price_excl_tax_html = wc_price( $min_price_excl_tax );
                }
            }
        }
        if ( isset($price_incl_tax_html) && isset($price_excl_tax_html) ) {
                        $price_html  = '<span class="amount">' . $price_incl_tax_html . ' Incl. BTW </span><br>';
                        $price_html .= '<span class="amountex">' . $price_excl_tax_html . ' Excl. BTW </span><br>';
                        $price_html .= $product->get_price_suffix();
        }
    }
    return $price_html;
}

1 个答案:

答案 0 :(得分:0)

编辑。通过交换这两条规则来解决显示税收的顺序:)

我现在唯一不能工作的是第一条规则中 $price_excl_tax_html 的样式。我的课似乎被 woocommerce 课否决了:woocommerce-Price-amount

        if ( isset($price_incl_tax_html) && isset($price_excl_tax_html) ) {
                        $price_html  = '<bdi><span class="amountex">' . $price_excl_tax_html . ' Excl. BTW </span><bdi><br>';
                        $price_html .= '<bdi><span class="amount">' . $price_incl_tax_html . ' Incl. BTW </span><bdi><br>';
                        $price_html .= $product->get_price_suffix();
        }
    }
    return $price_html;