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)
#1669 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 :)
#1670 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);
#1672 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 :)
#1673 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.
#6070 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 allpublic 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)
#6246 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 allpublic 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)
#6255 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_frontend/Extend_existing_components
#6261 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/tree/master/lib/mshoplib/src/MShop/Common/Manager/Decorator
- https://github.com/aimeos/aimeos-core/tree/master/lib/mshoplib/src/MShop/Plugin/Provider/Decorator
- https://github.com/aimeos/ai-controller-frontend/tree/master/controller/frontend/src/Controller/Frontend/Common/Decorator
- https://github.com/aimeos/ai-client-html/tree/master/client/html/src/Client/Html/Common/Decorator
- https://github.com/aimeos/ai-admin-jqadm/tree/master/admin/jqadm/src/Admin/JQAdm/Common/Decorator