How to import default attribute values for products via API in Magento 2

- Magento 2, Our extensions

Magento 2 Development; Magento 2 tutorial

Recently, we’ve discovered a discussion on Stack Exchange about Magento 2 API import. The author asks whether it is possible to import default attribute values for products via API in Magento 2. Although you cannot establish API transfers using the default import interface, the Improved Import & Export extension lets you obtain the desired functionality within a couple of clicks. Let’s explore the issue in more detail and then proceed to the solution. And don’t forget to follow our Magento 2 Cookbook for more information on how to run your e-commerce website smoothly. 

Mission impossible: importing default attribute values for products via API

Imran Isak argues that importing products to Magento 2 becomes problematic from time to time since attributes are not set or their values are missing. For instance, the lack of product weight becomes a real headache because UPS uses this parameter to calculate the shipping price.

So, the question is how to set a default value for any attribute for products imported using API.

How to import data to Magento 2 using API

As we’ve mentioned above, you can use Improved Import & Export to import data to Magento 2 via API. The extension supports REST and SOAP APIs. You can enable this transfer method in the Import Settings section.

Other REST and SOAP API settings are available in the Import Source section. For instance, you can select your import file type (XML or JSON) and import source (SOAP or REST)

Decide which API version to use, specify the Call Function, and choose what to get with the SOAP request. If you are not sure how to use the APIs to integrate something, FireBear Studio offers its services:

We will help you with REST and SOAP APIs! Drop us a line.

How to create new attributes during Magento 2 import

It is another feature that can help you import default attribute values for products via API in Magento 2. It lets you place data necessary to create an attribute rather than an attribute name: attribute set, frontend label, scopes, etc. Use this general format:

You can separate values with a pipe symbol – “|”. Your new attribute column should ALWAYS start with “attribute” to help the Improved Import & Export extension understands that it is a new attribute.

For instance, the name of the column with the new attribute may look as follows:

For more information, read the extension’s manual: Product attributes import.

How to map attributes in Magento 2

If the import file has a custom structure that doesn’t fit the Magento 2 requirements, you can apply mapping. However, this feature is also useful for creating missing attributes and attribute values.

The Map Attributes section in an import job provides the ability to apply presets that revise unsupported attributes automatically, following the Magento 2 requirements. Choose a platform to import from (in the Import Settings section) and the mapping preset will activate background data editing, automatically performing mapping for fields that don’t satisfy system requirements:

As you can see, there is a space for default attribute values per each attribute. You can fill it in manually with the values that you want to import: 

  • select a default Magento 2 attribute in the ‘System Attribute’ 
  • specify an appropriate import attribute
  • type a default (hardcoded) value for each attribute, adding it for each imported item in the appropriate attribute column.

How to create missing attributes with mapping during Magento 2 import

You can also use the Map Attributes section to create attributes that are missing from the import file. Moreover, it is possible to apply default values for such attributes.

Click the ‘Add New Custom’ button in the attributes grid.

The extension adds a new line to the attribute grid where you can specify the Magento 2 attribute that already exists, an attribute that you want to create (its name), and its default value.

Let’s suppose you are importing products with the missing weight attribute in the imported table. Use the Map Attribute section as follows:

  1. Click the “Add New Custom” button;
  2. Select the “weight” Magento 2 attribute which is responsible for storing product weight;
  3. Type in the attribute you want to create. In your example, we use “product_weight”; however, you can name the new attribute whatever you like.
  4. In the Default Value field, specify the value of the new weight attribute, e.g., “100”.

When the import is complete, every imported product will have the “weight” attribute value set to “100”.

How to apply default attribute values during Magento 2 import

As we’ve just mentioned above, setting the Default Value for the imported attribute is another feature associated with the Improved Import & Export extension. It offers two types of behavior:

  • Apply Default Values to All Rows – the extension REPLACES attribute value in ALL ROWS. E.g., your import file contains 100 products. No attribute values are missing. If you enable Apply Default Value to All Rows, the extension replaces ALL values with the specified Default Value.
  • Apply Default Values to Empty Rows – the extension adds the Default Value to EMPTY ROWS. It’s what Imran Isak requests. E.g., your import file contains 100 products. Attribute values are missing for 50 products. The extension adds the specified Default Value only to the empty rows. 

How to map attribute values during Magento 2 import

The Map Attribute section also contains instruments that let you map attribute values when importing data to Magento 2.

The Map Attribute Values section provides the ability to specify system attribute values and the replacement for them. For instance, you can adjust a specific qty value to any new value. Let’s say, your system attribute value that should be adjusted is “100”. Specify “100 in the System Attribute Value field.

Next, set a replacement in the Import Attribute Value field. Let’s suppose, it is “56”. The extension will replace ALL values that match the value you typed in System Attribute Value. It means that all product quantities that match “100” will be replaced by “56”. Therefore, we recommend you pay close attention to the adjustments you want to apply to the imported data. If you are not sure about some attribute values, don’t use this feature.

Follow these links to learn more about the Improved Import & Export extension: 

Get Improved Import & Export Magento 2 Extension

Contact Us

Improved Import & Export Manual