/** * Remove an attribute from product template * * @param AttributeInterface $attribute */ protected function removeFromProductTemplate(AttributeInterface $attribute) { if (null === $this->productTplBuilder || null === $this->productTplRepository) { return; } foreach ($this->productTplRepository->findAll() as $productTemplate) { if ($productTemplate->hasValueForAttribute($attribute)) { $this->productTplBuilder->removeAttribute($productTemplate, $attribute); $attributeCodes = $productTemplate->getAttributeCodes(); empty($attributeCodes) ? $this->objectManager->remove($productTemplate) : $this->objectManager->persist($productTemplate); } } }
function it_updates_a_product_template_if_attribute_has_been_deleted($eventDispatcher, $objectManager, $optionsResolver, AttributeInterface $attribute, ProductTemplateBuilderInterface $productTemplateBuilder, ProductTemplateRepositoryInterface $productTemplateRepository, ProductTemplate $productTemplate) { $optionsResolver->resolveRemoveOptions([])->willReturn(['flush' => true]); $objectManager->remove($attribute)->shouldBeCalled(); $objectManager->flush()->shouldBeCalled(); $attribute->getCode()->willReturn('test'); $productTemplate->getValuesData()->willReturn(['test', 'test2']); $productTemplateRepository->findAll()->willReturn([$productTemplate]); $productTemplate->hasValueForAttribute($attribute)->willReturn(true); $productTemplateBuilder->removeAttribute($productTemplate, $attribute)->shouldBeCalled(); $productTemplate->getAttributeCodes()->willReturn(['test2']); $objectManager->persist($productTemplate)->shouldBeCalled(); $objectManager->flush()->shouldBeCalled(); $this->remove($attribute); }
/** * @param RemoveEvent $event */ public function preRemove(RemoveEvent $event) { $subject = $event->getSubject(); if (!$subject instanceof AttributeInterface) { return; } $productTemplates = $this->productTplRepository->findByAttribute($subject); foreach ($productTemplates as $productTemplate) { $this->productTplBuilder->removeAttribute($productTemplate, $subject); $attributeCodes = $productTemplate->getAttributeCodes(); if (empty($attributeCodes)) { $this->objectManager->remove($productTemplate); } else { $this->objectManager->persist($productTemplate); } } }