Recent Topics

Product API behaving strangely - cannot fetch data for a single product



  • I noticed that the product API on my Bagisto site was behaving very strangely when it comes to fetching single items. I can fetch a paginated list of products at api/products, but when I try to fetch data for a single product, it behaves very strangely.

    Attempting to fetch a single product with the SKU, URL key or name as a parameter returns an empty set, as does attempting to fetch all featured or new products.
    postman1.png

    Fetching a product by ID simply returns the same paginated list of all products as calling api/products.

    I have no idea what is going on here. Is something misconfigured on the server or with the database?



  • Hi @KiwiNFLFan,



  • Hi devansh,

    Thanks. I checked the documentation for product requests with query parameters and tried a request for each of the parameters. limit and page work as expected, as does category_id (returns all the products in a given category).

    However, the others (including the correct spelling for url_key) all return an empty data array, even featured=0 and new=0. I am certain there are products in my database that meet those criteria.

    Is this a database issue, or what? Any ideas?

    You can try a request here: https://ncodebkk.ga/api/products?url_key=wacky-rugged-print-t-shirt . It should return an empty array, despite the fact that the product clearly exists (as can be seen from this request):
    postmanprod.png



  • Have you done any changes because in my instance it's working fine?



  • @devansh-webkul Yes, I made one small change - I added a single line of code to packages\Webkul\API\Http\Resources\Catalog\Product.php. Right below line 65 (in_stock), I added the following line, which worked perfectly previously:

    'stock_quantity' => $product->totalQuantity(),

    This was at the request of my clients, who would like the mobile app I am building to display the current stock quantity of each product.

    Could this one line of code really have caused this? I tested it after adding the line, and it worked fine. It's only been this past week when the API has been playing up.



  • UPDATE:

    I did a local install of Bagisto on my local PC (Linux Mint), imported the dumped database from my server to my local MariaDB instance, imported the images from the server and....

    Exactly the same problem!

    I made no modifications to the Bagisto code this time, so this issue has to be something to do with the data itself and the way it is stored.

    Any ideas what is going on here?



  • I fixed the problem.

    I had two levels of categories: Women/Men, and then subcategories under each of those (in addition to Root). I had listed the product in both the appropriate sub-category (e.g. T-shirts) as well as the parent category (e.g. Women).

    After I unticked either Women or Men in the dashboard, meaning that each product has only one category, i.e. the most detailed sub-category, the product API returns the correct data.


Log in to reply