Define a new method for product item class

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!
User avatar
niloofar
Posts: 16
Joined: 04 Apr 2021, 09:41

Define a new method for product item class

Post by niloofar » 15 Aug 2021, 21:21

Hi

What is the best solution to show only active properties on the product's detail page? (The properties must be shown regardless of status in admin panel - no changes there)
Here's something I came up with, that does work but doesn't seem right:

In `my_ext/controller/frontend/src/Controller/Frontend/Product/Decorator/ActiveProperties.php` a new method is added to product item class

Code: Select all


namespace Aimeos\Controller\Frontend\Product\Decorator;

class ActiveProperties extends Base
{
	
	public function __construct(\Aimeos\Controller\Frontend\Iface $controller, \Aimeos\MShop\Context\Item\Iface $context)
	{
		parent::__construct($controller, $context);
		\Aimeos\MShop\Product\Item\Standard::method('getEnabledPropertyItems', function ($type = null) use ($context) {
			$list = [];
			$types = [];

			foreach ($this->propItems as $prop) {
				$types[] = $prop->getType();
			}

			$manager = \Aimeos\MShop::create($context, 'product/property/type');
			$filter = $manager->filter(true)->add('product.property.type.code', '==', $types);
			$enabledTypes = $manager->search($filter)->walk(fn (&$type) => $type = $type->getCode());


			foreach ($this->propItems as $propId => $propItem) {
				if (($type === null || in_array($propItem->getType(), (array) $type))
					&& ($propItem->isAvailable()
						&& $enabledTypes->in($propItem->getType()))
				) {
					$list[$propId] = $propItem;
				}
			}

			return map($list);
		});
	}
}

Then in `my_ext/client/html/src/Client/Html/Catalog/Detail/RtlDetail.php` I get active properties:

Code: Select all

$propItems = $productItem->getEnabledPropertyItems();
Is there a way to add decorators to product item?
Aimeos version: ~2021.07
Laravel version: ^8.40
PHP: 7.4.9

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

Re: Define a new method for product item class

Post by aimeos » 16 Aug 2021, 05:29

You can't create a decorator for an item but you can create a decorator for the product or product/property manager which intercepts the get()/find()/search() methods and removes the properties you don't want. The "Depth" decorator could be a good start:
https://github.com/aimeos/aimeos-core/b ... /Depth.php
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply