Page 1 of 1

Retrieve the category of a product

Posted: 22 Jun 2015, 11:27
by Yvler
Is it possible to retrieve the category of a product?
In the breadcrumb of the detail page of the shop, the category is echo'd. But I don't seem to find how this works :)

Re: Retrieve the category of a product

Posted: 22 Jun 2015, 11:52
by aimeos
Yvler wrote:Is it possible to retrieve the category of a product?
In the breadcrumb of the detail page of the shop, the category is echo'd. But I don't seem to find how this works :)
Product can be in several categories, so there's no 1:1 relationship.
You can retrieve the categories of a product by using the catalog or catalog list manager and searching for the refid and the domain 'product':

Code: Select all

$manager = \MShop_Factory::createManager($context, 'catalog');
$search = $manager->createSearch();
$expr = array(
    $search->compare('==', 'catalog.list.refid', $prodid),
    $search->compare('==', 'catalog.list.domain', 'product'),
//  $search->compare('==', 'catalog.list.type.code', 'default'), // no promotion products, etc.
);
$search->setConditions($search->combine('&&', $expr));
$categoryItems = $manager->searchItems($search);

Re: Retrieve the category of a product

Posted: 23 Jun 2015, 06:39
by Yvler
I already thought that it'd be this way. But i was hoping I could retrieve them the same way we retrieve media or the price
The problem is that we're using this on a productlist page, so we'd have to query for all the item id's :)

Re: Retrieve the category of a product

Posted: 23 Jun 2015, 07:13
by aimeos
[quote="Yvler"The problem is that we're using this on a productlist page, so we'd have to query for all the item id's :)[/quote]

The most efficient way is to add all product IDs of that list page to your query (compare() also accepts an array as third parameter), execute that query once and map the product/categories in your view. Beware to set the slice to a higher value, otherwise only 100 items will be retrieved.

Re: Retrieve the category of a product

Posted: 26 Apr 2018, 11:43
by Sergunik
aimeos wrote:Is it possible to retrieve the category of a product?
You can retrieve the categories of a product by using the catalog or catalog list manager and searching for the refid and the domain 'product':

Code: Select all

$manager = \MShop_Factory::createManager($context, 'catalog');
$search = $manager->createSearch();
$expr = array(
    $search->compare('==', 'catalog.list.refid', $prodid),
    $search->compare('==', 'catalog.list.domain', 'product'),
//  $search->compare('==', 'catalog.list.type.code', 'default'), // no promotion products, etc.
);
$search->setConditions($search->combine('&&', $expr));
$categoryItems = $manager->searchItems($search);
Names 'catalog.list.refid' and 'catalog.list.domain' – doesn't work.
Need to use: 'catalog.lists.refid' and 'catalog.lists.domain'.

For example:

Code: Select all

public function getCategoryByProductId($id) {
    $context = app('\Aimeos\Shop\Base\Context')->get();

    /** @var \Aimeos\MShop\Catalog\Manager\Standard $manager */
    $manager = \Aimeos\MShop\Catalog\Manager\Factory::createManager( $context );

    $search = $manager->createSearch();
    $expr = array(
        $search->compare('==', 'catalog.lists.refid', $id),
        $search->compare('==', 'catalog.lists.domain', 'product'),
    );
    $search->setConditions($search->combine('&&', $expr));
    $categoryItems = $manager->searchItems($search);
    
    // foreach ($categoryItems as $item) {
    //     var_dump($item->getLabel());
    // }

    // dd($categoryItems);

    return $categoryItems;
}
---
Project: aimeos/ai-laravel (2018.04.1)

Re: Retrieve the category of a product

Posted: 20 Jun 2018, 11:15
by michal.fehér
Hi Sergunik,

thank you for the method. Where do you usually put helpers method like these? Do you extend managers or create your own helper?

Thank you.
Sergunik wrote:
aimeos wrote:Is it possible to retrieve the category of a product?
You can retrieve the categories of a product by using the catalog or catalog list manager and searching for the refid and the domain 'product':

Code: Select all

$manager = \MShop_Factory::createManager($context, 'catalog');
$search = $manager->createSearch();
$expr = array(
    $search->compare('==', 'catalog.list.refid', $prodid),
    $search->compare('==', 'catalog.list.domain', 'product'),
//  $search->compare('==', 'catalog.list.type.code', 'default'), // no promotion products, etc.
);
$search->setConditions($search->combine('&&', $expr));
$categoryItems = $manager->searchItems($search);
Names 'catalog.list.refid' and 'catalog.list.domain' – doesn't work.
Need to use: 'catalog.lists.refid' and 'catalog.lists.domain'.

For example:

Code: Select all

public function getCategoryByProductId($id) {
    $context = app('\Aimeos\Shop\Base\Context')->get();

    /** @var \Aimeos\MShop\Catalog\Manager\Standard $manager */
    $manager = \Aimeos\MShop\Catalog\Manager\Factory::createManager( $context );

    $search = $manager->createSearch();
    $expr = array(
        $search->compare('==', 'catalog.lists.refid', $id),
        $search->compare('==', 'catalog.lists.domain', 'product'),
    );
    $search->setConditions($search->combine('&&', $expr));
    $categoryItems = $manager->searchItems($search);
    
    // foreach ($categoryItems as $item) {
    //     var_dump($item->getLabel());
    // }

    // dd($categoryItems);

    return $categoryItems;
}
---
Project: aimeos/ai-laravel (2018.04.1)

Re: Retrieve the category of a product

Posted: 21 Jun 2018, 08:53
by aimeos
The best place is the addData() method of a decorator for dynamically adding that funktionality to a HTML client:
https://aimeos.org/docs/Developers/Html ... components

Re: Retrieve the category of a product

Posted: 21 Jun 2018, 11:51
by michal.fehér
And what about extending functionalities in general? The other day I wanted to create method that would return supplier name by product. I want to use this method across the whole app, for example in custom lib, services, plugins etc. Extending the manager or item seems like an overkill for this, so I created ProductTrait where I put all these helpers. Not sure if this is how I should do it though. I also have CatalogTrait where I have methods that returns parent product category.
aimeos wrote:The best place is the addData() method of a decorator for dynamically adding that funktionality to a HTML client:
https://aimeos.org/docs/Developers/Html ... components

Re: Retrieve the category of a product

Posted: 25 Jun 2018, 11:46
by aimeos
You can use a trait wherever you extend a existing class. The alternative would be an own class in the ./lib/custom/src/MShop/Common/Item/Helper/ directory of your own extension that you use to build decorators. These decorators extend the functionality of existing classes if you configure the decorators to wrap around the existing classes. Decorators work for managers, controllers, plugins, client and admin classes:
- https://github.com/aimeos/aimeos-core/t ... /Decorator
- https://github.com/aimeos/aimeos-core/t ... /Decorator
- https://github.com/aimeos/ai-controller ... /Decorator
- https://github.com/aimeos/ai-client-htm ... /Decorator
- https://github.com/aimeos/ai-admin-jqad ... /Decorator