This article shows the complete, technical and functional way to import Categories, Attributes and Products lists in Magento 2 CE ecommerce, using REST API interface and C# .NET code.

At bottom you will find some Magento Backup and Recovery tips.

You will find how-to step-by-step guide with examples codes to:

  1.  Upload The Categories;
  2.  Upload The Product Custom Attributes;
  3.  Upload Configurable Products;
  4.  Upload Simple Products;
  5.  Associate Configurable Product Options to Configurable Products and to Simple Products; 
  6.  Create Product Configurations;
  7.  Link Simple Products to Configurable Product;

The examples in this guide has been tested for the following versions:

  1. Magento CE 2.2.6
  2. Mysql 5.7.21
  3. Php 7.1.14

For more information on the Magento 2 CE installation prerequisites, you can find the official documentation here: https://devdocs.magento.com/guides/v2.0/install-gde/prereq/prereq-overview.html

Magento 2 exposes three main implementable interfaces for uploading and managing data:

  1. Rest API Interface
  2. SOAP API Interface
  3. Object Manager + Object Model Interface

By using these methods, you can easily add products lists from a generic data source to the magento ecommerce and you can maintain synchronization of the same.

PHP – ObjectManager:

It is possible to develop custom PHP code, for Plugins, Modules or generic scripts, and integrate it with Magento. The ObjectManager service allows you to implement the Object Model Interface in your code and to instantiate your objects at bootstrapping of the application. This is done by exploiting the peculiarities of Dependency injection .

You can choose not to use dependency injection and Magento bootstrapping simply by creating “Newable / non-injectable” objects, modifying Bootstrap, instantiating the Object Manager and invoking resources (Framework classes) whenever they are needed. With the modalities described above, using the Object Model , Magento data can be loaded and manipulated.

SOAP APIs:

Magento endpoints are protected and accessible only with authentication. To generate a proxy it is necessary to download the WSDL (eg.: http://<site url>/magento2/soap/default?wsdl=1&services=storeStoreRepositoryV1 ) of the requested service, by using a software that allows SOAP requests with authentication (es Postman). After you saved the WSDL with dedicated utilities it is possible to generate C# and VB .NET client-side proxy classes (In Visual Studio the most common tool is Svcutil.exe).

REST APIs:

Each Magento installation has a complete set of REST endpoints to interact with CMS data and structures.

The most convenient way to take advantage of Magento REST API is to use HTTP calls. In addition, you must generate a local data model and instantiate a REST Client.
An example of code written in C #:

JsonDeserializer jsonDeSerializer = new JsonDeserializer(); 
RestClient client = new RestClient("http://<your site url>/magento2/"); 
bool contentTypeHeaderWithUnderscore = false; 
string barerToken = "vztdr8zbicw4poozqfg5eo0vh3t29ikt";  
client.AddDefaultHeader(contentTypeHeaderWithUnderscore ? "Content_Type" : "Content-Type", "application/json"); 
client.ClearHandlers(); client.AddHandler("application/json", _jsonDeSerializer); 
var request = new RestRequest("rest/V1/products/000167", RestSharp.Method.GET); 
request.RequestFormat = DataFormat.Json; 
request.AddHeader("Authorization", "Bearer " + barerToken); 
request.AddHeader("Accept", "application/json");

RestClient is a Restsharp package that can be downloaded from the NuGet Package repository.

There is a endpoint for managing each Magento 2 CE entity. For each entity there is an interface to create a “data model” through which to build a request in JSON format.

You can find the complete list of available services here:http://devdocs.magento.com/guides/v2.0/rest/list.html

To transform a data model into a Json string (serialization), you can use the .NET Newtonsoft.Json Library, as shown in the following code:

Product pd = new product(); 
pd.name = “Jacket”; 
pd.sku = “GIUB01” ……. 
string json = JsonConvert.SerializeObject(pd, Formatting.Indented); 
request.AddParameter("application/json", json, ParameterType.RequestBody); 
response = Client.Execute(request);

Sequence of steps to be follow for a standard and complete data loading within Magento are:

  1. Creation of Categories;
  2. Creation of AttributeSet;
  3. Creation of Custom Product Attributes, AttributeSet Assignment, AttributeGroup assignment;
  4. Option assignment to individual Custom Product Attribute;
  5. Creating Configurable Products;
    1. Contextual assignment to AttributeSet;
  6. Creation of Simple Type Products;
    1. Contextual assignment to AttributeSet + Product Attributes Custom Option;
  7. Option of Attribute assignment to the Configurable Product for use in the products combinations;
  8. Option of Attribute assignamento to simple products for use in the products associations ;
  9. Simple Products association to the relative Configurable Product (products linking);

Point 1:

The Magento installation creates a root category, not visible either in the back-end or in the front-end, called ” Root Catalog “.

Magento 2 Category Table with the Root Catalog

It must not be deleted. Under the root category you have to insert all other categories according to the desired hierarchy. Below is a simple JSON to send to the end-point to insert a category: “/rest/V1/categories”:

 {"category":{"id":0,"parent_id":2,"name":"Clothing","is_active":true,"position":0,"level":0,"children":null,"created_at":null,"updated_at":null,"path":null,"available_sort_by":null,"include_in_menu":true,"custom_attributes":[{"attribute_code":"url_key","value":"clothing"}]}}

Tree of Magento Categories 2

Point 2:

In Magento the Product Attributes are organized and related to an AttributeSet . It is possible to create multiple Custom Product Attributes and assign them to AttributeSet. A Product Attribute can belong only to a attributeset. Here is a simple JSON for the end-point “/rest/V1/products/attribute-sets” to create a Attribute Set:

 {"skeletonId":4,"attributeSet":{"attribute_set_id":null,"attribute_set_name":"Etos_Magento_Attr","sort_order":2,"entity_type_id":0,"extension_attributes":null},"entityTypeCode":"catalog_product"}

Parameter named “Skeletonid” indicates the creation of the attribute set on the default attribute set that in Magento always has id = 4.

Magento Attribute Set and Attribute Group 2

Point 3:

Magento comes with some standard Attributes but it is possible to add Custom Product Attributes to qualify the products. We have to do this if Product Combinations that will be created for the products are based on attributes not present in Magento. Below is a simple JSON to send to the end-point “/rest/V1/products/attributes” to create a Custom Product Attribute:

 {"attribute":{"is_wysiwyg_enabled":false,"is_html_allowed_on_front":true,"used_for_sort_by":false,"is_filterable":false,"is_filterable_in_search":false,"is_used_in_grid":true,"is_visible_in_grid":true,"is_filterable_in_grid":false,"position":0,"apply_to":["simple","configurable","virtual"],"is_searchable":null,"is_visible_in_advanced_search":null,"is_comparable":"true","is_used_for_promo_rules":null,"is_visible_on_front":"true","used_in_product_listing":"true","is_visible":true,"scope":"Store View","attribute_id":0,"attribute_code":"a1","frontend_input":"select","entity_type_id":null,"is_required":false,"options":null,"is_user_defined":true,"default_frontend_label":"A1","frontend_labels":[{"storeId":0,"label":"A1"}],"backend_type":null,"source_model":null,"is_unique":null,"validation_rules":null}}

To use Attributes Options in products combination, Attribute must have the following features:

The “scope” must be set to “global”;
The attribute must be “visible”;
The attribute must be of “user defined” type;
The attribute must use a number of options (such as the “dropdown” and “multiselect” attributes)

The example json to assign the Custom Product Attribute to the Attributeset and to the Attribute group is shown below. The endpoint is: “/rest/V1/products/attribute-sets/attributes”:

 {"attributeSetId":16,"attributeGroupId":101,"attributeCode":"a1","sortOrder":0}

Attribute and Attribute Option in Magento 2

Point 4:

For each Attribute created, we must assign the relative Options Value lists by using the endpoint “/rest/V1/products/attributes/<attribute-code>/options” with a json as the following:

 {"option":{"label":"2XS","value":"T2XS_TA","storeLabels":{"storeId":1,"label":"2XS"}},"sortOrder":1}

Point 5:

Magento 2 CE allows to create at least 6 types of products : Simple, Configurable, Grouped, Virtual, Bundle and Downlodable. Usually Magento 2 shop should have Configurable Products (Parent) with several Simple Products (Child) associated in a number equal to all the possible combinations for the Attributes Options Values associated.

Configurable Product will be the only entity visible in the products Catalog of the shop. So the Simple Products will be displayed only in the detail pages every time customers will choose available options.

Configurable Product in Magento 2 shop lists

To create a product of type Configurable, you have to use the “/rest/V1/products” endpoint with the “POST” method. Below an example of json for the complete insertion of a Configurable Product with price, stock (stock), images, descriptions, attributes:

 {"sku":"000043","product":{"id":null,"sku":"000043","name":"Apepazza Women Sold Shoes Yle01/texture-box Derby Mascherina Con Perline","attribute_set_id":16,"price":139,"status":1,"visibility":4,"type_id":"configurable","created_at":null,"updated_at":null,"extension_attributes":{"stockItem":{"itemId":null,"productId":0,"stockId":1,"qty":0,"isInStock":true,"isQtyDecimal":true,"showDefaultNotificationMessage":false, "useConfigMinQty":true,"minQty":1,"useConfigMinSaleQty":1,"minSaleQty":1,"useConfigMaxSaleQty":false,"maxSaleQty":0,"useConfigBackorders":true, "backorders":0,"useConfigNotifyStockQty":true,"notifyStockQty":1,"useConfigQtyIncrements":true,"qtyIncrements":1,"useConfigEnableQtyInc":true,"enableQtyIncrements":true,"useConfigManageStock":true,"manageStock":true, "lowStockDate":null,"isDecimalDivided":true,"stockStatusChangedAuto":0,"extensionAttributes":null},"category_links":[{"position":0,"category_id":"3","extension_attributes":null},{"position":0,"category_id":"14","extension_attributes":null},{"position":0,"category_id":"60","extension_attributes":null},{"position":0,"category_id":"84","extension_attributes":null}]},"product_links":null,"options":null,"media_gallery_entries":[{"id":null,"mediaType":"image","label":"Foto-0-APEPAZZACALZATURESALDIDONNAYLE01TEXTUREBOXDERBYMASCHERINACONPERLINE-000043","position":0,"disabled":false,"types":["image","small_image","thumbnail"],"file":null,"content":{"Base64EncodedData":"/9j/4RJQRXhpZgAATU0AKgAAAAgAEgEAAAMAAAABEKAAAA .... <stringa.dati-base64>","Type":"image/jpeg","Name":"DSE_3460jpg"},"extensionAttributes":null}],"tier_prices":null,"custom_attributes":[{"attribute_code":"url_key","value":"apepazza-calzature-saldi-donna-yle01-texture-box-derby-mascherina-con-perline"},{"attribute_code":"description","value":"YLE01/TEXTURE-BOX<br>\t\t<p><span style=\"background-color:#FFFFFF;font-family:Calibri;font-size:11pt;\">Scarpa bassa in pelle spazzolata con inserti elasticizzati su linguetta.<\/span><\/p>\r\n\t\t<p><span style=\"background-color:#FFFFFF;font-family:Calibri;font-size:11pt;\"> Ricamo di perline frontale .<\/span><\/p>\r\n\t\t<p><span style=\"background-color:#FFFFFF;font-family:Calibri;font-size:11pt;\"> Suola in gomma light e tacco 30 .<\/span><\/p>\r\n\r\n"},{"attribute_code":"barcode","value":""},{"attribute_code":"tipoprodotto","value":"CALZATURE"},{"attribute_code":"reparto","value":"SALDI DONNA"},{"attribute_code":"brand","value":"APEPAZZA"}]}}

Point 6:

The next step in creating a Simple or Child product, which is the single variant (combination) of the Configurable product

 {"sku":"000257.010-41","product":{"id":null,"sku":"000257.010-41","name":"Atlantic Stars Calzature Saldi Uomo Antares Bpb-63n Camoscio/nylon F.do Sport","attribute_set_id":16,"price":185,"status":1,"visibility":1,"type_id":"simple","created_at":null,"updated_at":null,"extension_attributes":{"stockItem":{"itemId":null,"productId":0,"stockId":1,"qty":1,"isInStock":true,"isQtyDecimal":true,"showDefaultNotificationMessage":false, "useConfigMinQty":true,"minQty":1,"useConfigMinSaleQty":1,"minSaleQty":1,"useConfigMaxSaleQty":false,"maxSaleQty":0,"useConfigBackorders":true, "backorders":0,"useConfigNotifyStockQty":true,"notifyStockQty":1,"useConfigQtyIncrements":true,"qtyIncrements":1,"useConfigEnableQtyInc":true,"enableQtyIncrements":true,"useConfigManageStock":true, "manageStock":true,"lowStockDate":null,"isDecimalDivided":true,"stockStatusChangedAuto":0,"extensionAttributes":null},"category_links":[{"position":0,"category_id":"3","extension_attributes":null},{"position":0,"category_id":"25","extension_attributes":null},{"position":0,"category_id":"79","extension_attributes":null},{"position":0,"category_id":"85","extension_attributes":null}]},"product_links":null,"options":null,"media_gallery_entries":[{"id":null,"mediaType":"image","label":"Foto-0-ATLANTICSTARSCALZATURESALDIUOMOANTARESBPB63NCAMOSCIONYLONFDOSPORT-000257.010-41","position":0,"disabled":false,"types":["image","small_image","thumbnail"],"file":null,"content":{"Base64EncodedData":"/9j/4RDKRXhpZgAATU0AKgAAAAgAEgEAAA ... ","Type":"image/jpeg","Name":"DSE_4063jpg"},"extensionAttributes":null},{"id":null,"mediaType":"image","label":"Foto-1-ATLANTICSTARSCALZATURESALDIUOMOANTARESBPB63NCAMOSCIONYLONFDOSPORT-000257.010-41","position":1,"disabled":false,"types":null,"file":null,"content":{"Base64EncodedData":"/9j/4RIRRXhpZgAATU0AKgAAAAgAEgEAAA ... ","Type":"image/jpeg","Name":"DSE_4064jpg"},"extensionAttributes":null},{"id":null,"mediaType":"image","label":"Foto-2-ATLANTICSTARSCALZATURESALDIUOMOANTARESBPB63NCAMOSCIONYLONFDOSPORT-000257.010-41","position":2,"disabled":false,"types":null,"file":null,"content":{"Base64EncodedData":"/9j/4RFxRXhpZgAATU0AKgAAAAgAEgEAAA ...","Type":"image/jpeg","Name":"DSE_4065jpg"},"extensionAttributes":null}],"tier_prices":null,"custom_attributes":[{"attribute_code":"calzuomo3848senza12","value":"147"},{"attribute_code":"colore","value":"326"},{"attribute_code":"url_key","value":"atlantic-stars-calzature-saldi-uomo-antares-bpb-63n-camoscio-nylon-f-do-sport"},{"attribute_code":"barcode","value":"20094581"},{"attribute_code":"tipoprodotto","value":"CALZATURE"},{"attribute_code":"reparto","value":"SALDI UOMO"},{"attribute_code":"brand","value":"ATLANTIC STARS"}]}}

For the Simple Product, the visibility is set to “1” (= not visible individually) because only Configurable Product must be visible in the Catalog and in the Searches, while the simple product will be shown on the page detail by choosing relative combination.

Configurable Product and Simple Product in Magento 2 backend

Simple Product Details with Choice Options and image swapping

Point 7:

Now we have to assign the Configurable Product the attributes and the relative options to be used for the combinations. Assigning these options to the Configurable product corresponds to the value of the “Configurable Products Options” It is possible by usign “/rest/V1/configurable-products/{sku}/options” endpoint, and the following JSON, derived from a GET method (“rest/V1/products/:sku”) for a single product:

 {"configurable_product_options":[{"id":51642,"attribute_id":"197","label":"Colore","position":0,"values":[{"value_index":322}],"product_id":6728},{"id":51643,"attribute_id":"210","label":"Ta","position":0,"values":[{"value_index":8},{"value_index":5},{"value_index":9},{"value_index":6},{"value_index":7}],"product_id":6728}]}

If these options are not assigned, when we try to link Simple Product to the Configurable the Magento system, it will raise the “Configurable” exception

Option for Color attribute

 {"option":{"attributeId":"174","label":"COLORE","position":0,"isUseDefault":true,"values":[{"valueIndex":346,"extensionAttributes":{}}],"extensionAttributes":{},"productId":0}}

Options for attribute “CALZ. WOMAN 34-42 WITH ½ “:

 {"option":{"attributeId":"160","label":"CALZ. DONNA 34-42 CON ½","position":0,"isUseDefault":true,"values":[{"valueIndex":32,"extensionAttributes":{}},{"valueIndex":33,"extensionAttributes":{}},{"valueIndex":34,"extensionAttributes":{}},{"valueIndex":35,"extensionAttributes":{}},{"valueIndex":36,"extensionAttributes":{}},{"valueIndex":37,"extensionAttributes":{}}],"extensionAttributes":{},"productId":0}}

All options are assigned to the Configurable. All the options must be included in a single json, otherwise the error “Something went wrong while saving the option” is raised, which corresponds to the violation of the primary key constraint.

Selecting the configurable options to generate product configurations

Point 8:

We also have to assign the Configurable Attributes Options to the Simple product, if it is a Combination of one Configurable Product, by using “/rest/V1/configurable-products/{sku}/options “. For  Simple Product we have to assign only one option value for each Attribute:

Option for Color Attribute:

 {"option":{"attributeId":"174","label":"COLORE","position":0,"isUseDefault":true,"values":[{"valueIndex":326,"extensionAttributes":{}}],"extensionAttributes":{},"productId":0}}

Color attribute selected for Simple Product

Option for Size attribute:

 {"option":{"attributeId":"163","label":"CALZ.UOMO 38-48 SENZA 1/2","position":0,"isUseDefault":true,"values":[{"valueIndex":150,"extensionAttributes":{}}],"extensionAttributes":{},"productId":0}}

Point 9:

The last step is to associate the simple product with the configurable. Via the endpoint “/ rest / V1 / configurable-products / {sku} / child” and the following example json:

 {   "childSku": "000042.032-36" }

Configurations List / Simple Products Associated with the Configurable product

Backup and Security of Magento 2 e-commerce

Before making a full upload of products or other data, it is important to make a complete backup of Magento MySQL database and Magento files located on the FTP space.
First of all, it is necessary to configure MySQL to allow remote connections (this must be allowed by your hosting provider), then you have to locate the MySQL configuration file where at specific directive you have to indicate the IP address from which you need to connect:

 bind-address = 192.168.0.97

Then access MySQL server by using root credentials and run the following script:

 GRANT ALL ON <mysql db name>.* TO <utente accesso remoto>@<indirizzo ip computer remoto> IDENTIFIED BY '<password utente remoto>';

For example:

 GRANT ALL ON magento2.* TO root@192.168.0.200 IDENTIFIED BY 'mypswd';

To perform the Magento e-commerce backup procedures, we will use Iperius Backup.

In the image below you can see the simple configuration to automatically download the site via FTP and to perform a MySQL backup (dump).

Backup of MySql with Iperius Backup

For more information, see the appropriate tutorial: MySQL backup with Iperius

It is essential to perform a full backup of all Magento 2 CE installation files. To do this you have to configure a new backup operation on Iperius where the source items are the Magento files located on the web space, reachable via FTP:

Backup Ftp Web Space with Iperius Backup

For more information, see the appropriate tutorial: Automatic download of websites with Iperius

Click here to download the latest version of Iperius Backup

Management software for shops and e-commerce

Importing product lists into an e-commerce is an operation of great importance if you have a store management software that manages a lot of data. In fact, one of the best ways for a shop to expand its offering and its customers is to sell products through a Magento e-commerce system. Shops and companies often encounter difficulties in integrating their management software with an effective e-commerce strategy. To simplify this process, there are complete solutions that allow you to automatically import product lists into a Magento e-commerce system such as the management software for stores Etos : https://www.etosweb.com/negozio-online/

 

(Italian, French, Spanish, Portuguese (Brazil))



Magento 2 – how to add products in Magento using REST API and C #
Iperius Team
*****************************************

PLEASE NOTE: if you need technical support or have any sales or technical question, don't use comments. Instead open a TICKET here: https://www.iperiusbackup.com/contact.aspx

*****************************************

Comments

  1. Jeroen

    Is there a place where I can find the fields of the JSON objects to use in request or to expect in response?
    Or do you already have C# classes?

    Thanks in advance

  2. Rich

    Hello

    How does point 8 different to 7? They’re both being posted to configurable-products/sku/options and seem to contain the same data?

    Have I misunderstood?

    Thanks

    1. Iperius Team

      Hi,

      The difference is substantial. It is necessary to assign all “attribute option” we will use to generate the combinations to the “configurable product”. Then we will assign each one option to the specific “simple product” for all “simple products” to be related to the “configurable product”. The two different operation have the same endpoint but in the first one we use the sku of the “configurable product” and in the second one the sku of the “simple product”. this two operation are necessary to relate the “simple product” at the “configurable product”. So in the back office we will view the simple product as one of the “configurable product” variation. If you do not applies this method when you will call the last endpoint “/rest/V1/configurable-products/{sku}/child” magento will return you the error: “Parent And Child does not have the same attributes options related”.

      For more technical support, please open a ticket on https://support.iperius.net

      1. Rich

        Thank you for the reply, it is much appreciated.

        For the parent configurable product, you are POSTing to /rest/V1/configurable-products/parentSKU/options ?, eg. /rest/V1/configurable-products/000042/options

        But then for each child, you cannot post to /rest/V1/configurable-products/childSKU/options because the child SKU is a simple product, not a configurable product.
        That would be /rest/V1/configurable-products/000042.032-36/options ?
        Or is that a different URL that should be used for the child?

        1. Iperius Team

          Hi,

          Exactly. you wrote correctly. The url is the same, the only one thing to change is the sku.

Leave a Reply to Iperius Team Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*****************************************

PLEASE NOTE: if you need technical support or have any sales or technical question, don't use comments. Instead open a TICKET here: https://www.iperiusbackup.com/contact.aspx

*****************************************