在 WooCommerce 中显示销售购物车项目格式的价格范围

Display on sale cart item formatted prices range in WooCommerce

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

/**
 * 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 文件。已测试并有效。