Score:0

How can Commerce Stores be imported with Feeds?

cn flag

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?

No Sssweat avatar
ua flag
The commerce feeds, says in the description that is [for products](https://git.drupalcode.org/project/commerce_feeds/-/blob/8.x-1.x/commerce_feeds.info.yml#L3) and does not mention stores. The feeds address field integration was done via the [address field module](https://www.drupal.org/project/address/issues/2882589). 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 avatar
ua flag
An alternative you can consider is using Drupal core's migrate. https://drupal.stackexchange.com/questions/208908/migrate-address-field
cn flag
@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.
Score:0
cn flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.