Magento Product API

I have magento2 for my store and I want to get all custom / simple products and their child (option) product with REST API. I can get all the products in one REST API call, but it does not provide me with a custom child product.

I need custom products as shown below:

{
    "id":1,
    "parent_id":1,
    "name":"myProduct",
    "is_active":true,
    .................
    "children_data":[

        {
            "id":1,
            "parent_id":1,
            "name":"myProduct",
            "is_active":true,
            .................
        },
        {
            "id":1,
            "parent_id":1,
            "name":"myProduct",
            "is_active":true,
            .................
        }               
    ]   
}
+4
source share
2 answers

you need to use the following REST API to get data for children

GET / V1 / custom products / {sku} / children

Answer:

[
  {
    "sku": "MH01-XS-Black",
    "name": "Chaz Kangeroo Hoodie-XS-Black",
    "attribute_set_id": 9,
    "price": 52,
    "status": 1,
    "type_id": "simple",
    "created_at": "2015-11-20 08:12:24",
    "updated_at": "2015-11-20 08:12:24",
    "weight": 1,
    "extension_attributes": [],
    "product_links": [],
    "tier_prices": [],
    "custom_attributes": [
      {
        "attribute_code": "required_options",
        "value": "0"
      },
      {
        "attribute_code": "has_options",
        "value": "0"
      },
      {
        "attribute_code": "tax_class_id",
        "value": "2"
      },
      {
        "attribute_code": "image",
        "value": "/m/h/mh01-black_main.jpg"
      },
      {
        "attribute_code": "category_ids",
        "value": [
          "2",
          "15",
          "36"
        ]
      },
      {
        "attribute_code": "size",
        "value": "167"
      },
      {
        "attribute_code": "color",
        "value": "49"
      },
      {
        "attribute_code": "small_image",
        "value": "/m/h/mh01-black_main.jpg"
      },
      {
        "attribute_code": "thumbnail",
        "value": "/m/h/mh01-black_main.jpg"
      },
      {
        "attribute_code": "url_key",
        "value": "chaz-kangeroo-hoodie-xs-black"
      },
      {
        "attribute_code": "msrp_display_actual_price_type",
        "value": "0"
      }
    ]
  },
...
]

and if you need all the configurable parameters and the following API is called

GET / V1 / custom products / {sku} / options / all

Answer:

[
  {
    "id": 3,
    "attribute_id": "90",
    "label": "Color",
    "position": 0,
    "values": [
      {
        "value_index": 49
      },
      {
        "value_index": 52
      },
      {
        "value_index": 56
      }
    ],
    "product_id": 67
  },
  {
    "id": 2,
    "attribute_id": "137",
    "label": "Size",
    "position": 0,
    "values": [
      {
        "value_index": 167
      },
      {
        "value_index": 168
      },
      {
        "value_index": 169
      },
      {
        "value_index": 170
      },
      {
        "value_index": 171
      }
    ],
    "product_id": 67
  }
]
+3
source

How to use the REST API

1.

Magento -API webapi.xml. -API.

-API, () API. Magento . (, , ) API API.

2.

API- Magento :

2.1 HTTP-

  • GET. . , GET.
  • PUT. , , .
  • POST. , , .
  • DELETE. , .

2.2 ConfigurableProduct

GET    /V1/configurable-products/:sku/children
DELETE /V1/configurable-products/:sku/children/:childSku
PUT    /V1/configurable-products/variation
POST   /V1/configurable-products/:sku/child
GET    /V1/configurable-products/:sku/options/:id
GET    /V1/configurable-products/:sku/options/all
POST   /V1/configurable-products/:sku/options
PUT    /V1/configurable-products/:sku/options/:id
DELETE /V1/configurable-products/:sku/options/:id

2.3 HTTP-

HTTP cURL, -H --header.

-API HTTP-:

.---------------.-----------------------------------.
|  HTTP header  |                Syntax             |
|---------------|-----------------------------------|
| Authorization | Authorization: Bearer <TOKEN>     |
| Accept        | Accept: application/<FORMAT>      |
| Content-Type  | Content-Type:application/<FORMAT> |
'---------------'-----------------------------------'

<TOKEN> - , Magento. . .

<FORMAT> JSON, XML.

2.4

- , . API , .

URI.

JSON XML.

2.5

  • Authorization, Accept Content-Type . , Magento.

    $token = 'token';
    $httpHeaders = new \Zend\Http\Headers();
    $httpHeaders->addHeaders([
       'Authorization' => 'Bearer ' . $token,
       'Accept' => 'application/json',
       'Content-Type' => 'application/json'
    ]);
    
  • Magento/ConfigurableProduct/etc/webapi.xml getChildren Magento\ConfigurableProduct\Api\LinkManagementInterface.

  • , URI .

    $request = new \Zend\Http\Request();
    $request->setHeaders($httpHeaders);
    $request->setUri('http://yourdomain.com/rest/V1/configurable-products/:sku/children');
    $request->setMethod(\Zend\Http\Request::METHOD_GET);    
    $params = new \Zend\Stdlib\Parameters([
       'sku' => 'sku-needed-for-example'
    ]);
    $request->setQuery($params);
    
  • HTTP Curl Client::send().

    $client = new \Zend\Http\Client();
    $options = [
       'adapter'   => 'Zend\Http\Client\Adapter\Curl',
       'curloptions' => [CURLOPT_FOLLOWLOCATION => true],
       'maxredirects' => 0,
       'timeout' => 30
    ];
    $client->setOptions($options);  
    $response = $client->send($request);
    
  • JSON. XML, Accept .

3. cURL

cURL - , HTTP- script. Linux, Mac OS X Windows.

cURL -API REST, cURL cURL.

, URI REST, 2.5. URL-:

https://<MAGENTO_HOST_OR_IP>/<MAGENTO_BASE_INSTALL_DIR>/rest/

cURL . curl.1 man page.

4.

-API HTTP . , .

HTTP

-API HTTP, :

.===========.=================.=================================================.
| HTTP code |     Meaning     |                     Description                 |
|===========|=================|=================================================|
|    200    |      Success    | M2 return HTTP 200 to the caller upon success.  |
|-----------|-----------------|-------------------------------------------------|
|           |                 | If service implementation throws either         |
|           |                 | `Magento_Service_Exception` or its derivative,  |
|           |                 | the framework returns a HTTP 400 with a error   |
|           |                 | response including the service-specific error   |
|    400    |   Bad Request   | code and message. This error code could         |
|           |                 | indicate a problem such as a missing required   |
|           |                 | parameter or the supplied data didn't pass      |
|           |                 | validation.                                     |
|-----------|-----------------|-------------------------------------------------|
|    401    |   Unauthorized  | The caller was not authorized to perform the    |
|           |                 | request. For example, the request included an   |
|           |                 | invalid token or a user with customer           |
|           |                 | permissions attempted to access an object that  |
|           |                 | requires administrator permissions.             |
|-----------|-----------------|-------------------------------------------------|
|    403    |    Forbidden    | Access is not allowed for reasons that are not  |
|           |                 | covered by error code 401.                      |
|-----------|-----------------|-------------------------------------------------|
|    404    |    Not found    | The specified REST endpoint does not exist. The |
|           |                 | caller can try again.                           |
|-----------|-----------------|-------------------------------------------------|
|    405    |   Not allowed   | A request was made of a resource using a method |
|           |                 | that is not supported by that resource. For     |
|           |                 | example, using GET on a form which requires data|
|           |                 | to be presented via POST, or using PUT on a     |
|           |                 | read-only resource.                             |
|-----------|-----------------|-------------------------------------------------|
|    406    | Not acceptable  | The requested resource is only capable of       |
|           |                 | generating content that is not acceptable       |
|           |                 | according to the Accept headers sent in the     |
|           |                 | request.                                        |
|-----------|-----------------|-------------------------------------------------|
|    500    | System Errors   | If service implementation throws any other      |
|           |                 | exception like network errors, database         |
|           |                 | communication, framework returns HTTP 500.      |
'==========='================='================================================='

POST, PUT GET -API . JSON XML. Accept: application/header , FORMAT json, xml.

DELETE true. DELETE , .

, .

.------------.---------------------------------------------------------------.
|     Part   |                          Description                          |
|------------|---------------------------------------------------------------|
|     code   | The status code representing the error.                       |
|------------|---------------------------------------------------------------|
|   message  | The message explaining the error.                             |
|------------|---------------------------------------------------------------|
| parameters | Optional. An array of attributes used to generate a different |
|            | and/or localized error message for the client.                |
'------------'---------------------------------------------------------------'
0

Source: https://habr.com/ru/post/1649002/


All Articles