API call to add/update/delete categories for specific product.

How to configure and adapt Aimeos based shops as developer
Forum rules
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
User avatar
FrancisThong
Posts: 3
Joined: 13 Jan 2022, 14:34

API call to add/update/delete categories for specific product.

Post by FrancisThong » 16 Jan 2022, 05:23

Hi,

I'm using AIMEOS 2021.x.
I will need advice on how to make ajax call to add, update & delete the categories of a specific product.

I already have categories created and tried to do a ajax PATCH request to with the following code. But instead of replacing the existing product category, it is adding on to it. The next time I do a GET request, I can see it being added.
However when I go back to the admin page or store front to check on it, the additions are not visible.
I have no idea what is wrong. Please help. Thanks.

Code: Select all

let url = "/admin/default/jsonadm/product?id=15&_token=xxxxxx"
let data = {
  data: {
    id: '15',
    type: 'product'
    relationships: {
      catalog: {
        data: [
          {id: 8, type: 'catalog'},
          {id: 9, type: 'catalog'},
        ]
      }
    }
  }
}
$.ajax({
    method: "PATCH",
    dataType: "json",
    url: url,
    data: JSON.stringify(data)
  }).done(function (result) {
    console.log('PATCH', result.data);
  });

User avatar
aimeos
Administrator
Posts: 7866
Joined: 01 Jan 1970, 00:00

Re: API call to add/update/delete categories for specific product.

Post by aimeos » 17 Jan 2022, 08:25

In 2021.x and before, you must attach the product to the category, not the category to the product. The relation is stored in the mshop_catalog_list table.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
FrancisThong
Posts: 3
Joined: 13 Jan 2022, 14:34

Re: API call to add/update/delete categories for specific product.

Post by FrancisThong » 18 Jan 2022, 15:46

Thanks for your advice.
Finally managed to get add a product to a category after many tries with the following code.
Not sure if I'm doing it correctly but it works. Realize that I will need to use a patch request for it.
But how do I remove the product from the category?

Code: Select all

let url = /admin/default/jsonadm/catalog?id=2&_token=xxxxx
  $.ajax({
    method: "PATCH",
    dataType: "json",
    url: url,
    data: JSON.stringify({
      data: {
        type: 'catalog',
        relationships: {
          product: {
            data: [
              {
                "id": "15",
                "type": "product"
              }
            ]
          }
        }
      }
    })
  })

User avatar
aimeos
Administrator
Posts: 7866
Joined: 01 Jan 1970, 00:00

Re: API call to add/update/delete categories for specific product.

Post by aimeos » 20 Jan 2022, 09:31

Currently, there seems to be no way to remove products from categories again because relationships are only saved:
- https://github.com/aimeos/ai-admin-json ... #L255-L257
- https://github.com/aimeos/ai-admin-json ... #L337-L366

You can't use a DELETE request because it would delete the category, not only the relationship:
- https://github.com/aimeos/ai-admin-json ... p#L34-L101
- https://github.com/aimeos/ai-admin-json ... #L507-L530

A solution could be to implement an endpoint for "catalog/lists" resource which can modify and delete relationships.

The JsonAdm API has some drawbacks because it's not suited well for WRITE requests because it requires one request per resource and that can be a lot in case of products for example. We are experimenting with GraphQL last year and want to add such an API to allow full control over for adminstrating all data.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply