添加退货和退款政策复选框,并在 WooCommerce 条款和条件复选框下方添加链接

问题描述 投票:0回答:1

我想在 WooCommerce 的条款和条件复选框下方添加退货和退款政策复选框。我已经创建了退货和退款页面,我想要一个与条款和条件类似的功能,其中单击链接后会在下面出现一个框。

我尝试在代码片段中添加此代码:

add_action( 'woocommerce_review_order_before_submit', 'bbloomer_add_checkout_privacy_policy', 9 );
    
function bbloomer_add_checkout_privacy_policy() {
   
woocommerce_form_field( 'privacy_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'         => 'I have read and agree to the Returns and Refunds policy',
)); 
   
}

该复选框是在条款和条件下方形成的,但我不知道如何创建退货和退款的链接。关于如何解决这个问题有什么建议吗?谢谢你。

php wordpress woocommerce checkbox checkout
1个回答
0
投票

要在 WooCommerce 上的条款和条件复选框下方添加指向自定义“退货和退款政策”复选框的链接,并添加所需的验证,请使用以下内容 (您将在其中定义“退货和退款政策”页面链接 Url) :

// Add a custom checkbox to checkout
add_action( 'woocommerce_review_order_before_submit', 'add_custom_checkout_privacy_policy', 9 ); 
function add_custom_checkout_privacy_policy() {
    // Here define the privacy policy link page URL
    $page_link = site_url('/privacy-policy/');

    $privacy_text = sprintf( __("I have read and agree to the website %s", 'woocommerce'), 
        '<a href="'. $page_link .'">'. __("returns and refunds policy", 'woocommerce') . '</a>');
   
    printf('<p class="form-row privacy validate-required privacy_policy_field">
    <label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox">
        <input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="privacy_policy" id="privacy_policy" %s />
        <span class="woocommerce-terms-and-conditions-checkbox-text">%s</span>
        &nbsp;<abbr class="required" title="%s">*</abbr>
    </label></p>', checked( isset($_POST['privacy_policy']), true, false ), 
    $privacy_text, esc_attr__( 'required', 'woocommerce' ) );
}

// Checkbox validation as it's a required field
add_action( 'woocommerce_checkout_process', 'validate_custom_checkout_privacy_policy' ); 
function validate_custom_checkout_privacy_policy() {
    if ( ! isset($_POST['privacy_policy']) ) {
        wc_add_notice('Please read and accept the returns and refunds policy to proceed with your order.', 'error');
    }
}

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。

enter image description here


enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.