3

I want to create a new variant group in Akeneo with some fixed attributes to "simulate" the behaviour of a product family. Unfortunately I don't know how to get a ProductTemplate with some attributes with empty values.

When I tried my code below, I get an error after opening the created variant group in the 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);
Sonson123
  • 10,879
  • 12
  • 54
  • 72

1 Answers1

5

I advise you to use the Pim\Bundle\CatalogBundle\Builder\ProductTemplateBuilder to create and add attributes to your product template.

This will ensure that the product template will be properly created with empty product values.

Julien Sanchez
  • 843
  • 4
  • 12