Search code examples
pimcore

How to write object classificationstore in a product?


There is a class product, it has a layer ProductSpecs (classificationstore). How to write a classificationStore group I need into an object?

The documentation found nothing on this topic. Please, help me


namespace AppBundle\EventListener;

use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent; 
use Pimcore\Event\Model\AssetEvent;
use Pimcore\Event\Model\DocumentEvent; 

class ProductPropertyListener
{
    public function onPreUpdate (ElementEventInterface $e) {

        if($e instanceof AssetEvent) {
            // do something with the asset
            $foo = $e->getAsset(); 
        } else if ($e instanceof DocumentEvent) {
            // do something with the document
            $foo = $e->getDocument(); 
        } else if ($e instanceof DataObjectEvent) {
            // do something with the object
            $foo = $e->getObject();

            // getting an object here is not clear what to do with it
        }
    }
}

Solution

  • Assuming your CS field is called ProductSpecs, you could try this to set active group for object:

    $groupConfig = GroupConfig::getByName($groupName, $storeId);
    
    if ($groupConfig) {
        $groupId = $groupConfig->getId();
        $foo->getProductSpecs()->setActiveGroups([$groupId => true]);
    }