1

I want to unlink some (not all) simple products from configurable product. I don't want to delete simple products, I just want to unlink from the main configurable product programmatically.

1

1 Answer 1

0

the code below should help you removing the simple from your configurable

  class ConfigurableProductHandler
    {
        /**
         * @var \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable
         */
        private $configurable;
        /**
         * @var \Magento\Catalog\Api\ProductRepositoryInterface
         */
        private $productRepository;
    
        /**
         * ConfigurableProductHandler constructor.
         * @param \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $configurable
         */
        public function __construct(
            \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
            \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $configurable
        ) {
            $this->configurable = $configurable;
            $this->productRepository = $productRepository;
        }
    
        public function clearAssociatedProducts($sku)
        {
            try {
                $product = $this->productRepository->get($sku);
                $this->configurable->saveProducts($product, []);
            } catch (NoSuchEntityException $e) {
            }       
        }
 
   }

Edit: if we want to unlink specific products, we can refer ourselves to the following:

$simpleIds = $product->getAssociatedProductIds();
   $associatedProducts= $product->getUsedProducts();

for instance, if we want to unlink the simple product with the id=1, we'd add to remove this id from all the array $simpleIds to the configurable. If we want to find the product id by comparing the simple product options (colour, size), the second method above would be useful. Finally, once the array contains the product ids we want to link, we can replace

$this->configurable->saveProducts($product, []);

by

$this->configurable->saveProducts($product, $simpleIds);
3
  • Right. I see such helping links. But it removes all simple products from its main configurable product. I needed to remove some (not all) simple products from configurable. Will it work if I pass the relevant products array as second param of $this->configurable->saveProducts($product, $retainedSimpleProductArr); . What should be exact data format for $retainedSimpleProductArr ....? Commented Jul 10, 2020 at 11:00
  • Yes, exactly you’ve got it Commented Jul 10, 2020 at 11:07
  • Perhaps you could update your reply to answer the question correctly. The question was not how to remove ALL but specific associated simple products from the configurable parent product.
    – paj
    Commented Mar 7 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.