How to import default attribute values for products via API in Magento 2
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.
Table of contents
- 1 Mission impossible: importing default attribute values for products via API
- 2 How to import data to Magento 2 using API
- 3 How to create new attributes during Magento 2 import
- 4 How to map attributes in Magento 2
- 5 How to create missing attributes with mapping during Magento 2 import
- 6 How to apply default attribute values during Magento 2 import
Mission impossible: importing default attribute values for products via API
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:
1 |
attribute|attribute_property_name:attribute_property_value|… |
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:
1 |
attribute|attribute_code:size|frontend_input:select|is_required:0|is_global:1|default_value_text:XS text|is_unique:0|is_used_in_grid:1|is_filterable_in_grid:1|is_searchable:1|search_weight:3|is_visible_in_advanced_search:0|is_comparable:0|is_filterable:1|is_filterable_in_search:1|position:2|is_used_for_promo_rules:0|is_html_allowed_on_front:1|is_visible_on_front:1|used_in_product_listing:0|used_for_sort_by:1|frontend_label_0:Size|frontend_label_1:Size|attribute_set:Default |
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:
- Click the “Add New Custom” button;
- Select the “weight” Magento 2 attribute which is responsible for storing product weight;
- 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.
- 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
Improved Import & Export Manual