You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Woocommerce商品动态折扣问题:购物车总计未显示折扣价

Fix for Dynamic Sale Price Not Reflecting in WooCommerce Cart Total

Hey Jules, great work getting the dynamic sale price setup mostly working! The missing piece here is ensuring WooCommerce uses your calculated sale price when computing cart totals—right now, the display might show the sale price, but the underlying total calculation still relies on the original price. Let's adjust your code to fix that.

Updated Full Code

Replace your existing code with this refined version:

// Generating dynamically the product "regular price"
add_filter( 'woocommerce_product_get_regular_price', 'custom_dynamic_regular_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_regular_price', 'custom_dynamic_regular_price', 10, 2 );
function custom_dynamic_regular_price( $regular_price, $product ) {
    if( empty($regular_price) || $regular_price == 0 ) 
        return $product->get_price();
    else 
        return $regular_price;
}

// Generating dynamically the product "sale price"
add_filter( 'woocommerce_product_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
function custom_dynamic_sale_price( $sale_price, $product ) {
    $rate = 0.7; // 30% discount (adjust this value as needed)
    if( empty($sale_price) || $sale_price == 0 ) 
        return $product->get_regular_price() * $rate;
    else 
        return $sale_price;
};

// Displayed formatted regular price + sale price
add_filter( 'woocommerce_get_price_html', 'custom_dynamic_sale_price_html', 20, 2 );
function custom_dynamic_sale_price_html( $price_html, $product ) {
    if( $product->is_type('variable') ) 
        return $price_html;

    $regular_price_display = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
    $sale_price_display = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
    
    $price_html = wc_format_sale_price( $regular_price_display, $sale_price_display ) . $product->get_price_suffix();
    return $price_html;
}

// Update cart item prices to use dynamic sale price for calculations
add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_sale_price', 10, 1 );
function set_cart_item_sale_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // Prevent infinite loops from repeated trigger of this action
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;

    // Iterate through each cart item
    foreach( $cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];
        
        // Fetch our dynamic sale price
        $sale_price = $product->get_sale_price();
        
        if ( $sale_price ) {
            // Set both sale price and active price to ensure totals use this value
            $product->set_sale_price( $sale_price );
            $product->set_price( $sale_price );
            
            // Handle variable product variations to maintain price consistency
            if ( $product->is_type('variation') ) {
                $product->set_regular_price( $product->get_regular_price() ); // Preserve original regular price
            }
        }
    }
}

// Update cart table price display to show sale price formatting
add_filter( 'woocommerce_cart_item_price', 'bbloomer_change_cart_table_price_display', 30, 3 );
function bbloomer_change_cart_table_price_display( $price, $values, $cart_item_key ) {
    $product = $values['data'];
    $is_on_sale = $product->is_on_sale();
    
    if ( $is_on_sale ) {
        // Match product page's strike-through price formatting
        $regular_price_display = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
        $sale_price_display = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
        $price = wc_format_sale_price( $regular_price_display, $sale_price_display ) . $product->get_price_suffix();
    }
    return $price;
}

Key Fixes Breakdown

  • Infinite Loop Prevention: Added a check to avoid repeated execution of the price calculation hook, which can cause unexpected errors.
  • Explicit Price Assignment: In the set_cart_item_sale_price function, we now set both set_sale_price and set_price—this ensures WooCommerce uses the dynamic sale price for all total calculations, not just display.
  • Variation Support: Added handling for variable product variations to maintain consistent pricing across all product types.
  • Display Consistency: Updated the cart item price display to mirror the product page's strike-through formatting, so users see the same price presentation everywhere.

How to Test

  1. Replace your existing code with the version above
  2. Check product listings to confirm dynamic sale prices show correctly
  3. Add items to cart and verify both individual item prices and the cart total use the sale price
  4. Proceed to checkout to ensure the final order total reflects the dynamic discounts

内容的提问来源于stack exchange,提问作者user2197774

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:56:04