0

I'm trying to set up a feed to import Commerce store entities.

I installed the Feeds module and the Commerce Feeds module.

I then created a CSV file feed with the following mappings:

  • Name: The store name
  • Email: Store email notifications
  • Currency: Default currency
  • Timezone: Store timezone

However, when I try to import the feed, I get an error:

Address (address): This value should not be null. Please check your mappings.

This makes sense because I did not set a mapping for the address.

However, in the mappings, I don't see any option for the address:

feeds store import options

Feeds has support for Address fields, so what do I need to do to set the store entity address field?

3
  • The commerce feeds, says in the description that is for products and does not mention stores. The feeds address field integration was done via the address field module. So you'll need to look at the feeds and address field module's code to try and figure out where the problem is. Also, double check that the commerce module store form is indeed utilizing the address field module.
    – No Sssweat
    May 25, 2022 at 4:44
  • 1
    An alternative you can consider is using Drupal core's migrate. drupal.stackexchange.com/questions/208908/migrate-address-field
    – No Sssweat
    May 25, 2022 at 4:45
  • @NoSssweat Thanks, it appears that the Address field added to stores is non-standard (for example, it doesn't show up in the Field UI). So I'm trying to figure out if it's possible to somehow treat it like a regular Address field. May 25, 2022 at 7:57

1 Answer 1

1

At present, there is no support for importing stores with Feeds or Commerce Feeds. I made a feature request in the Commerce Feeds issue queue.

In the meantime, I set up a feed type for importing Commerce stores that uses mappings for every field except address (because there is no support for address).

I want to be able to update my stores, so I am now using this feed to update them, but creating the store entities manually in hook_module_update() as shown below.

use \Drupal\commerce_store\Entity\Store;

MYMODULE_update_9001() {
  // The default address value to use.
  $address_default = [
    'country_code' => 'JP',
    'administrative_area' => 'Tokyo',
    'locality' => 'Place',
    'postal_code' => '111-1111',
    'address_line1' => 'Someplace',
    'address_line2' => 'Somewhere',
  ];

  // Find the ID for the store feed type that we want to use to update this store.
  $store_feed_id_query = 
 \Drupal::entityQuery('feeds_feed')
    ->condition('type', 'store_feed_type_machine_name');
  $store_feed_result = $store_feed_id_query->execute();
  $store_feed_id = 0;
  if (isset($store_feed_result)) {
    // Need to iterate through the results; there should only be one result, but if there are multiple, take the most recent.
    foreach ($store_feed_result as $only_result) {
      $store_feed_id = $only_result;
    }
  }
  if ($store_feed_id > 0) {
    $store = Store::create([
      'type' => 'store_type_machine_name',
      'name' => 'My Awesome Store',
      'mail' => '[email protected]',
      'default_currency' => 'JPY',
      'address' => $address_default,
      'timezone' => 'Asia/Tokyo',
      'is_default' => 1,
      'path' => '/store',
      'uid' => 2,
    ]);
    $store->feeds_item->target_id = $store_feed_id;
    $store->feeds_item->guid = 'my-store-id'; // Optional, only set if you are using GUID to make your feed unique.
    $store->save();
  } else {
    throw new \Exception("Failed to get store feed id: $store_feed_id");
  }
}

Note that in my mappings for the Feeds Item field, I have set GUID to be the unique value. This allows me to use my feed type to update the store values. More information on working with Feed Items programmatically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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