Jump to content

How To Get Products And Subcategories Of A Given Category As A Json Object Using Webservice Api


Lakshmanan PHP

Recommended Posts

We are developing an android app using prestashop webservice.

 

Currently we are able to obtain JSON response of categories successfully.

 

We need to get subcategories and the products from a given category id.

 

We found that this can be achieved through multiple requests

 

  1. api/categories/?output_format=JSON&filter[active]=1&display=[id,name]&filter[level_depth]=2 to get all categories
  2. /api/categories/?output_format=JSON&filter[active]=1&display=[id,name]&filter[id_parent]=47 to find subcategories in a category page
  3. /api/products/?output_format=JSON&filter[id_category_default]=47 to get all products of the given category in category page

Is this the only way or any other right way to get all subcategories and the products from a given category id?

 

Version 1.6

 

 

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 weeks later...
  • 3 months later...
  • 3 years later...
On 1/18/2016 at 12:26 PM, Lakshmanan PHP said:

Still waiting for your support

 

Hope someone will save us

 

Thanks again

Did you get any solution, now I'm in same situation need to know the solution for this, If you were find the solutions means please share

Link to comment
Share on other sites

  • 7 months later...

I have tried below but not working ,

$opt = array(
            'resource'          =>'categories',
            'filter[id]' => "[$data->id_category]",
            'output_format'  => 'JSON',
            'display'    => 'full'
        );

 

Edited by rvkvino (see edit history)
Link to comment
Share on other sites

  • 5 months later...

`/prestashop/api/categories?language=1&output_format=JSON&display=full` gives below
```json
active: "1"
associations: {products: Array(1)}
date_add: "2021-03-17 10:21:48"
date_upd: "2021-03-17 10:21:48"
description: null
id: 4
id_parent: "3"
id_shop_default: "1"
is_root_category: "0"
level_depth: "3"
link_rewrite: null
meta_description: null
meta_keywords: null
meta_title: null
name: null
nb_products_recursive: "1"
position: "0"
```
here name is null. how to fix it?

Link to comment
Share on other sites

  • 9 months later...

Hello, I hope it's not too late to answer the question, but I figured out a way to get product details of a given category by overriding the Category class. 

Below you can find the steps that I have followed

  • Create a new prestashop module, I called mine : pn_productcategoryws
  • Inside module's directory "pn_productcategoryws" create the php module file and call it : pn_productcategory_ws.php
  • Then add the costructor to the class called : "Pn_ProductCategoryWs" 
    public function __construct()
    {
        $this->name = 'ml_productcategoryws';
        $this->tab = 'front_office_features';
        $this->version = '1.0.0';
        $this->author = 'Author';
        $this->need_instance = 0;
        $this->bootstrap = true;
        parent::__construct();
        $this->displayName = $this->l('Webservice Products By Category');
        $this->description = $this->l('Display products details by category');
        $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
    }
  • Then, inside the "pn_productcategoryws" create 2 subdirectories : "/override/classes" inside the classes directory create the file Category.php
  • You should have by now this structure 
pn_productcategoryws
__pn_productcategoryws.php
__override
____classes
_______Cateogory.php
  • Inside your Category.php file you can modify the fields that you want to display by updating the protected class member $this->webserviceParameter as long as you update the function getProductsWs() that gets the data directly from the table you can, you just need to add LEFT JOINS each time you need an info from another table.
  • I call the setWebServicesParameters function in the constructor to update the protected member. If you try to modify it directly, it doesn't work as prestashop cannot generate the overriden class by using this method. Don't forget to reinitialize your module once you finished modifying your files.
  • The example below could help you get the data you want to display
<?php

class Category extends CategoryCore
{
    /**
     * Category override constructor
     */
    public function __construct($idCategory = null, $idLang = null, $idShop = null)
    {
        $this->clearCache(true);
        $this->setWebServicesParameters();
        parent::__construct($idCategory, $idLang, $idShop);
    }

    /**
     * Function to add fields to product associations when getting categories.
     * @return void
     */
    public function setWebServicesParameters()
    {
        $this->webserviceParameters = [
            'objectsNodeName' => 'categories',
            'hidden_fields' => ['nleft', 'nright', 'groupBox'],
            'fields' => [
                'id_parent' => ['xlink_resource' => 'categories'],
                'level_depth' => ['setter' => false],
                'nb_products_recursive' => ['getter' => 'getWsNbProductsRecursive', 'setter' => false],
            ],
            'associations' => [
                'products' => array(
                    'resource' => 'product',
                    'getter' => 'getProductsWs',
                    'fields' => array(
                        'id_product' => array('required' => true),
                        'name' => array('required' => true),
                        'price' => array('required' => true),
                        'quantity' => array('required' => true),
                        'date_add' => array('required' => true),
                        'id_image' => array('required' => true),
                        'on_sale' => array('required' => true),
                        'reduction' => array('required' => true),
                        'link_rewrite' => array('required' => true),
                    ),
                ),
            ],
        ];
    }

    /**
     * Overriding getProductsWs to add product fields to the result.
     * @throws PrestaShopDatabaseException
     */
    public function getProductsWs()
    {
        return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
		SELECT p.id_product, p.price, pl.name, sa.quantity, p.date_add, i.id_image, p.on_sale, sp.reduction, pl.link_rewrite
		FROM `' . _DB_PREFIX_ . 'category_product` cp 
		LEFT JOIN `' . _DB_PREFIX_ . 'product_lang` pl
		ON (cp.`id_product` = pl.`id_product`)
		LEFT JOIN `' . _DB_PREFIX_ . 'product` p
		ON (cp.`id_product` = p.`id_product`)
		LEFT JOIN `' . _DB_PREFIX_ . 'image` i
		ON (i.`id_product` = p.`id_product`)
		LEFT JOIN `' . _DB_PREFIX_ . 'stock_available` sa
		ON (sa.`id_product` = p.`id_product`)
		LEFT JOIN `' . _DB_PREFIX_ . 'specific_price` sp
		ON (sp.`id_product` = p.`id_product`)
		WHERE cp.`id_category` = ' . (int)$this->id . ' AND pl.id_lang = 1 AND i.cover = 1
        GROUP BY p.`id_product`
		ORDER BY p.`date_add` DESC
		LIMIT 12');
    }
}

 

Edited by Nouhail AL FIDI (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...