【问题标题】:Create simple product programmatically in magento2在 magento2 中以编程方式创建简单的产品
【发布时间】:2015-12-01 14:31:53
【问题描述】:

Magento-2:

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

【问题讨论】:

    标签: magento magento2


    【解决方案1】:

    我不知道您所说的外部是什么意思,但我能够使用 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()
    

    【讨论】:

    【解决方案2】:

    使用 Magneto 2 实例的 REST APIhttp://mage.host.com/index.php/rest/...

    您可以在http://mage.host.com/index.php/rest/default/schema/ 上获取OpenAPI 定义(用于 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 实例切换为“开发者”mode 以在 REST 响应中获取错误消息:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-10
      • 2012-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      相关资源
      最近更新 更多