問(wèn)題描述
在 woocommerce 注冊(cè)表中,注冊(cè)按鈕前沒(méi)有條款和條件".
有沒(méi)有辦法讓它出現(xiàn)在表單中?
這是
獲取注冊(cè)表單上的條款和條件復(fù)選框的代碼:
//在注冊(cè)表單上添加條款和條件復(fù)選框add_action('woocommerce_register_form', 'add_terms_and_conditions_to_registration', 20);函數(shù) add_terms_and_conditions_to_registration() {如果 (wc_get_page_id('terms') > 0 &&is_account_page()) {?><p class="form-row terms wc-terms-and-conditions"><label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox"><輸入類型=復(fù)選框"class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox";名稱=條款"<?php 已檢查( apply_filters( 'woocommerce_terms_is_checked_default', isset( $_POST['terms'] ) ), true );?>id=條款"/><span><?php printf( __( '我已經(jīng)閱讀并接受了 <a href="%s" target="_blank" class="woocommerce-terms-and-conditions-鏈接>條款&條件</a>', 'woocommerce'), esc_url(wc_get_page_permalink('terms')));?></span><span class="required">*</span>標(biāo)簽><輸入類型=隱藏"名稱=術(shù)語(yǔ)字段"值=1"/></p><?php}}//驗(yàn)證所需的條款和條件復(fù)選框add_action('woocommerce_register_post', 'terms_and_conditions_validation', 20, 3);函數(shù)terms_and_conditions_validation( $username, $email, $validation_errors ) {if ( !isset( $_POST['terms'] ) )$validation_errors->add( 'terms_error', __( '條款和條件未檢查!', 'woocommerce' ) );返回 $validation_errors;}
代碼位于活動(dòng)子主題(或活動(dòng)主題)的 function.php 文件中.經(jīng)測(cè)試有效.
未勾選復(fù)選框時(shí),將顯示錯(cuò)誤消息.
<塊引用>此復(fù)選框只是一個(gè)驗(yàn)證步驟(它不會(huì)保存在數(shù)據(jù)庫(kù)中,因?yàn)槲覀儾粫?huì)在任何地方使用該信息).
補(bǔ)充:
要僅在注冊(cè)頁(yè)面允許條款和條件,請(qǐng)使用以下方法之一:
add_filter('woocommerce_checkout_show_terms', '__return_false');
或
add_filter('woocommerce_get_terms_and_conditions_checkbox_text', '__return_false');
代碼位于活動(dòng)子主題(或活動(dòng)主題)的 function.php 文件中.已在 WC 3.5+ 上測(cè)試并有效.
In woocommerce registration form, there's no "terms and conditions" before sign up button.
Is there a way to make it appears in the form?
Here is the link of my theme layout.
Updated on july 2018 - Added compatibility for Woocommerce version 3.4+
If not done yet, first you need enable terms and conditions on checkout page:
- To create a new page in Wordpress for your terms and conditions
- To enable that page in Woocommerce > Settings > Checkout > Checkout pages (section):
- Then save… you are done.
The code to get the term and conditions check box on registration form:
// Add term and conditions check box on registration form
add_action( 'woocommerce_register_form', 'add_terms_and_conditions_to_registration', 20 );
function add_terms_and_conditions_to_registration() {
if ( wc_get_page_id( 'terms' ) > 0 && is_account_page() ) {
?>
<p class="form-row terms wc-terms-and-conditions">
<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="terms" <?php checked( apply_filters( 'woocommerce_terms_is_checked_default', isset( $_POST['terms'] ) ), true ); ?> id="terms" /> <span><?php printf( __( 'I’ve read and accept the <a href="%s" target="_blank" class="woocommerce-terms-and-conditions-link">terms & conditions</a>', 'woocommerce' ), esc_url( wc_get_page_permalink( 'terms' ) ) ); ?></span> <span class="required">*</span>
</label>
<input type="hidden" name="terms-field" value="1" />
</p>
<?php
}
}
// Validate required term and conditions check box
add_action( 'woocommerce_register_post', 'terms_and_conditions_validation', 20, 3 );
function terms_and_conditions_validation( $username, $email, $validation_errors ) {
if ( ! isset( $_POST['terms'] ) )
$validation_errors->add( 'terms_error', __( 'Terms and condition are not checked!', 'woocommerce' ) );
return $validation_errors;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
When checkbox is not ticked, an error message is displayed.
This checkbox is only a validation step (It is not saved in database, as we don't use that information anywhere).
Addition:
To allow terms and conditions only in Registration page use one of the following:
add_filter( 'woocommerce_checkout_show_terms', '__return_false' );
Or
add_filter( 'woocommerce_get_terms_and_conditions_checkbox_text', '__return_false' );
Code goes in function.php file of your active child theme (or active theme). Tested on WC 3.5+ and works.
這篇關(guān)于在 Woocommerce 注冊(cè)表中添加條款和條件復(fù)選框的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!