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

商品计算价格未同步至WooCommerce购物车及结算页求助

问题:黄金珠宝商城商品价格计算异常 - 详情页正常但购物车/结算页显示0.00美元

我们正在开发一家黄金珠宝商城,已编写代码根据商品所属24K/22K分类,结合每日金价与商品重量计算价格。该价格可在商品详情页正常显示,但添加至购物车及进入结算页时,显示的仍是0.00美元的基础价格,求指点遗漏之处。

附原代码:

add_filter( 'woocommerce_get_price_html', 'calculate_price_by_weight', 10, 2 );

function calculate_price_by_weight($price, $product){
    // define gold type prices
    $GOLD24K_PRICE = 98.00;
    $GOLD22K_PRICE = 92.00;
    
    // get the product weight and regular price
    $weight = $product->get_weight();
    $regular_price = $product->get_regular_price();
    
    // check if the product has a 24k and 22k category
    // 1. get product category list
    $product_categories = wc_get_product_category_list($product->ID);
    // 2. check if the category list has a 24k in it
    $has_24k_category = strpos(strtolower($product_categories), '24k') !== false;
    $has_22k_category = strpos(strtolower($product_categories), '22k') !== false;
    
    // get the final gold price base on the category
    if ($has_24k_category) {
        $gold_price = $GOLD24K_PRICE;
    } else if ($has_22k_category) {
        $gold_price = $GOLD22K_PRICE;
    } else {
        // non gold products
        return $regular_price;
    }
    
    // calculate product price based on gold type price, product weight
    $price = $gold_price * $weight;
    return $price;
    }

问题原因与修复方案

核心问题

你只使用了woocommerce_get_price_html钩子,这个钩子仅负责前端页面的价格显示渲染,不会修改商品在系统中的实际价格数据。购物车和结算页读取的是商品的原始价格(或购物车项目的价格字段),因此无法获取你计算后的价格。

修复步骤及优化代码

需要替换/补充负责修改商品实际价格的钩子,同时优化分类检测逻辑避免误判:

// 修改商品实际价格(影响购物车、结算及所有价格读取场景)
add_filter( 'woocommerce_product_get_price', 'calculate_gold_product_price', 10, 2 );
add_filter( 'woocommerce_product_get_regular_price', 'calculate_gold_product_price', 10, 2 );

// 保持前端价格的格式化显示(可选,若无需自定义格式可移除)
add_filter( 'woocommerce_get_price_html', 'calculate_gold_product_price_html', 10, 2 );

function calculate_gold_product_price($price, $product){
    // 定义24K/22K基础金价
    $GOLD24K_PRICE = 98.00;
    $GOLD22K_PRICE = 92.00;
    
    $weight = $product->get_weight();
    // 处理商品未设置重量的情况,返回原始价格
    if( empty($weight) ) return $price;
    
    // 获取商品分类slug数组,避免字符串匹配的误判
    $category_slugs = array_map('strtolower', wp_list_pluck($product->get_categories(), 'slug'));
    
    $is_24k = in_array('24k', $category_slugs);
    $is_22k = in_array('22k', $category_slugs);
    
    if ($is_24k) {
        return $GOLD24K_PRICE * $weight;
    } elseif ($is_22k) {
        return $GOLD22K_PRICE * $weight;
    }
    
    // 非金饰商品返回原始价格
    return $price;
}

// 可选:统一价格显示格式(自动添加货币符号)
function calculate_gold_product_price_html($price_html, $product){
    $calculated_price = calculate_gold_product_price($product->get_price(), $product);
    return wc_price($calculated_price);
}

关键说明

  1. 钩子选择:woocommerce_product_get_price和woocommerce_product_get_regular_price会直接修改商品在系统中的价格数值,所有依赖商品价格的模块(购物车、结算、订单)都会使用这个计算后的结果。
  2. 分类检测优化:改用get_categories获取分类slug数组,避免通过分类列表字符串匹配导致的误判(比如分类名称包含"24k"但并非目标分类的情况)。
  3. 边界处理:增加了重量为空的判断,防止因商品未设置重量导致计算出0价格。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 20:55:38