public function deleteAction(Request $request, $variant, $redirectPath = null)
{
/**
* @var ProductInterface $product
* @var VariantInterface $variant
*/
$product = $variant->getProduct();
/*
* Getting the list of unique Attributes associated with
* the Variant to be deleted. This is a safety check,
* since a Variant should *not* have more than one
* option with the same Attribute
*/
$variantAttributes = $this->getUniqueAttributesFromVariant($variant);
$notRemovableAttributes = [];
/**
* @var VariantInterface $iteratedVariant
*
* Getting all the Attributes by iterating over the parent
* product Variants (except for the Variant being deleted)
* to see if we can safetly remove from the product collection
* the Attributes associated with the Variant to be removed
*
*/
foreach ($product->getVariants() as $iteratedVariant) {
/*
* We want to collect Attributes from Varints other
* than the one we want to delete
*/
if ($iteratedVariant == $variant) {
/*
* Do not add attributes from Variant to be deleted
*/
continue;
}
$notRemovableAttributes = array_merge($notRemovableAttributes, $this->getUniqueAttributesFromVariant($iteratedVariant));
}
/**
* @var AttributeInterface $variantAttribute
*
* Checking whether we can safely de-associate
* Attributes from the Variant we are deleting
* from parent product Attribute collection
*/
foreach ($variantAttributes as $variantAttribute) {
if (in_array($variantAttribute, $notRemovableAttributes)) {
continue;
}
$product->removeAttribute($variantAttribute);
}
$this->flush($product);
return parent::deleteAction($request, $variant);
}