# Product Collection

It is provided with the `ProductCollection` model of grouping the products in catalogs or creating special collections with various rule sets. The collection type may be `Dynamic` or `Static`. Collection items are created only from items available for sale. In dynamic collections, a collection rule set is created using certain filters so that the products are dynamically added to the collection through this rule set. Dynamic collections remain active for the specified time `expiration_date` period. As long as they are not expired, the items in the collection are updated according to the rule set. This process is managed with tasks. In static collections, the items are added to the collection once and remain stable. While the created collections can be used to create special pages, they can also be used to create campaigns.

## `GET` Product Collection

Sample HTTP request to get all `product_collections` lists from the system.

‘content\_type’ header represents the response type.

The ‘Authorization’ header is required for authentication. You can retrieve `api_token` by logging in.

**Request**

```python

import requests

url = "https://{customer_api_url}/api/v1/product_collections/"
api_token = "API TOKEN"

headers = {
    'content-type': 'application/json',
    'Authorization': 'Token {}'.format(api_token)
}

response = requests.get(url, headers=headers)
print(response.text)
```

Path: `/api/v1/product_collections/`

**Response**

```json

{
    "count": n,
    "next": "/api/v1/product_collections/?page=2",
    "previous": null,
    "results": [
        {
            "pk": 170,
            "name": "test",
            "created_date": "2020-02-25T14:37:04.907721Z",
            "modified_date": "2021-05-07T11:35:27.751999Z",
            "collection_type": "dynamic",
            "catalog": 1,
            "is_active": true,
            "sort_option": 909,
            "uuid": "12cdaa24-31f6-4e4d-849c-93851b214b2e",
            "expiration_date": "2020-02-27T18:00:00Z",
            "value_dict": {
                "0": [
                    {
                        "value": "4",
                        "key": "active_stock__stock__gte"
                    }
                ],
                "1": [
                    {
                        "value": "True",
                        "key": "active_price__discount"
                    }
                ],
                "active_catalog_item__isnull": [
                    false
                ],
                "active_stock__isnull": [
                    false
                ],
                "active_price__isnull": [
                    false
                ]
            },
            "selected_filters": [
                {
                    "value": "4",
                    "key": "active_stock__stock__gte"
                },
                {
                    "value": "True",
                    "key": "active_price__discount"
                }
            ]
        },
        {
            "pk": 2,
            "name": "test-collection-sezgin22",
            "created_date": "2019-09-19T07:12:51.713764Z",
            "modified_date": "2020-03-31T16:11:21.606342Z",
            "collection_type": "static",
            "catalog": 1,
            "is_active": false,
            "sort_option": null,
            "uuid": "2be5902a-9dac-4720-a092-4faba04bc1a0",
            "expiration_date": null,
            "value_dict": {},
            "selected_filters": []
        },
        ...
    ]
}
```

## `POST` Creating a Static Collection

Records new objects in the `ProductCollection` table. The `ProductCollectionSerializer` class defined at `omnitron.channels.integrations.omniweb.serializers` is used to validate the data.

‘content\_type’ header represents the response type.

The ‘Authorization’ header is required for authentication. You can retrieve `api_token` by logging in.

**Request**

Path: `/api/v1/product_collections/`

```python

import requests

url = "https://{customer_api_url}/api/v1/product_collections/"
api_token = "API TOKEN"

headers = {
    'content-type': 'application/json',
    'Authorization': 'Token {}'.format(api_token)
}

data = {
    "name": "test-collection-1",
    "collection_type": "static", # can be "static" or "dynamic"
    "catalog": {catalog-id}
}


response = requests.post(url, headers=headers, data=data)
print(response.text)

```

**Response**

```json

{
    "pk":{product-collection-pk},
    "name":"test-collection-1",
    "created_date":"2021-05-11T11:50:01.348381Z",
    "modified_date":"2021-05-11T11:50:01.348414Z",
    "collection_type":"static",
    "catalog":{catalog-id},
    "is_active":true,
    "sort_option":null,
    "uuid":"df9b8773-6a35-44cd-b692-8eebadc69d60",
    "expiration_date":null,
    "value_dict":{},
    "selected_filters":[]
}

```

## `POST` Add a Product to a Static Collection

Allows users to create their own product collection items. `catalog_item` and collection field IDs must be inserted.

Path: `/api/v1/product_collection_items/`

```json

{
    "catalog_item": {catalog-item-id},
    "collection":  {product-collection-id}
}
```

**Response**

```json
{
    "pk":1,
    "catalog_item": {catalog-item-od},
    "collection":{product-collection-id}
}
```

## `DELETE` Product from Static Collection

Allows users to remove specific product collection items completely. It returns "204 No Content"

Path: `/api/v1/product_collection_items/{product-collection-item-id}`

**Response Status Code:** `204`

## `POST` Create Dynamic Collection

Allows users to create their own product collections. Dynamic refers users to add dynamic filters and automatically find and add products that fit into filters. Users can mark these collections as active or inactive, by default `is_active` is true.

Path: `/api/v1/product_collections/`

```json
{
    "collection_type": "dynamic",
     "is_active": true,
     "name": "test",
     "catalog": {catalog-id},
     "expiration_date": "2021-05-12T15:27+0300"
}
```

**Response**

```json
{
    "pk":1359,
    "name":"test",
    "created_date":"2021-05-11T12:27:09.239906Z",
    "modified_date":"2021-05-11T12:27:09.239932Z",
    "collection_type":"dynamic",
    "catalog":{catalog-id},
    "is_active":true,
    "sort_option":null,
    "uuid":"831174d8-ebb9-4cd2-aad1-00011ca056cf",
    "expiration_date":"2021-05-12T15:27:00+03:00",
    "value_dict":{},
    "selected_filters":[]
}
```

## `PATCH` Update a Dynamic Collection

Update can be performed for collections with `PATCH` Patch operations. The following request is an example of a rule set assignment for a dynamic collection.

Path: `/api/v1/product_collections/{product-collection-id}`

```json
{
   "value_dict":{
      "active_categories__category":{category-id}
   },
   "selected_filters":[
      {
         "name":"Test Category",
         "value":{catergory-id},
         "key":"active_categories__category",
         "label":"Category",
         "dumpedLabel":"Category"
      }
   ]
}
```

## `DELETE` Collection

With the deletion request, the collection is deactivated but not completely removed from the system.The sample `is_active` parameter is updated to false because the `perform_destroy` method of the `ProductCollectionViewSet` is overridden.

Path: `/api/v1/product_collections/{product-collection-pk}/`

**Response Status Code:** 204

## `GET` List All Products in a Collection

Path: `/api/v1/product_collections/{product-collection-pk}/item_list`

The endpoint where the products in the collection are listed so all the products belonging to this collection are listed. Listing is done by calling the `item_list` method of the `ProductCollectionViewSet`.

**Response**:

```json
{
    "count": n,
    "next": "/api/v1/product_collections/1357/item_list/?limit=1&page=2",
    "previous": null,
    "results": [
        {
            "pk": 856,
            "name": "WEDGIE ALPARGATA FUCHSIA",
            "base_code": "1115101",
            "sku": "1115101003",
            "product_type": {
                "value": "0",
                "label": "Simple"
            },
            "is_active": false,
            "parent": 217740,
            "attributes": {
                "erp_MalDetayAdi": "WEDGIE",
                "integration_AsilRenk": "FUCHSIA"
            },
            "attributes_kwargs": {},
            "extra_attributes": {},
            "active_catalog_item": {
                "pk": 18,
                "active_collection_item": null
            },
            "active_price": {
                "pk": 12,
                "price": "100.00",
                ...
            },
            "active_stock": {
                "pk": 229,
                "stock": 100,
                "unit_type": "qty"
            },
            "active_categories": [
                {
                    "pk": 19,
                    "category": {
                        "pk": 7,
                        ...
                    }
                }
            ],
            "productimage_set": [
                {
                    "pk": 111635,
                    "status": "active"
                    ...
                },

            ],
            "attribute_set": {
                "pk": 2,
                "name": "Shoe"
            }
        }
    ]
}
```

## `GET` List All Addable Products for a Collection

Path: `/api/v1/product_collections/{product-collection-pk}/product_list/`

The endpoint where the products that can be added in the collection are listed. Listing is done by calling the `product_list` method of the `ProductCollectionViewSet`.

**Response:**

```json
{
    "count": 758,
    "next": "api/v1/product_collections/1357/product_list/?limit=1&page=2",
    "previous": null,
    "results": [
        {
            "pk": 3,
            "name": "Guitar",
            "base_code": "1020583",
            "sku": "1020583031",
            "product_type": {
                "value": "0",
                "label": "Simple"
            },
            "is_active": true,
            "parent": null,
            "attributes": {},
            "attributes_kwargs": {},
            "extra_attributes": {},
            "active_catalog_item": {
                "pk": 3383,
                "active_collection_item": null
            },
            "active_price": {
                "pk": 39,
                "price": "120.00",
                "currency_type": "usd"
            },
            "active_stock": {
                "pk": 1,
                "stock": 83,
                "unit_type": "qty"
            },
            "active_categories": [
                {
                    "pk": 67,
                    "category": {
                        "pk": 4,
                        ...
                    }
                }
            ],
            "productimage_set": [],
            "attribute_set": {
                "pk": 151,
                "name": "New feature set"
            }
        }
    ]
}

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://apidocs.akinon.com/omnitron/catalogue/product-collection.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
