如何从magento-2外部以编程方式创建简单产品
答案 0 :(得分: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()
答案 1 :(得分:1)
使用您的Magneto 2实例的REST API:http://mage.host.com/index.php/rest/ ...
您可以OpenAPI
获得http://mage.host.com/index.php/rest/default/schema/定义(REST的WSDL模拟)第一步获取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..."
然后使用授权令牌和新产品的最小属性发布新产品数据(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的产品对象定义(可用产品属性发布):
{
"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