Skip to main content
added 3 characters in body
Source Link

I am programming a custom import system with Magento libraries, but when I import configurable products I get an SQL error. 

This is my configurable product function (only a part of my code):

I am programming a custom import system with Magento libraries, but when I import configurable products I get an SQL error. This is my configurable product function (only a part of my code):

I am programming a custom import system with Magento libraries, but when I import configurable products I get an SQL error. 

This is my configurable product function (only a part of my code):

added 2 characters in body
Source Link
function createConfigurableProduct($product, $objectManager, $_product) {

    // 1. Get swatches attributes
    $attr_swatches = explode("#", $product["configurable_attributes"]);
    $attr_ids = array();
    $attr_options = array();
    foreach ($attr_swatches as $attr_sw) {
        $attribute = $objectManager->create('\\Magento\\Catalog\\Model\\Product\\Attribute\\Repository')
            ->get($attr_sw);
        array_push($attr_ids, $attribute->getId());
        $attributeOptionAll = $objectManager->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class)
            ->setPositionOrder('asc')
            ->setAttributeFilter($attribute->getId())
            ->setStoreFilter()
            ->load();
        array_push($attr_options, $attributeOptionAll);
    }

    // 2. Select configurable attributes
    $_product->getTypeInstance()->setUsedProductAttributeIds($attr_swatches, $_product);

    // 2. prepare information for each simple product
    $configurableAttributesData = $_product->getTypeInstance()->getConfigurableAttributesAsArray($_product);
    $_product->setConfigurableAttributesData($configurableAttributesData);

    $simple_products = explode("#", $product["simples_skus"]);
    $simple_ids = array();

    $configurableProductsData = array();
    foreach ($simple_products as $prod) {

        $productId = $objectManager->get('Magento\Catalog\Model\Product')->getIdBySku($prod);
        array_push($simple_ids, $productId);
        $simple_product = $objectManager->get('\Magento\Catalog\Model\Product')->load($productId);

        // VISIBILITY_NOT_VISIBLE = 1
        // VISIBILITY_IN_CATALOG = 2
        // VISIBILITY_IN_SEARCH = 3
        // VISIBILITY_BOTH = 4 

        $simple_product->setVisibility(1);
        $simple_product->save();

        $i = 0;
        $configurableProductsData['' . $productId] = array(); //['920'] = id of a simple product associated with this configurable
        foreach ($attr_swatches as $attr) {
            $attr_value = $simple_product->getData($attr);
            //$optionText = $attribute->getSource()->getOptionText($attr_value);
            var_dump($attr_value);
            //var_dump($optionText);

            $value_index = getOptionIDByCode($attr_options[$i], $attr_value);

            $productData = [
                'label' => $attr_value, //attribute label
                'attribute_id' => $attr_ids[$i], //attribute ID of attribute 'color' in my store
                'value_index' => $value_index, //value of 'Green' index of the attribute 'color'
                'is_percent' => '0', //fixed/percent price for this option
                'pricing_value' => '0' //value for the pricing
            ];

            $configurableProductsData[$productId] = $productData;
            $configurableAttributesData[$i]['values'][] = $productData;
            $i++;
        }
    }

    echo "configurableProductsData<pre>";
    print_r($configurableProductsData);
    echo "</pre>";

    echo "configurableAttributesData<pre>";
    print_r($configurableAttributesData);
    echo "</pre>";

    // 4. set data in 2 required formats
    $_product->setConfigurableProductsData($configurableProductsData);
    $_product->setConfigurableAttributesData($configurableAttributesData);

    // 5. save product with special flag
    $_product->setCanSaveConfigurableAttributes(true);
    $_product->save();

    //N.B. We need to have the same attribute set in both configurable product and it's associated product.
}
function createConfigurableProduct($product, $objectManager, $_product) {

    // 1. Get swatches attributes
    $attr_swatches = explode("#", $product["configurable_attributes"]);
    $attr_ids = array();
    $attr_options = array();
    foreach ($attr_swatches as $attr_sw) {
        $attribute = $objectManager->create('\\Magento\\Catalog\\Model\\Product\\Attribute\\Repository')
            ->get($attr_sw);
        array_push($attr_ids, $attribute->getId());
        $attributeOptionAll = $objectManager->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class)
            ->setPositionOrder('asc')
            ->setAttributeFilter($attribute->getId())
            ->setStoreFilter()
            ->load();
        array_push($attr_options, $attributeOptionAll);
    }

    // 2. Select configurable attributes
    $_product->getTypeInstance()->setUsedProductAttributeIds($attr_swatches, $_product);

    // 2. prepare information for each simple product
    $configurableAttributesData = $_product->getTypeInstance()->getConfigurableAttributesAsArray($_product);
    $_product->setConfigurableAttributesData($configurableAttributesData);

    $simple_products = explode("#", $product["simples_skus"]);
    $simple_ids = array();

    $configurableProductsData = array();
    foreach ($simple_products as $prod) {

        $productId = $objectManager->get('Magento\Catalog\Model\Product')->getIdBySku($prod);
        array_push($simple_ids, $productId);
        $simple_product = $objectManager->get('\Magento\Catalog\Model\Product')->load($productId);

        // VISIBILITY_NOT_VISIBLE = 1
        // VISIBILITY_IN_CATALOG = 2
        // VISIBILITY_IN_SEARCH = 3
        // VISIBILITY_BOTH = 4
        $simple_product->setVisibility(1);
        $simple_product->save();

        $i = 0;
        $configurableProductsData['' . $productId] = array(); //['920'] = id of a simple product associated with this configurable
        foreach ($attr_swatches as $attr) {
            $attr_value = $simple_product->getData($attr);
            //$optionText = $attribute->getSource()->getOptionText($attr_value);
            var_dump($attr_value);
            //var_dump($optionText);

            $value_index = getOptionIDByCode($attr_options[$i], $attr_value);

            $productData = [
                'label' => $attr_value, //attribute label
                'attribute_id' => $attr_ids[$i], //attribute ID of attribute 'color' in my store
                'value_index' => $value_index, //value of 'Green' index of the attribute 'color'
                'is_percent' => '0', //fixed/percent price for this option
                'pricing_value' => '0' //value for the pricing
            ];

            $configurableProductsData[$productId] = $productData;
            $configurableAttributesData[$i]['values'][] = $productData;
            $i++;
        }
    }

    echo "configurableProductsData<pre>";
    print_r($configurableProductsData);
    echo "</pre>";

    echo "configurableAttributesData<pre>";
    print_r($configurableAttributesData);
    echo "</pre>";

    // 4. set data in 2 required formats
    $_product->setConfigurableProductsData($configurableProductsData);
    $_product->setConfigurableAttributesData($configurableAttributesData);

    // 5. save product with special flag
    $_product->setCanSaveConfigurableAttributes(true);
    $_product->save();

    //N.B. We need to have the same attribute set in both configurable product and it's associated product.
}
function createConfigurableProduct($product, $objectManager, $_product) {

    // 1. Get swatches attributes
    $attr_swatches = explode("#", $product["configurable_attributes"]);
    $attr_ids = array();
    $attr_options = array();
    foreach ($attr_swatches as $attr_sw) {
        $attribute = $objectManager->create('\\Magento\\Catalog\\Model\\Product\\Attribute\\Repository')
            ->get($attr_sw);
        array_push($attr_ids, $attribute->getId());
        $attributeOptionAll = $objectManager->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class)
            ->setPositionOrder('asc')
            ->setAttributeFilter($attribute->getId())
            ->setStoreFilter()
            ->load();
        array_push($attr_options, $attributeOptionAll);
    }

    // 2. Select configurable attributes
    $_product->getTypeInstance()->setUsedProductAttributeIds($attr_swatches, $_product);

    // 2. prepare information for each simple product
    $configurableAttributesData = $_product->getTypeInstance()->getConfigurableAttributesAsArray($_product);
    $_product->setConfigurableAttributesData($configurableAttributesData);

    $simple_products = explode("#", $product["simples_skus"]);
    $simple_ids = array();

    $configurableProductsData = array();
    foreach ($simple_products as $prod) {

        $productId = $objectManager->get('Magento\Catalog\Model\Product')->getIdBySku($prod);
        array_push($simple_ids, $productId);
        $simple_product = $objectManager->get('\Magento\Catalog\Model\Product')->load($productId);

        // VISIBILITY_NOT_VISIBLE = 1
        // VISIBILITY_IN_CATALOG = 2
        // VISIBILITY_IN_SEARCH = 3
        // VISIBILITY_BOTH = 4 

        $simple_product->setVisibility(1);
        $simple_product->save();

        $i = 0;
        $configurableProductsData['' . $productId] = array(); //['920'] = id of a simple product associated with this configurable
        foreach ($attr_swatches as $attr) {
            $attr_value = $simple_product->getData($attr);
            //$optionText = $attribute->getSource()->getOptionText($attr_value);
            var_dump($attr_value);
            //var_dump($optionText);

            $value_index = getOptionIDByCode($attr_options[$i], $attr_value);

            $productData = [
                'label' => $attr_value, //attribute label
                'attribute_id' => $attr_ids[$i], //attribute ID of attribute 'color' in my store
                'value_index' => $value_index, //value of 'Green' index of the attribute 'color'
                'is_percent' => '0', //fixed/percent price for this option
                'pricing_value' => '0' //value for the pricing
            ];

            $configurableProductsData[$productId] = $productData;
            $configurableAttributesData[$i]['values'][] = $productData;
            $i++;
        }
    }

    echo "configurableProductsData<pre>";
    print_r($configurableProductsData);
    echo "</pre>";

    echo "configurableAttributesData<pre>";
    print_r($configurableAttributesData);
    echo "</pre>";

    // 4. set data in 2 required formats
    $_product->setConfigurableProductsData($configurableProductsData);
    $_product->setConfigurableAttributesData($configurableAttributesData);

    // 5. save product with special flag
    $_product->setCanSaveConfigurableAttributes(true);
    $_product->save();

    //N.B. We need to have the same attribute set in both configurable product and it's associated product.
}
added 1633 characters in body
Source Link

EDIT: The attribute I use for swatches in the test I made is created in an UpgradeSchema.php like this:

$eavSetup->addAttribute(
            ProductAttributeInterface::ENTITY_TYPE_CODE,
            'mycustomer_ip',
            [
                'type' => 'varchar',
                'backend' => '',
                'frontend' => '',
                'label' => 'mycustomer_ip',
                'input' => 'select',
                'class' => '',
                'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => "",
                'searchable' => false,
                'filterable' => true,
                'comparable' => true,
                'visible_on_front' => true,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => '',
                'system' => 1,
                'group' => 'My Attributes Group',
                'option' => array('values' => [
                    '',
                    'IP00',
                    'IP20',
                    'IP22',
                    'IP40',
                    'IP42',
                    'IP43',
                    'IP44',
                    'IP65',
                    'IP66',
                    'IP67',
                    'IP68'
                ])
            ]
        );

EDIT: The attribute I use for swatches in the test I made is created in an UpgradeSchema.php like this:

$eavSetup->addAttribute(
            ProductAttributeInterface::ENTITY_TYPE_CODE,
            'mycustomer_ip',
            [
                'type' => 'varchar',
                'backend' => '',
                'frontend' => '',
                'label' => 'mycustomer_ip',
                'input' => 'select',
                'class' => '',
                'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => "",
                'searchable' => false,
                'filterable' => true,
                'comparable' => true,
                'visible_on_front' => true,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => '',
                'system' => 1,
                'group' => 'My Attributes Group',
                'option' => array('values' => [
                    '',
                    'IP00',
                    'IP20',
                    'IP22',
                    'IP40',
                    'IP42',
                    'IP43',
                    'IP44',
                    'IP65',
                    'IP66',
                    'IP67',
                    'IP68'
                ])
            ]
        );
added 10 characters in body
Source Link
Loading
Source Link
Loading