Woocommerce registeration check if referral code i

2019-08-01 10:46发布

I am looking to add a check to my woocommerce registration form to check if a referral code exists, that is set as a custom field (repeater) in the backend. It is currently set up with Advanced Custom Fields and set on an options page. It should check if code is in the repeater fields otherwise refuse registration. The code then needs to be added to the user and shown in the user backend.

I have tried to manipulate terms and conditions code on registration but havent got anywhere. Thanks

This is my ACF code to grab the valid codes for validation.

<?php

// check if the repeater field has rows of data
if( have_rows('referralcode', 'option') ):

    // loop through the rows of data
    while ( have_rows('referralcode', 'option') ) : the_row();

        // display a sub field value
        the_sub_field('referralvalue', 'option');

    endwhile;

else :
    // no rows found
endif;

?>

1条回答
太酷不给撩
2楼-- · 2019-08-01 11:30

Here is the way to do it:

1) Add referral registration field

add_action( 'woocommerce_register_form', 'add_referral_registration_field', 20 );
function add_referral_registration_field() {
    ?>
    <p class="form-row form-row-wide">
        <label for="reg_referralcode"><?php _e( 'Referral code', 'woocommerce' ); ?><span class="required"> *</span></label>
        <input type="text" class="input-text" name="referralcode" id="reg_referralcode" value="<?php if ( ! empty( $_POST['referralcode'] ) ) esc_attr_e( $_POST['referralcode'] ); ?>" />
    </p>
    <?php
}

2) Validate (or check) referral registration field…

Here you will add your repeater field code to check this referral code

add_action( 'woocommerce_register_post', 'referral_registration_field_validation', 20, 3 );
function referral_registration_field_validation( $username, $email, $validation_errors ) {
    $domain = 'woocommerce';
    $error  = '<strong>' . __( 'Error', $domain ) . '</strong>: ';

    // HERE you get the array of referral codes (to be replaced with ACF code)
    $referral_codes_to_check = array("ABCABC","DEFDEF");

    if ( isset( $_POST['referralcode'] ) && empty( $_POST['referralcode'] ) )
    {
        $validation_errors->add( 'referralcode_error', $error . __( 'Referral code is required!', $domain ) );
    }
    // HERE BELOW we check the referral code
    elseif( ! in_array( $_POST['referralcode'], $referral_codes_to_check ) )
    {
        $validation_errors->add( 'referralcode_error', $error . __( 'This referral code is not valid, please try something else…', $domain ) );
    }
}

3) Save referral field data

add_action('woocommerce_created_customer', 'save_referral_registration_field_data', 20, 1 );
function save_referral_registration_field_data( $customer_id ) {

    // Referral code field
    if ( isset( $_POST['referralcode'] ) )
        update_user_meta( $customer_id, 'referralcode', sanitize_text_field( $_POST['referralcode'] ) );
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

enter image description here

enter image description here


And if you wish to display the field (and update the field) in edit user account form:

1) Edit referral registration field:

add_action( 'woocommerce_edit_account_form', 'edit_referral_registration_field', 20 );
function edit_referral_registration_field() {
    $user = wp_get_current_user();
    ?>
        <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
        <label for="referralcode"><?php _e( 'Referral code', 'woocommerce' ); ?>
        <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="referralcode" id="referralcode" value="<?php echo esc_attr( $user->referralcode ); ?>" />
    </p>
    <?php
}

2) Update referral registration field:

add_action( 'woocommerce_save_account_details', 'save_referral_registration_field', 20, 1 );
function save_favorite_color_account_details( $user_id ) {

    if( isset( $_POST['referralcode'] ) )
        update_user_meta( $user_id, 'referralcode', sanitize_text_field( $_POST['referralcode'] ) );
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

enter image description here

查看更多
登录 后发表回答