在 WooCommerce 购物车上显示含增值税和不含增值税的价格

Display prices with and without VAT on WooCommerce cart

我正在尝试在购物车页面上显示含增值税和不含增值税的价格。但是当我调用 price 变量给我零值时。

我使用了这段代码:

function sv_change_product_price_cart( $price, $cart_item, $cart_item_key ) {
    global $product;
    $price = $product->price ;
    $price_excl_tax = $price - round($price * ( 20 / 100 ), 2);
    $price_excl_tax = number_format($price_excl_tax, 2, ",", ".");
    $price = number_format($price, 2, ",", ".");

    $display_price = ' <span class="amount" >';
    $display_price .= '<span class=" eptax "><span class="nprice">£ '.  $price_excl_tax.'</span><small class="woocommerce-price-suffix" style="color:black" > ex VAT</p></small> ';
    $display_price .= '<span class=" ptax " ></small><span class="nprice">£ '. $price .'</span><small class="woocommerce-price-suffix" style="color:black"> inc VAT </p></small>';
    $display_price .= '</span>';
 
    return $display_price;
}
add_filter( 'woocommerce_cart_item_price', 'sv_change_product_price_cart', 10, 3 );

它给了我这个:

有什么建议吗?

要在购物车页面上显示价格,您可以在该特定列中使用 woocommerce_cart_item_price 过滤器挂钩。

使用的函数:


所以你得到:

function filter_woocommerce_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
    // Get the product object
    $product = $cart_item['data'];
    
    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        // Price without VAT
        $price_excl_tax = (float) wc_get_price_excluding_tax( $product );
        
        // Price with VAT
        $price_incl_tax = (float) wc_get_price_including_tax( $product );
        
        // Edit price html
        $price_html = wc_price( $price_excl_tax ) . '<span class="my-class">&nbsp;' . __( 'ex VAT', 'woocommerce' ) . '</span><br>';
        $price_html .= wc_price( $price_incl_tax ) . '<span class="my-class">&nbsp;' . __( 'inc VAT', 'woocommerce' ) . '</span>';
    }

    return $price_html;
}
add_filter( 'woocommerce_cart_item_price', 'filter_woocommerce_cart_item_price', 10, 3 );