1

I'm trying to import some products from an ERP with a cron job and I'd like to utilize the data import functionality which is built-in with magento 2.

My idea is to create a small csv file (very easy to convert my data to a csv file) which will contain product update data for each modified product on the ERP side and then use the import functionality because it's really fast compared to the other methods.

REST API and programmtically saving products seems to take considerably more time compared to the import tool.

Now my problem is that in each update, I won't be working with the whole product set. So to handle configurables I want to break it down to 3 simple steps.

  • Create / Update configurable products from my update batch
  • Create / Update simple products
  • Programmatically link simple products to configurables based on the parent SKU utilizing LinkManagementInterface.

So I'm experimenting by creating a csv file like this

sku,store_view_code,attribute_set_code,product_type,product_websites,name,description,short_description,weight,product_online,tax_class_name,visibility,price,special_price,special_price_from_date,special_price_to_date,url_key,meta_title,meta_keywords,meta_description,created_at,updated_at,new_from_date,new_to_date,display_product_options_in,map_price,msrp_price,map_enabled,gift_message_available,custom_design,custom_design_from,custom_design_to,custom_layout_update,page_layout,product_options_container,msrp_display_actual_price_type,country_of_manufacture,additional_attributes,qty,out_of_stock_qty,use_config_min_qty,is_qty_decimal,allow_backorders,use_config_backorders,min_cart_qty,use_config_min_sale_qty,max_cart_qty,use_config_max_sale_qty,is_in_stock,notify_on_stock_below,use_config_notify_stock_qty,manage_stock,use_config_manage_stock,use_config_qty_increments,qty_increments,use_config_enable_qty_inc,enable_qty_increments,is_decimal_divided,website_id,related_skus,related_position,crosssell_skus,crosssell_position,upsell_skus,upsell_position,additional_image_labels,hide_from_product_page,custom_options,bundle_price_type,bundle_sku_type,bundle_price_view,bundle_weight_type,bundle_values,bundle_shipment_type,associated_skus,configurable_variations,configurable_variation_labels,has_options,required_options
test-config-no-variations,,Default,configurable,base,test-config-no-variations,,,2222.000000,1,Taxable Goods,"Catalog, Search",,,,,test-config-no-variations,Air tank galvanized,Air tank galvanized,Air tank galvanized ,"3/9/20, 5:26 PM","3/9/20, 5:27 PM",,,Block after Info Column,,,,Use config,,,,,,,Use config,,"am_hide_price_customer_gr=NOT LOGGED IN|General|Retailer|Wholesale,am_hide_price_mode=Yes,featured=0,ts_packaging_type=None",0.0000,0.0000,1,0,0,1,1.0000,1,10000.0000,1,1,1.0000,1,1,1,1,1.0000,1,0,0,0,,,,,,,",",,,,,,,,,,,,1,1
test-config-no-variationshorizontal,,Default,simple,base,test-config-no-variationshorizontal,,,2222.000000,1,Taxable Goods,"Catalog, Search",4,,,,test-config-no-variationshorizontal,Air tank galvanized,Air tank galvanized,Air tank galvanized ,"3/9/20, 5:26 PM","3/9/20, 5:27 PM",,,Block after Info Column,,,,Use config,,,,,,,Use config,,"am_hide_price_customer_gr=NOT LOGGED IN|General|Retailer|Wholesale,am_hide_price_mode=Yes,featured=0,ts_packaging_type=None,io_installation=Horizontal",0.0000,0.0000,1,0,0,1,1.0000,1,10000.0000,1,1,1.0000,1,1,1,1,1.0000,1,0,0,0,,,,,,,",",,,,,,,,,,,,0,0
test-config-no-variationsvertical,,Default,simple,base,test-config-no-variationsvertical,,,2222.000000,1,Taxable Goods,"Catalog, Search",5,,,,test-config-no-variationsvertical,Air tank galvanized,Air tank galvanized,Air tank galvanized ,"3/9/20, 5:26 PM","3/9/20, 5:27 PM",,,Block after Info Column,,,,Use config,,,,,,,Use config,,"am_hide_price_customer_gr=NOT LOGGED IN|General|Retailer|Wholesale,am_hide_price_mode=Yes,featured=0,ts_packaging_type=None,io_installation=Vertical",0.0000,0.0000,1,0,0,1,1.0000,1,10000.0000,1,1,1.0000,1,1,1,1,1.0000,1,0,0,0,,,,,,,",",,,,,,,,,,,,0,0

which sucessfully creates 1 configurable product and 2 simple ones with the attributes I need. However the configurable product is missing the super attributes so linking the products between them breaks with an error

The parent product doesn't have configurable product options.

How can I define the configurable product options at the import step? Keep in mind I don't want to add each variations at the import step but only at the linking step because at this point I only have partial knowledge about the product variations. Meaning I might be processing only 10 variations out of a total of 100 so filling those columns would leave me with a product with incomplete data.

I've tried setting the configurable_variation_labels column to my attribute io_installation=Installation but it has no effect unless I also add the column configurable_variations and define specific variations.

Is it possible to define the configurable product attributes by which the variations will be created? Something similar to how we define super attributes in magento 1.9 before creating a configurable product.

  • After a lot of debugging I've added it to Magento 2 issues because I think the core needs to be refactored a bit for this. The behaviour is currently really weird. If anyone wants to read what I ended up doing to bypass the issue you can follow this github.com/magento/magento2/issues/28119 – gabtzi May 6 at 6:42
0

I ended up using this function to bypass the issue.

 private function addChild($parentSku, $childSku, $attributeCodes)
    {
        // sample for linking products to configurable
        $this->io->note(sprintf("Linking child %s to configurable %s against attributes %s", $childSku, $parentSku, json_encode($attributeCodes)));
        /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $config */
        $configurable = $this->productRepository->get(
          $parentSku
        );
        $child = $this->productRepository->get(
          $childSku
        );

        if (empty(
        $configurable->getExtensionAttributes()
          ->getConfigurableProductOptions()
        )) {
            $configurableOptions = [];
            foreach ($attributeCodes as $attributeCode) {
                $configurableOption = $this->optionInterfaceFactory->create();
                $configurableOption->setAttributeId(
                  $this->attributeHelper->getAttributeIdByCode($attributeCode)
                );
                $configurableOption->setLabel(
                  $this->attributeHelper->getAttributeByCode($attributeCode)
                    ->getDefaultFrontendLabel()
                );
                $optionValue = $this->optionValueInterfaceFactory->create();
                $optionValue->setValueIndex(
                  'temp'
                );//only needed for bypassing magento 2 bugs
                $configurableOption->setValues([$optionValue]);
                array_push($configurableOptions, $configurableOption);

                $configurable->getExtensionAttributes()
                  ->setConfigurableProductOptions($configurableOptions)
                ;
            }
            $this->productRepository->save($configurable);
        }

        $this->linkManagement->addChild(
          $parentSku,
          $childSku
        );
    }
| improve this answer | |

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.