首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以编程方式在magento2中创建简单产品

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

Stack Overflow用户
提问于 2015-12-01 14:31:53
回答 2查看 3.3K关注 0票数 6

Magento-2:

如何从magento-2外部专业地创建一个简单的产品

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-23 08:43:37

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

代码语言:javascript
复制
# 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()
票数 2
EN

Stack Overflow用户

发布于 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)

在第一步获得授权令牌

代码语言:javascript
复制
$ 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 )发布新产品数据,并得到响应的新产品数据:

代码语言:javascript
复制
$ 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):

代码语言:javascript
复制
{
  "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响应中的错误消息:

代码语言:javascript
复制
$ ./bin/magento deploy:mode:set developer
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34022858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档