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

Magento 2:设置服务条款与条件(terms and conditions)为非必填项

Magento 2:将服务条款设置为非必填项的方法

可以将服务条款设置为非必填项,以下是两种常用实现方案:

方案一:通过自定义模块修改后端验证逻辑

这种方法从根源上取消条款的必填限制,前后端都会生效:

  • 创建自定义模块(示例命名为YourVendor/AgreementOptional),在模块目录下创建Block/Agreements/Agreement.php文件:
    <?php
    namespace YourVendor\AgreementOptional\Block\Agreements;
    
    class Agreement extends \Magento\Checkout\Block\Agreements\Agreement
    {
        /**
         * 重写方法,返回false取消必填要求
         * @return bool
         */
        public function getIsRequired()
        {
            return false;
        }
    }
    
  • 在模块的etc/di.xml中配置类重写:
    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <preference for="Magento\Checkout\Block\Agreements\Agreement" type="YourVendor\AgreementOptional\Block\Agreements\Agreement" />
    </config>
    
  • 执行以下命令启用模块并完成部署:
    bin/magento module:enable YourVendor_AgreementOptional
    bin/magento setup:upgrade
    bin/magento setup:di:compile
    bin/magento setup:static-content:deploy -f
    

方案二:前端JavaScript移除必填验证

如果不想开发模块,可通过前端JS临时移除验证(仅前端生效,后端仍可能有校验,适合快速测试):

  • 在当前主题的Magento_Checkout/layout/checkout_index_index.xml中添加自定义JS引用:
    <?xml version="1.0"?>
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <body>
            <referenceContainer name="content">
                <block class="Magento\Framework\View\Element\Template" name="checkout.agreements.custom.js" template="Magento_Checkout::agreements-custom.phtml"/>
            </referenceContainer>
        </body>
    </page>
    
  • 创建Magento_Checkout/templates/agreements-custom.phtml文件,添加以下JS代码:
    require(['jquery'], function($) {
        $(document).on('checkout:load', function() {
            // 移除所有条款勾选框的验证规则和必填类
            $('input[name^="agreement["]').each(function() {
                $(this).removeAttr('data-validate').removeClass('required-entry');
            });
        });
    });
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 02:35:23