Pre-fill Woocommerce checkout fields with Url variables saved in session

前端 未结 2 1695
半阙折子戏
半阙折子戏 2020-12-12 04:58

When people enter the my woocommerce shop following a link in an sales email with email and name as parameters I would like to prefill the name and email in the checkout pag

相关标签:
2条回答
  • 2020-12-12 05:29

    Here below, you will find the correct working code, to save user data from an URL (GET) into WooCommerce sessions and to autofill with that data the related checkout fields.

    URL will be like: http://example.com/sales-page/?tu_em=name@example.com&tu_name=theFirstName

    This can be done from any URL as the code will detect the URL variable, when they are set.

    The code;

    // Save user data from URL to Woocommerce session
    add_action( 'template_redirect', 'set_custom_data_wc_session' );
    function set_custom_data_wc_session () {
        if ( isset( $_GET['tu_em'] ) || isset( $_GET['tu_name'] ) ) {
            $em   = isset( $_GET['tu_em'] )   ? esc_attr( $_GET['tu_em'] )   : '';
            $name = isset( $_GET['tu_name'] ) ? esc_attr( $_GET['tu_name'] ) : '';
    
            // Set the session data
            WC()->session->set( 'custom_data', array( 'email' => $em, 'name' => $name ) );
        }
    }
    
    // Autofill checkout fields from user data saved in Woocommerce session
    add_filter( 'woocommerce_billing_fields' , 'prefill_billing_fields' );
    function prefill_billing_fields ( $address_fields ) {
        // Get the session data
        $data = WC()->session->get('custom_data');
    
        // Email
        if( isset($data['email']) && ! empty($data['email']) )
            $address_fields['billing_email']['default'] = $data['email'];
    
        // Name
        if( isset($data['name']) && ! empty($data['name']) )
            $address_fields['billing_first_name']['default'] = $data['name'];
    
        return $address_fields;
    }
    

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

    0 讨论(0)
  • 2020-12-12 05:49

    Shipping information(send to another address) by default is auto we use that for autofill billing fildes

    add_filter( 'woocommerce_checkout_fields' , 'ahmadyani_checkout_field_defaults', 20 );
    function ahmadyani_checkout_field_defaults( $fields ) {
        $user = get_user_meta(get_current_user_id());
        $first_name = $user ? $user['shipping_first_name'][0] : '';
        $last_name = $user ? $user['shipping_last_name'][0] : '';
        $company = $user ? $user['shipping_company'][0] : '';
        $shipping_address_1 = $user ? $user['shipping_address_1'][0] : '';
        $shipping_address_2 = $user ? $user['shipping_address_2'][0] : '';
        $shipping_city = $user ? $user['shipping_city'][0] : '';
        $shipping_state = $user ? $user['shipping_state'][0] : '';
        $shipping_postcode = $user ? $user['shipping_postcode'][0] : '';
        $fields['billing']['billing_first_name']['default'] = $first_name;
        $fields['billing']['billing_last_name']['default'] = $last_name;
        $fields['billing']['billing_company']['default'] = $company;
        $fields['billing']['billing_address_1']['default'] = $shipping_address_1;
        $fields['billing']['billing_address_2']['default'] = $shipping_address_2;
        $fields['billing']['billing_city']['default'] = $shipping_city;
        $fields['billing']['billing_state']['default'] = $shipping_state;
        $fields['billing']['billing_postcode']['default'] = $shipping_postcode;
        return $fields;
    }
    
    0 讨论(0)
提交回复
热议问题