在 magento2 中以编程方式创建简单产品

Create simple product programmatically in magento2

Magento-2:

如何从 magento-2 外部以编程方式创建简单的产品

我不知道你所说的外部是什么意思,但我能够用 magerun2 创建一个产品。这是我得到的结果:

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

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

你可以获得OpenAPI defintion (WSDL analog for REST) on http://mage.host.com/index.php/rest/default/schema/

第一步获得authorization token

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

然后 post 使用新产品的授权令牌和最小属性的新产品数据(4 - 是空 Magento 实例中产品属性集的默认 ID)并获取新产品数据作为响应:

$ 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":..., ...

这是来自 OpenAPI 的产品对象定义(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"]
  }
}

将您的 Magento 2 实例切换到 "developer" mode 以在 REST 响应中获取错误消息:

$ ./bin/magento deploy:mode:set developer