【问题标题】:Display and style prices with and without tax in Woocommerce在 Woocommerce 中显示和设置含税和不含税的价格
【发布时间】:2021-04-02 20:32:24
【问题描述】:

我有一个关于样式化 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;
}

【问题讨论】:

    标签: php css woocommerce tax


    【解决方案1】:

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

    我现在唯一不能工作的是第一条规则中 $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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 2019-04-19
      • 2014-06-07
      相关资源
      最近更新 更多