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

Woocommerce技术求助:仅在店铺页显示批发价÷6的商品单价

解决Woocommerce店铺页显单价、产品页显批发价的问题

嘿,这个需求我刚好折腾过,给你两个靠谱的实现方案,都是基于Woocommerce的钩子机制,完全不用动核心文件,安全又好维护:

方案一:仅修改价格显示文案(推荐,不影响结算逻辑)

这个方法适合你只需要改变前端显示,后台价格和购物车结算依然沿用批发价的场景。假设你已经给商品设置了批发价(存在自定义元字段_wholesale_price,如果你的字段名不一样,记得替换成自己实际用的),把下面的代码加到主题的functions.php或者用代码片段插件添加:

add_filter('woocommerce_get_price_html', 'custom_price_display_by_page', 10, 2);
function custom_price_display_by_page($price, $product) {
    // 获取商品的批发价,替换成你实际的元字段名
    $wholesale_price = get_post_meta($product->get_id(), '_wholesale_price', true);
    
    // 验证批发价是否有效
    if (!empty($wholesale_price) && is_numeric($wholesale_price)) {
        $unit_price = $wholesale_price / 6;
        $formatted_unit_price = wc_price($unit_price);
        
        // 判断当前页面类型
        if (is_shop() || is_product_category() || is_product_tag()) {
            // 店铺页、分类页、标签页显示单价
            return __('单价: ', 'woocommerce') . $formatted_unit_price;
        } elseif (is_product()) {
            // 产品详情页显示批发价
            return __('批发价: ', 'woocommerce') . wc_price($wholesale_price);
        }
    }
    
    // 没有批发价时返回默认价格
    return $price;
}

注意事项:

  • 如果你用的是第三方批发插件(比如WooCommerce Wholesale Prices),批发价的元字段可能是_wc_wholesale_price,记得对应修改代码里的字段名。
  • 可以自定义价格前的文案,比如把单价:改成你店铺的专属表述。

方案二:修改价格计算逻辑(适合需要按显示价格结算的场景)

如果你的需求是店铺页下单时也按单价(批发价÷6)结算,那可以用这个方案,它会修改产品的实际价格取值:

// 修改产品价格取值
add_filter('woocommerce_product_get_price', 'custom_product_price_by_page', 10, 2);
function custom_product_price_by_page($price, $product) {
    $wholesale_price = get_post_meta($product->get_id(), '_wholesale_price', true);
    
    if (!empty($wholesale_price) && is_numeric($wholesale_price)) {
        if (is_shop() || is_product_category() || is_product_tag()) {
            return $wholesale_price / 6;
        } elseif (is_product()) {
            return $wholesale_price;
        }
    }
    return $price;
}

// 同步更新价格显示文案
add_filter('woocommerce_get_price_html', 'custom_price_html_by_page', 10, 2);
function custom_price_html_by_page($price_html, $product) {
    $wholesale_price = get_post_meta($product->get_id(), '_wholesale_price', true);
    
    if (!empty($wholesale_price) && is_numeric($wholesale_price)) {
        if (is_shop() || is_product_category() || is_product_tag()) {
            return __('单价: ', 'woocommerce') . wc_price($wholesale_price / 6);
        } elseif (is_product()) {
            return __('批发价: ', 'woocommerce') . wc_price($wholesale_price);
        }
    }
    return $price_html;
}

怎么选方案?

  • 只是前端展示不同价格,结算还是用批发价:选方案一
  • 店铺页下单按单价结算,产品页按批发价结算:选方案二

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:34:16