RESTomancy: Getting products from category in Prestashop 1.5

纵饮孤独 提交于 2019-12-13 02:17:00

问题


Disclaimers:

  1. This is oriented towards Prestashop 1.5 but if the answer is: "this is fixed in version 1.x" then I'll raise a petition to update our shop.
  2. I'm also tagging it as REST because I think I explained it throughly enough so you don't need actual experience with Prestashop to understand it.

So in Prestashop we have this Web Services which lack support for use cases as simple as search by category.

1. Let's say you want to get all the products from categories 1, 3 and 17:

So what is the solution? Well, you can do something in the lines of this answer: https://stackoverflow.com/a/34061229/4209853

where you get all the products from categories 1, 3 and 17 and then you make another call for products filtering by those ID's.

'filter[id]' => '['.implode('|',$productIDsArrayIGotBefore).']',

it's ugly and 20th centurish, but well... if it gets the job done... except it doesn't. You see, this is a call for getting a resource, and someone somewhere decided:

Hey, we have all this nice HTTP action verbs, so let's use them for REST CRUD interfaces: POST for C, GET for R, PUT for U and DELETE for D. Neat.

And that's nice and all, but when combined with the lack of expressive power of Prestashop's Web Services means it's stupidly easy to run into, you guessed it? Yes, 414.

Error HTTP 414 Request URI too long

and we all know that modifying Apache so it accepts longer request URIs is not the neat scalable solution.

So we could try to split the array and make multiple calls, which is just conceptually ugh. Not just because of the performance hit of making multiple queries, but also because we would need to take into account the number of characters of all IDs concatenated to make the calculation of how many we can (safely) ask for in one call. And all that would have their own caveats, like:

2. What if we want to also filter them e.g. active=1?

Now we're in for a ride, because we can't know beforehand how many calls we will need to make.

Let's define:

  • N are the IDs I got from categories
  • n is the number of IDS I can safely ask for
  • T is the number of (filtered) products I want
  • tare the (filtered) products I already have
  • k are the (filtered) products we receive from the call

So we would end up with something like:

do{
    n0= max(T-t, n);
    k= get(products, n0);
    t +=k;
}while(count(k)!=0 and count(t)<T and !empty(N))

..which is just... bonkers.

The only elegant solution I can come up with is creating a new Prestashop Web Service that acts as a wrapper, receiving the petition through POST and forwarding it to the Prestashop service.

But, befores that... do you have a better solution using some kind of RESTomancy I may be missing?

来源:https://stackoverflow.com/questions/35600346/restomancy-getting-products-from-category-in-prestashop-1-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!