Question

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;
    }
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top