Retrieve the category of a 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!
Yvler
Posts: 33
Joined: 04 Jun 2015, 10:15

Retrieve the category of a product

Post by Yvler » 22 Jun 2015, 11:27

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 :)

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

Re: Retrieve the category of a product

Post by aimeos » 22 Jun 2015, 11:52

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);

Yvler
Posts: 33
Joined: 04 Jun 2015, 10:15

Re: Retrieve the category of a product

Post by Yvler » 23 Jun 2015, 06:39

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 :)

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

Re: Retrieve the category of a product

Post by aimeos » 23 Jun 2015, 07:13

[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.

Sergunik
Posts: 12
Joined: 16 Mar 2018, 14:14

Re: Retrieve the category of a product

Post by Sergunik » 26 Apr 2018, 11:43

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)

michal.fehér
Posts: 44
Joined: 05 Feb 2018, 09:57

Re: Retrieve the category of a product

Post by michal.fehér » 20 Jun 2018, 11:15

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)

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

Re: Retrieve the category of a product

Post by aimeos » 21 Jun 2018, 08:53

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
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

michal.fehér
Posts: 44
Joined: 05 Feb 2018, 09:57

Re: Retrieve the category of a product

Post by michal.fehér » 21 Jun 2018, 11:51

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

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

Re: Retrieve the category of a product

Post by aimeos » 25 Jun 2018, 11:46

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
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply