Create a simple product programmatically in magento2

Magento-2:

how to create a simple product programmatically from outside magento-2

+5
source share
2 answers

I don't know what you mean by looks, but I was able to create a product with magerun2 . Here is how I guessed:

# bin/n98-magerun2.phar dev:console $productFactory = $objectManager->create("\Magento\Catalog\Model\ProductFactory"); // this needs to be set to avoid exception: // Magento\Framework\Exception\SessionException with message 'Area code not set: Area code must be set before starting a session.' // I don't know why ... $appState = $objectManager->get("Magento\Framework\App\State") $appState->setAreaCode("de") $product = $productFactory->create() $product->setName("FooBar") $product->setName("123") $product->setAttributeSetId(15) $product->save() 
+2
source

Use the REST API of your Magneto 2 instance: http://mage.host.com/index.php/rest/ ...

You can get OpenAPI defintion (analog WSDL for REST) ​​at http://mage.host.com/index.php/rest/default/schema/

Get the authorization token in the first step:

 $ curl -X POST "http://mage.host.com/index.php/rest/V1/integration/admin/token" -H "Content-Type:application/json" -d '{"username":"admin", "password":"..."}' "uhb..." 

Then publish new product data using the authorization token and the minimum attributes for the new product (4 is the default identifier for the product attribute installed in an empty Magento instance) and get new product data:

 $ curl -X POST "http://mage.host.com/index.php/rest/V1/products" -H "Content-Type:application/json" -H "Authorization:Bearer uhb..." -d '{"product": {"sku": "sku01","name": "product 001","attribute_set_id": 4}}' {"id":...,"sku":"sku01","name":"product 001","attribute_set_id":4,"status":..., ... 

This is the definition of a product object from OpenAPI (available product attributes for publication):

 { "catalog-data-product-interface": { "type": "object", "description": "", "properties": { "id": {"type": "integer", "description": "Id"}, "sku": {"type": "string", "description": "Sku"}, "name": {"type": "string", "description": "Name"}, "attributeSetId": {"type": "integer", "description": "Attribute set id"}, "price": {"type": "number", "description": "Price"}, "status": {"type": "integer", "description": "Status"}, "visibility": {"type": "integer", "description": "Visibility"}, "typeId": {"type": "string", "description": "Type id"}, "createdAt": {"type": "string", "description": "Created date"}, "updatedAt": {"type": "string", "description": "Updated date"}, "weight": {"type": "number", "description": "Weight"}, "extensionAttributes": {"$ref": "#/definitions/catalog-data-product-extension-interface"}, "productLinks": { "type": "array", "description": "Product links info", "items": {"$ref": "#/definitions/catalog-data-product-link-interface"} }, "options": { "type": "array", "description": "List of product options", "items": {"$ref": "#/definitions/catalog-data-product-custom-option-interface"} }, "mediaGalleryEntries": { "type": "array", "description": "Media gallery entries", "items": {"$ref": "#/definitions/catalog-data-product-attribute-media-gallery-entry-interface"} }, "tierPrices": { "type": "array", "description": "List of product tier prices", "items": {"$ref": "#/definitions/catalog-data-product-tier-price-interface"} }, "customAttributes": { "type": "array", "description": "Custom attributes values.", "items": {"$ref": "#/definitions/framework-attribute-interface"} } }, "required": ["sku"] } } 

Switch your Magento 2 instance to "developer" mode to get error messages in REST answers:

 $ ./bin/magento deploy:mode:set developer 
+1
source

Source: https://habr.com/ru/post/1237173/


All Articles