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.

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) {
            }       
        }
 
   }
| improve this answer | |
  • 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 ....? – Ghulam Akbar Jul 10 at 11:00
  • Yes, exactly you’ve got it – Herve Tribouilloy Jul 10 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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