gpt4 book ai didi

php - 在 WooCommerce 中显示销售车商品格式的价格范围

转载 作者:行者123 更新时间:2023-12-04 03:38:56 24 4
gpt4 key购买 nike

我尝试在购物车和结帐页面中显示每个单独产品的小计价格和销售价格。我已经设法使用以下代码将销售价格(24TL)添加到正常价格(33TL):
enter image description here

/**
* 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)。
经过一番挖掘,我发现以下价格过滤器在我的默认 cart.php 文件中将每件商品的价格显示为小计 - 在我使用上面的代码表单将销售价格添加到价格之前,价格以正确的方式显示。
    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 );
}
}
知道如何更改第一个代码块以将每件商品的产品价格显示为小计吗?

最佳答案

更新:您可以使用以下内容在销售价格格式化范围内显示购物车项目:

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 文件中。测试和工作。

关于php - 在 WooCommerce 中显示销售车商品格式的价格范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66407742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com