如何为WooCommerce结账页添加带链接的退换货政策复选框
解决WooCommerce退换货政策复选框添加链接及弹窗问题
你需要修改复选框的label内容,插入退换货政策页面的链接,并复用WooCommerce自带的弹窗样式逻辑,具体代码调整如下:
add_action( 'woocommerce_review_order_before_submit', 'bbloomer_add_checkout_returns_policy', 9 ); function bbloomer_add_checkout_returns_policy() { // 获取退换货政策页面的链接,替换为你的页面slug或ID $returns_policy_link = get_permalink( get_page_by_path( 'returns-refunds-policy' ) ); // 构建带链接的标签文本,复用WooCommerce弹窗类 $label_text = sprintf( 'I have read and agree to the <a href="%s" class="woocommerce-terms-and-conditions-link" target="_blank">Returns and Refunds policy</a>', esc_url( $returns_policy_link ) ); woocommerce_form_field( 'returns_refunds_policy', array( 'type' => 'checkbox', 'class' => array('form-row privacy'), 'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'), 'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'), 'required' => true, 'label' => wp_kses_post( $label_text ), // 安全处理HTML内容 )); } // 添加验证逻辑,确保用户勾选 add_action( 'woocommerce_checkout_process', 'bbloomer_validate_returns_policy_checkbox' ); function bbloomer_validate_returns_policy_checkbox() { if ( ! isset( $_POST['returns_refunds_policy'] ) ) { wc_add_notice( __( 'Please agree to the Returns and Refunds policy.', 'woocommerce' ), 'error' ); } }
关键说明:
- 替换
returns-refunds-policy为你实际的退换货政策页面的slug(可在后台页面编辑页查看URL末尾的内容);若知道页面ID,也可直接使用get_permalink( 页面ID )。 - 链接添加了
woocommerce-terms-and-conditions-link类,该类是WooCommerce默认用来触发条款弹窗的,和服务条款的弹窗逻辑完全一致。 - 新增验证逻辑,确保用户必须勾选该复选框才能完成结账,匹配服务条款的必填规则。
- 使用
wp_kses_post处理标签文本,保障HTML内容的安全性,避免XSS风险。
若弹窗未自动生效,检查你的主题是否加载了WooCommerce核心JS文件(woocommerce.min.js),该文件包含条款弹窗的触发逻辑。
内容的提问来源于stack exchange,提问作者Grace
相关产品推荐
相关产品推荐

