Vinoth kumar
Vinoth kumar

Reputation: 475

Create simple product programmatically in magento2

Magento-2:

how to create a simple product pro-grammatically from outside magento-2

Upvotes: 6

Views: 3795

Answers (2)

Alex Gusev
Alex Gusev

Reputation: 1854

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

Upvotes: 1

bka
bka

Reputation: 156

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()

Upvotes: 2

Related Questions