/** * {@inheritdoc} */ public function save($group, array $options = []) { /* @var GroupInterface */ if (!$group instanceof GroupInterface) { throw new \InvalidArgumentException(sprintf('Expects a "Pim\\Bundle\\CatalogBundle\\Model\\GroupInterface", "%s" provided.', ClassUtils::getClass($group))); } $this->eventDispatcher->dispatch(GroupEvents::PRE_SAVE, new GenericEvent($group)); $options = $this->optionsResolver->resolveSaveOptions($options); $this->versionContext->addContextInfo(sprintf('Comes from variant group %s', $group->getCode()), $this->productClassName); if ($group->getType()->isVariant()) { $template = $group->getProductTemplate(); if (null !== $template) { $this->templateMediaManager->handleProductTemplateMedia($template); } } $this->objectManager->persist($group); if (true === $options['flush']) { $this->objectManager->flush(); } if (count($options['add_products']) > 0) { $this->addProducts($options['add_products']); } if (count($options['remove_products']) > 0) { $this->removeProducts($options['remove_products']); } if ($group->getType()->isVariant() && true === $options['copy_values_to_products']) { $this->copyVariantGroupValues($group); } $this->eventDispatcher->dispatch(GroupEvents::POST_SAVE, new GenericEvent($group)); }
/** * Update the variant group values * * @param GroupInterface $variantGroup * @param array $groupData */ protected function updateVariantGroupValues(GroupInterface $variantGroup, array $groupData) { $valuesData = $this->filterVariantGroupData($groupData, false); if (!empty($valuesData)) { $values = $this->denormalizeValuesFromItemData($valuesData, $variantGroup->getProductTemplate()); $this->validateValues($variantGroup, $values, $groupData); $template = $this->getProductTemplate($variantGroup); $template->setValues($values); $this->templateMediaManager->handleProductTemplateMedia($template); $structuredValuesData = $this->normalizeValuesToStructuredData($template->getValues()); $template->setValuesData($structuredValuesData); } }