I need to import customer data from CSV files with billing and shipping data using Drupal 8.8 and Commerce 2. I want to create the customer with its address data programmatically, but i do not know how to create billing and shipping.

I create the user as:

 $lang = \Drupal::languageManager()->getCurrentLanguage()->getId();
 $user = \Drupal\user\Entity\User::create();
 $user->set("init", $email);
 $user->set("langcode", $lang);
 $user->set("preferred_langcode", $lang);
 $user->set("preferred_admin_langcode", $lang);
 // Save user account.

How do i create its billing and shipping data?

  • 1
    take a look at this drupal.org/project/commerce/issues/2811529 – berramou Jan 27 at 15:48
  • Thanks @berramou , but it does not cover profiles creation with shipping address and billing address. I would have preferred not to go to Commerce source code to find billing and shipping field names. – Cris Jan 27 at 22:02
  • 1
    I'm not familiar with Commerce, but from berramou's link it seems its profiles are drupal Entities, so you should be able to use Migrate API with the Migrate Source CSV plugin to populate them with destination: plugin: 'entity:profile' in your migration yml. – stevekeiretsu Jan 29 at 17:20
  • Yes, Migrate is the route to go over custom scripting. – Kevin Jan 29 at 17:34

you got 2 options 1 migrate them, such as (https://www.drupal.org/project/migrate_source_csv) or make an admin form to submit the csv file and get the content... but really you need to be create profiles for this billing info...

// Save user account.
// Add billing info. 
$the_user_created_id = $user->id();
$profileStorage = \Drupal::entityTypeManager()->getStorage('profile');
$profile_data = [
 'type' => 'customer',
 'uid' => $the_user_created_id,
 'status'=> 1,
 'address' => [
   "langcode" => "",
   "country_code" => "AU",
   "administrative_area" => "NSW",
   "locality" => "SOME PLACE",
   "dependent_locality" => null,
   "postal_code" => "2000",
   "sorting_code" => null,
   "address_line1" => "Some Street",
   "address_line2" => "",
   "organization" => "47",
   "given_name" => "firstname",
   "additional_name" => null,
   "family_name" => "lastname",

$new_profile = $profileStorage->create($profile_data);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.