在WooCommerce中显示销售车上已格式化项目的价格范围 [英] Display on sale cart item formatted prices range in WooCommerce
问题描述
我尝试在购物车和结帐页面中显示每种产品的小计价格和销售价格.我已经设法通过以下代码将销售价格(24TL)添加到正常价格(33TL):
I try to show the subtotal price and sale price in the Cart and Checkout page for each individual product. I already managed to add the sales price (24TL) to the regular price (33TL) with the following code:
/**
* Show sale prices on the Cart and Checkout pages
*/
function my_custom_show_sale_price( $old_display, $cart_item, $cart_item_key ) {
$price_string = $old_display;
$product = $cart_item['data'];
if ( $product ) {
$price_string = $product->get_price_html();
}
return $price_string;
}
add_filter( 'woocommerce_cart_item_price', 'my_custom_show_sale_price', 10, 3 );
add_filter( 'woocommerce_cart_item_subtotal', 'my_custom_show_sale_price', 10, 3 );
不幸的是,上面的代码仅显示了一件商品的价格,而不是购物车或结帐页面中的小计(在这种情况下,商品价格应为66TL/48TL).
Unfortunately the above code shows the product price only for one piece not for the subtotal (in this case it should be 66TL / 48TL) in the cart or checkout page.
经过一番挖掘,我发现以下价格过滤器在我的默认cart.php文件中以小计形式显示了每件商品的价格-在我使用上述代码形式向其添加销售价格之前,价格显示了正确的方式.
After some digging i found the following price filter to show the price per item as subtotal in my default cart.php file - where the price displayed the correct way before i added the sales price to it with the code form above.
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key );
}
}
有什么想法如何更改第一段代码以将每件产品的价格显示为小计?
Any idea how to change the first block of code to show the product price per item as subtotal?
推荐答案
已更新:您可以使用以下内容来显示销售价格格式范围内的购物车商品:
Updated: You can use the following to display cart item on sale prices formatted range:
add_filter( 'woocommerce_cart_item_price', 'filter_cart_item_price', 10, 3 );
function filter_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
if( $cart_item['data']->is_on_sale() ) {
return $cart_item['data']->get_price_html();
}
return $price_html;
}
add_filter( 'woocommerce_cart_item_subtotal', 'filter_cart_item_subtotal', 10, 3 );
function filter_cart_item_subtotal( $subtotal_html, $cart_item, $cart_item_key ){
$product = $cart_item['data'];
$quantity = $cart_item['quantity'];
$tax_string = '';
if ( $product->is_taxable() ) {
if ( WC()->cart->display_prices_including_tax() ) {
$regular_price = wc_get_price_including_tax( $product, array( 'qty' => $quantity, 'price' => $product->get_regular_price() ) );
$active_price = wc_get_price_including_tax( $product, array( 'qty' => $quantity ) );
if ( ! wc_prices_include_tax() && WC()->cart->get_subtotal_tax() > 0 ) {
$tax_string = ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
}
} else {
$regular_price = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity, 'price' => $product->get_regular_price() ) );
$row_price = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity ) );
if ( wc_prices_include_tax() && WC()->cart->get_subtotal_tax() > 0 ) {
$tax_string = ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
}
} else {
$regular_price = $product->get_regular_price() * $quantity;
$active_price = $product->get_price() * $quantity;
}
if( $product->is_on_sale() ) {
return wc_format_sale_price( $regular_price, $active_price ) . $product->get_price_suffix() . $tax_string;
}
return $subtotal_html;
}
代码进入活动子主题(或活动主题)的functions.php文件中.经过测试并可以正常工作.
Code goes in functions.php file of the active child theme (or active theme). tested and works.
这篇关于在WooCommerce中显示销售车上已格式化项目的价格范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!