如何通过REST API

时间:2016-08-29 13:04:39

标签: magento magento2

我尝试为产品创建设置可下载链接,并尝试在产品上更新产品但是没有用。

我试过这个结束点吧

POST "/v1/products/$sku/downloadable-links"
像这样

$downloadable_product_links = [
                [
                'id' => 1
                , 'title' => json_encode(["resolution" => "1000x2000 px", "dpi" => "72", "size" => "16 KB"])
                , 'sort_order' => 1
                , 'is_shareable' => 1
                , 'price' => 15
                , 'link_type' => 'url'
                , 'link' => 'https://my-domain/image/DSC_0801.JPG'
                , 'link_url' => 'https://my-domain/image/DSC_0801.JPG'
            ],
                [
                    'id' => 2
                    , 'title' => json_encode(["resolution" => "150x200 px", "dpi" => "72", "size" => "18 KB"])
                    , 'sort_order' => 2
                    , 'is_shareable' => 1
                    , 'price' => 14
                    , 'link_type' => 'url'
                    , 'link' => 'https://my-domain/image/DSC_0801.JPG'
                    , 'link_url' => 'https://my-domain/image/DSC_0801.JPG'
            ]
        ];
        $result = MagentoConn::instance()->reqPOST("/products/$sku/downloadable-links", $downloadable_product_links );

它给了我这个错误消息

object(stdClass)[163]
  public 'message' => string '%fieldName is a required field.' (length=31)
  public 'parameters' => 
    object(stdClass)[164]
      public 'fieldName' => string 'link'

这个链接字段应该是什么以及它应该包含什么?Iried将它作为上述数组的键但是给出了异常"no setter"

1 个答案:

答案 0 :(得分:0)

我认为如果有人能发现它有用

$item = [
            "product" => [
                'sku' => $sku,
                'name' => $title,
                'visibility' => self::VISIBILITY_BOTH, 
                "type_id" => self::TYP_DOWNLOADABLE,
                'price' => 0 ,
                'status' => self::STATUS_ENABLED   ,
                'attribute_set_id' => 9,
                'weight' => 0,
                "custom_attributes" => [
                    ["attribute_code" => "url_key", "value" => $url_key],
                    ["attribute_code" => "links_purchased_separately", "value" => '1'],
                    ["attribute_code" => "category_ids", "value" => $categoryIds ],
                    ["attribute_code" => "meta_keyword", "value" => "my_keywords" ],
                    ["attribute_code" => "meta_description", "value" => "my_description"  ],
                    ["attribute_code" => "tax_class_id", "value" => "0"  ],  // "0" => NO TAX ||  "2"  => taxable
                ],
                "extension_attributes" => [
                    "stock_item" => [
                        "manage_stock" => 1,
                        "is_in_stock" => 1,
                        "qty" => "10"
                    ],
                    "downloadable_product_links" => [
                        [
                            'title' => "AAAA"
                            , 'sort_order' => 1
                            , 'is_shareable' => 1
                            , 'number_of_downloads' => 999
                            , 'price' => 15
                            , 'link_type' => 'url'
                            , 'link_url' => $link_url1
                        ],[
                            'title' => "BBB"
                            , 'sort_order' => 2
                            , 'is_shareable' => 0
                            , 'number_of_downloads' => 0
                            , 'price' => 18
                            , 'link_type' => 'url'
                            , 'link_url' => $link_url2
                        ],
                    ]
                ],
            ]
        ];
        $result = MagentoConn::POST("/products", $item);