未达到最低订单金额时禁用WooCommerce结算按钮并修改CSS的方案咨询
实现方案
步骤1:移除插件原有重定向逻辑
原有插件的block_checkout方法挂载在wp动作上触发重定向,我们首先移除该默认逻辑,避免页面跳转:
add_action('wp', 'remove_alg_oma_block_checkout_redirect', 5); function remove_alg_oma_block_checkout_redirect() { global $alg_wc_oma; if (isset($alg_wc_oma)) { remove_action('wp', array($alg_wc_oma, 'block_checkout')); } }
步骤2:添加按钮禁用与样式修改逻辑
通过钩子动态判断购物车是否存在金额限制错误,直接给结算按钮添加禁用属性和灰化样式,完全不需要jQuery支持:
add_filter('woocommerce_order_button_html', 'disable_checkout_button_when_oma_error', 999); add_action('wp_head', 'add_oma_disabled_button_style'); function disable_checkout_button_when_oma_error($button_html) { // 仅购物车页面生效 if (!is_cart()) { return $button_html; } global $alg_wc_oma; $has_oma_error = false; if (isset($alg_wc_oma)) { $notices = $alg_wc_oma->messages->get_notices('cart'); $has_oma_error = !empty($notices); } // 存在错误时给按钮追加禁用属性和自定义样式类 if ($has_oma_error) { $button_html = str_replace('<a ', '<a disabled aria-disabled="true" class="oma-disabled-checkout" ', $button_html); } return $button_html; } function add_oma_disabled_button_style() { if (!is_cart()) { return; } ?> <style> .oma-disabled-checkout { pointer-events: none !important; opacity: 0.6 !important; cursor: not-allowed !important; background: #cccccc !important; border-color: #bbbbbb !important; } </style> <?php }
注意事项
- 以上代码添加到当前WordPress主题的
functions.php文件末尾,或自定义功能插件中即可生效 - 可根据网站设计风格调整
.oma-disabled-checkout类的样式参数,适配网站整体视觉 - 测试前请清理站点缓存、浏览器缓存避免旧代码干扰
内容的提问来源于stack exchange,提问作者Morgan Kennedy
相关产品推荐
相关产品推荐

