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

WooCommerce单品页仅10美元以上商品显示分期提示的代码修改咨询

代码修改方案

你只需要在原有逻辑中增加价格阈值判断,当商品价格低于10美元时直接终止函数执行,即可实现需求,同时可以顺带优化原有代码中重复获取商品对象的冗余逻辑。

修改后的完整代码

add_action( 'woocommerce_single_product_summary', 'show_emi', 1 );
function show_emi() {
    global $product; 
    // 直接使用全局获取的商品对象,无需重复查询
    $product_price = $product->get_price();
    // 定义分期提示最低展示价格,单位为美元
    $min_emi_price = 10;
    // 价格低于阈值直接返回,不输出分期内容
    if ($product_price < $min_emi_price) {
        return;
    }
    $installment_month = 20;
    $monthly_pay = $product_price / $installment_month;
    echo '<div class="afbetaling">
    <b>  Eller kun '.$monthly_pay.' kr./md i 20 mdr. </b>
0% i rente og gebyrer. <br> Få mere info i butikken</div>';
}

核心修改说明

  • 新增了$min_emi_price变量存放分期展示的最低价格阈值,后续要调整阈值直接修改这个变量即可
  • 增加了价格判断逻辑,商品价格低于10美元时直接通过return终止函数后续执行,不会输出分期提示内容
  • 优化了原有代码中冗余的商品对象查询逻辑,原有代码已经通过global $product拿到了当前商品对象,无需再通过ID二次查询商品,提升执行效率

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 16:27:03