0

I know this has been asked a lot of times already but in none of the topics a descend answer has been posted.

Creating a configurable product and simple products are pretty straight forward but im having issues to add a configuration to the configurable product and using that to add simple products to the configurable product.

Note that i mean doing this by code and not in the backend.

This is what i have so far

    /**
     * @param $configproduct \Magento\Catalog\Api\Data\ProductInterface
     * @param $assocproducts
     * @param $params
     */
    protected function addProductsToConfigurable($configproduct, $assocproducts, $params)
    {
        $configproduct = $this->_productRepository->get($configproduct->getSku());

        $attribute = $this->CheckOrConvertAttribute($params['attribute'], $params['attributesetid']);

        if (!isset($attribute)) {
            $this->messageManager->addErrorMessage('A problem occured convering the multiselect attribute');
            return;
        }

        $optionsInterface = $this->_attributeOptionProviderInterfaceFactory->create();
        /**
         * @var $optionsInterface \Magento\ConfigurableProduct\Model\AttributeOptionProviderInterface
         */


        $linkmanagement = $this->_linkManagement->create();
        /**
         * @var $linkmanagement \Magento\ConfigurableProduct\Api\LinkManagementInterface
         */


        $attribute = $this->_eavConfig->getAttribute(ProductAttributeInterface::ENTITY_TYPE_CODE, $attribute->getAttributeCode());
        $options = $attribute->getOptions();

        array_shift($options); //remove the first option which is empty

        $attributeValues = [];


        $configurableOptionsInterface = $this->_optionInterface->create();
        /**
         * @var $configurableOptionsInterface \Magento\ConfigurableProduct\Api\Data\OptionInterface
         */

        $optionvalues = array();
        $position = 0;

        $configurableOptions = $this->getConfigurableAttributesData(array($attribute->getId()));
        $optionfactory = $this->_option->create($configurableOptions);
        $configproduct->getExtensionAttributes()->setConfigurableProductOptions($optionfactory);

        $configproduct = $this->_productRepository->get($configproduct->getSku());

        $associatedProductIds = array();
        foreach ($assocproducts as $assocproduct) {

            /**
             * @var $assocproduct \Magento\Catalog\Model\Product
             */


            $assocproduct = $this->_productRepository->get($assocproduct->getSku());


            if ($attribute->getAttributeCode() !== $params['attribute']) {
                $this->setProductSelectAttributeFromMultiselect($assocproduct, $attribute, $params['attribute']);
            }

            $associatedProductIds[] = $assocproduct->getId();

            $configproductprice = $configproduct->getPrice();
            if (!isset($configproductprice) || $configproductprice == 0 || $assocproduct->getPrice() < $configproduct->getPrice()) {
                $configproduct->setPrice($assocproduct->getPrice());
            }

        }

        $configproduct->getExtensionAttributes()->setConfigurableProductLinks(array_filter($associatedProductIds));

        try {
            $this->_saveHandler->execute($configproduct);
//            $this->_productRepository->save($configproduct);
        } catch (\Exception $e) {
            $this->messageManager->addErrorMessage($e->getMessage());
        }
    }

 /**
     * Get Configurable Attribute Data
     *
     * @param int[] $attributeIds
     * @return array
     */
    private function getConfigurableAttributesData($attributeIds)
    {
        $configurableAttributesData = [];
        $attributeValues = [];
        $attributes = $this->_attributeFactory->create()
            ->getCollection()
            ->addFieldToFilter('attribute_id', $attributeIds)
            ->getItems();
        foreach ($attributes as $attribute) {
            foreach ($attribute->getOptions() as $option) {
                if ($option->getValue()) {
                    $attributeValues[] = [
                        'label' => $option->getLabel(),
                        'attribute_id' => $attribute->getId(),
                        'value_index' => $option->getValue(),
                    ];
                }
            }
            $configurableAttributesData[] =
                [
                    'attribute_id' => $attribute->getId(),
                    'code' => $attribute->getAttributeCode(),
                    'label' => $attribute->getStoreLabel(),
                    'values' => $attributeValues,
                ];
        }

        return $configurableAttributesData;
    }
  • which issue you are facing while try above code? – Dhiren Vasoya Jan 27 '18 at 5:52
  • Im converting multiselect attributes to select attributes and those dont get saved in the product i guess – CompactCode Jan 29 '18 at 4:14
0

I fixed this by making sure the products had the select attribute set because i convert multi select into select.

Providing the code would not be useful since its very scenario bound

but if anyone needs it , ask here

  • Hi, i also have issue , i have created simple products and Configurable product programatically, but simple products are not assigning to Config product. if you can share your code would be great help. may be we can get some hint from your code. – Dharmesh Nov 4 '18 at 17:40
  • Saving config products seemed to be very complicated depending on the version that you are running. I will however look into this again soon and i will post the most needed code here. Currently not on my project list but it will be by the end of this week. I will update the answer here accordingly – CompactCode Nov 6 '18 at 10:49

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.