Populate Store Name and Address Fields from Custom Post Type

We're shifting our Forum based support to a more dedicated support system!

We'll be closing our Forum support from 10th June, 2020 and move to Email Support assistance.

  • If you are a WCFM premium add-ons user, contact us- here
  • Want to know more before buying our add-ons? Send Pre sale queries- here
  • If you are a WCFM free user, please open a support ticket at WordPress.org
  • For WCFM App related queries, reach us- here
From now the forum will be read-only!

Viewing 3 reply threads
  • Author
    Posts
    • #118892
      csheriff
      Participant

      I have a custom post type “business”. The users (business owners) login to the website and click on the “Become a Vendor”.
      What code can I use in functions.php to use the Business “title” and “address” as the Store Name and Store Address so that
      the users do not have to re-enter this information when they register to become a vendor?

    • #118961

      Hello,

      Add this code in your theme’s functions.php

      // Function to check starting char of a string
      function startsWith($haystack, $needle){
          return $needle === '' || strpos($haystack, $needle) === 0;
      }
      // Custom function to display the Billing Address form to registration page
      function zk_add_billing_form_to_registration(){
          global $woocommerce;
          $checkout = $woocommerce->checkout();
          ?>
          
          <?php foreach ( $checkout->get_checkout_fields( 'billing' ) as $key => $field ) : ?>
      
              <?php if($key!='billing_email' && $key!='billing_phone' && $key!='billing_company'){ 
                  woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
              } ?>
      
          <?php endforeach;  ?>
          <p class="form-row form-row-first">
             <label for="reg_store_name"><?php _e( 'Business Title', 'woocommerce' ); ?><span class="required">*</span></label>
             <input type="text" class="input-text" name="store_name" id="reg_store_name" value="<?php if ( ! empty( $_POST['store_name'] ) ) esc_attr_e( $_POST['store_name'] ); ?>" />
             </p>
          <?php
      }
      add_action('woocommerce_register_form_start','zk_add_billing_form_to_registration');
      
      // Custom function to save Usermeta or Billing Address of registered user
      function zk_save_billing_address($user_id){
          global $woocommerce;
          $address = $_POST;
          if ( ! empty( $_POST['store_name'] ) ) {
          	update_user_meta( $user_id, 'store_name', $_POST['store_name'] );
          }
          foreach ($address as $key => $field){
              if(startsWith($key,'billing_')){
                  // Condition to add firstname and last name to user meta table
                  if($key == 'billing_first_name' || $key == 'billing_last_name'){
                      $new_key = explode('billing_',$key);
                      update_user_meta( $user_id, $new_key[1], $_POST[$key] );
                  }
                  update_user_meta( $user_id, $key, $_POST[$key] );
              }
          }
      
      }
      add_action('woocommerce_created_customer','zk_save_billing_address');
      
      // Registration page billing address form Validation
      function zk_validation_billing_address(){
          global $woocommerce;
          $address = $_POST;
          foreach ($address as $key => $field) :
              // Validation: Required fields
              if(startsWith($key,'billing_')){
                  if($key == 'billing_country' && $field == ''){
                      $woocommerce->add_error( '<strong>' . __( 'ERROR', 'woocommerce' ) . '</strong>: ' . __( 'Please select a country.', 'woocommerce' ) );
                  }
                  if($key == 'billing_first_name' && $field == ''){
                      $woocommerce->add_error( '<strong>' . __( 'ERROR', 'woocommerce' ) . '</strong>: ' . __( 'Please enter first name.', 'woocommerce' ) );
                  }
                  if($key == 'billing_last_name' && $field == ''){
                      $woocommerce->add_error( '<strong>' . __( 'ERROR', 'woocommerce' ) . '</strong>: ' . __( 'Please enter last name.', 'woocommerce' ) );
                  }
                  if($key == 'billing_address_1' && $field == ''){
                      $woocommerce->add_error( '<strong>' . __( 'ERROR', 'woocommerce' ) . '</strong>: ' . __( 'Please enter address.', 'woocommerce' ) );
                  }
                  if($key == 'billing_city' && $field == ''){
                      $woocommerce->add_error( '<strong>' . __( 'ERROR', 'woocommerce' ) . '</strong>: ' . __( 'Please enter city.', 'woocommerce' ) );
                  }
                  if($key == 'billing_state' && $field == ''){
                      $woocommerce->add_error( '<strong>' . __( 'ERROR', 'woocommerce' ) . '</strong>: ' . __( 'Please enter state.', 'woocommerce' ) );
                  }
                  if($key == 'billing_postcode' && $field == ''){
                      $woocommerce->add_error( '<strong>' . __( 'ERROR', 'woocommerce' ) . '</strong>: ' . __( 'Please enter a postcode.', 'woocommerce' ) );
                  }
                  
      
              }
          endforeach;
      }
      add_action('register_post','zk_validation_billing_address');

      Thanks.

    • #119040
      csheriff
      Participant

      Hi,

      Thank you for your quick response! The information that I need is for the Vendor Registration, not the Customer…
      I’m using the WooCommerce Front End Manager – I think that I need to update the wcfmmp_profile_settings in the database,but I don’t know what action hook to use. I was able to populate the Shop Name with my custom post type Business title using the following code, but I haven’t been able to populate the Address Fields from the Business address…

      add_action( 'wcfmmp_new_store_created', function( $member_id, $wcfmmp_settings ) {
      	global $WCFM, $wpdb;
      	update_user_meta( $member_id, 'store_name', $member_id );
      	update_user_meta( $member_id, 'wcfmmp_store_name', $member_id );	
      	$wcfmmp_settings['store_name'] = $member_id;
      	update_user_meta( $member_id, 'wcfmmp_profile_settings', $wcfmmp_settings );
      	$wpdb->query( "UPDATE {$wpdb->prefix}users SET <code>user_nicename</code> = '{$member_id}' WHERE ID =  $member_id" );
      }, 50, 2 );
      add_filter( 'wcfm_is_allow_store_name', '__return_false' );
    • #119104

      Hello,

      What we understand that, you have created a post type(business) and connect with the user who have already their address information.And when he want to “become a vendor” his store name(post title) and address will auto populated in registration form. Correct us if we are wrong. (assume that one user have only one business post otherwise one vendor cann’t have multiple storename).

      This is not a tweak. It will need some customization work.
      Please contact us here :https://wclovers.com/setup-guidance/ with topic link.

      Thanks.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.