Magento-2:
如何从magento-2外部专业地创建一个简单的产品
发布于 2015-12-23 08:43:37
我不知道你所说的“外部”是什么意思,但我能够用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()发布于 2016-03-18 14:33:26
使用REST API的Magneto 2实例:http://mage.host.com/index.php/rest/.
您可以在OpenAPI上获得http://mage.host.com/index.php/rest/default/schema/定义(WSDL模拟为REST)
在第一步获得授权令牌:
$ 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的产品对象定义(可用的产品属性到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“模式,以获取REST响应中的错误消息:
$ ./bin/magento deploy:mode:set developerhttps://stackoverflow.com/questions/34022858
复制相似问题