Akeneo: create a new variant group with attributes

I want to create a new variation group in Akeneo with some fixed attributes to "mimic" the behavior of a product family. Unfortunately, I don't know how to get ProductTemplate

with some attributes with empty values.

When I tried my code below, I get an error after opening the created option group in Akeneo GUI:

Error: Option "attributes" must only contains instances of "Pim\Bundle\CatalogBundle\Entity\Attribute", got "Pim\Bundle\EnrichBundle\Form\Type\AvailableAttributesType"

      

My code looks like this:

    $groupType = $this->groupManager
        ->getGroupTypeRepository()
        ->findOneBy(['code' => 'VARIANT']);
    $group = $this->groupFactory->createGroup($groupType);
    $group->setCode('MY_VARIANT_GROUP');

    $attributes = array($this->attributeRepository->findOneByIdentifier('AXIS_ATTRIBUTE'));
    $group->setAxisAttributes($attributes);

    // ??? How can I create a new product value?
    $productValue1 = new ProductValue();
    $productValue1->setId('PREDEFINED_ATTRIBUTE1');
    $productValue1->setAttribute($this->attributeRepository->findOneByIdentifier('PREDEFINED_ATTRIBUTE1'));

    $productTemplate = new ProductTemplate();
    $productTemplate->setValuesData(array($productValue1));

    $group->setProductTemplate($productTemplate);
    $this->groupSaver->save($group);

      

+3


source to share


1 answer


I advise you to use Pim\Bundle\CatalogBundle\Builder\ProductTemplateBuilder

to create and add attributes to your product template.



This ensures that the product template is correctly generated with empty product values.

+5


source







All Articles