Retrieve the category of a product
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!
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
Retrieve the category of a product
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
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
Product can be in several categories, so there's no 1:1 relationship.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
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
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
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
[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.
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
Names 'catalog.list.refid' and 'catalog.list.domain' – doesn't work.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);
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)
-
- Posts: 44
- Joined: 05 Feb 2018, 09:57
Re: Retrieve the category of a product
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.
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:Names 'catalog.list.refid' and 'catalog.list.domain' – doesn't work.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);
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
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
https://aimeos.org/docs/Developers/Html ... components
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, give us a star
If you like Aimeos, give us a star
-
- Posts: 44
- Joined: 05 Feb 2018, 09:57
Re: Retrieve the category of a product
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
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
- 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, give us a star
If you like Aimeos, give us a star