how to create a simple product pro-grammatically from outside magento-2
Magento 2 has the feature to create the products from the backend but there is the time at which you want to create multiple products to test page performance, you need to create products in a certain format, you have some custom attribute or you have created your own attribute set or product type.
You can find all the values you are able to set for the product by logging the post data on product save. I’m thinking of saveAction in the ProductController that gets called from Magento administration when you’re saving a product.
Magento supplies you with it’s default Sample Data that contains some products. Thing is, they’re not of much use if you have some custom attributes added to your products, if you create your own attribute set, or a product type. For this, you’ll need to add some products yourself.
$sourceItem->setSourceCode ('default'): Using the “default” inventory source, which is added to Magento by the install script and it’s there unless it was removed from the admin $sourceItem->setStatus (SourceItemInterface::STATUS_IN_STOCK): Set Stock Status for product ( STATUS_IN_STOCK or STATUS_OUT_OF_STOCK)
I don't know what you mean by outside, but I was able to create a product with magerun2. This is how far I got:
# 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()
Use REST API of your Magneto 2 instance: http://mage.host.com/index.php/rest/...
You can get OpenAPI defintion (WSDL analog for REST) on http://mage.host.com/index.php/rest/default/schema/
Get authorization token on 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 post new product data using authorization token and minimal attributes for new product (4 - is the default ID for product attribute set in empty Magento instance) and get new products data in response:
$ 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 product object definition from OpenAPI (available product attributes to post):
{
"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 into "developer" mode to get error messages in the REST responses:
$ ./bin/magento deploy:mode:set developer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With