繁体   English   中英

Woocommerce中含税和不含税的展示和款式价格

[英]Display and style prices with and without tax in Woocommerce

我有一个关于设置 php 脚本的样式并更改其显示方式的问题,但我无法使其正常运行

我想先显示不含税的价格,它需要使用 class amountex (也是 $price 值)在不含税的价格之后我想在新行上显示含税的价格,这必须使用class 数量

我做了很多更改,但显示的价格没有样式,而且我无法在没有税的情况下获得价格。

非常欢迎建议

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

        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;
}

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

我现在唯一不能工作的是第一条规则中 $price_excl_tax_html 的样式。 我的 class 似乎被 woocommerce class 否决: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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM