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

Woocommerce如何为商品设置替代原价的自定义价格,通过短代码展示分期金额

WooCommerce 单期分期金额短代码实现方案

可以通过自定义短代码实现该功能,完整实现方案如下:

  • 代码部署
    将以下代码添加到WordPress主题的functions.php文件,或者自定义功能插件的代码文件中即可:
// 注册单期分期金额短代码
add_shortcode('wc_single_installment_price', 'calc_wc_installment_price');
function calc_wc_installment_price() {
    // 校验WooCommerce运行环境和商品上下文
    if (!class_exists('WooCommerce') || !is_product()) return '';
    global $product;
    $origin_price = $product->get_price();
    if (empty($origin_price) || $origin_price <= 0) return '';
    // 单期金额计算逻辑:商品实际售价除以4
    $installment_price = $origin_price / 4;
    // 返回适配WooCommerce设置的格式化货币金额
    return wc_price($installment_price);
}
  • 短代码使用
    你可以在商品详情页、商品列表、自定义页面的对应位置,直接插入短代码[wc_single_installment_price],即可展示对应商品的单期分期金额。

代码会自动读取当前商品的实际售价完成计算,输出时自动适配WooCommerce后台设置的货币符号、小数位数规则,原价1500的商品将直接输出格式化后的375金额。

如果需要灵活调整分期期数,可以使用支持参数的扩展版本代码:

// 支持自定义期数的分期金额短代码
add_shortcode('wc_installment_price', 'calc_wc_installment_price');
function calc_wc_installment_price($atts) {
    $atts = shortcode_atts([
        'periods' => 4 // 默认分4期,可通过参数修改
    ], $atts);
    if (!class_exists('WooCommerce') || !is_product() || $atts['periods'] <= 0) return '';
    global $product;
    $origin_price = $product->get_price();
    if (empty($origin_price) || $origin_price <= 0) return '';
    $installment_price = $origin_price / $atts['periods'];
    return wc_price($installment_price);
}

使用时只需传入期数参数即可,例如分3期时使用短代码:[wc_installment_price periods="3"]


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 18:18:00