0

Im building an import extention, that imports bicycles from an external Database into Magento.

I managed to create working helpers, that create a configurable product and the different bikesizes as simple products.

Now I struggle to get them linked. I tried 7 "solutions" so far, none is working. The Configuration in the configurable product wont show up.

Here are my 2 helpers, that create configurable and simple product:

public function createsimpleproduct($productoptions)
    {
      $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
      $product = $objectManager->create('\Magento\Catalog\Model\Product');

      $product->setUrlKey($productoptions['url']);
      $attid = $this->createOrGetId('marke', $productoptions['marke']);
      $product->setData('marke', $attid);
      $attid = $this->createOrGetId('rahmenhoehe', $productoptions['rahmenhoehe']);
      $product->setData('rahmenhoehe', $attid);
      $product->setSku($productoptions['sku']); // Set your sku here
      $product->setName($productoptions['name']); // Name of Product
      $product->setAttributeSetId(4); // Attribute set id
      $product->setStatus(1); // Status on product enabled/ disabled 1/0
      $product->setWeight(10); // weight of product
      $product->setVisibility(4); // visibilty of product (catalog / search / catalog, search / Not visible individually)
      $product->setTaxClassId(1); // Tax class id
      $product->setTypeId('simple'); // type of product (simple/virtual/downloadable/configurable)
      $product->setPrice(100); // price of product
      $product->setEan($productoptions['ean']);
      $product->setStockData(
                              array(
                                  'use_config_manage_stock' => 1,
                                  'manage_stock' => 1,
                                  'is_in_stock' => 1,
                                  'default_stock' => 10,
                                  'qty' => 10
                              )
                          );


      $product->save();
      return $product;
    }

And the one for the configurable product:

public function createconfigproduct($productoptions)
    {
      $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
      $product = $objectManager->create('\Magento\Catalog\Model\Product');
      $product->setUrlKey($productoptions['url']);
      $product->setSku($productoptions['sku']."-base"); // Set your sku here
      $product->setName($productoptions['name']); // Name of Product
      $product->setAttributeSetId(4); // Attribute set id
      $product->setStatus(1); // Status on product enabled/ disabled 1/0
      $product->setVisibility(3); // visibilty of product (catalog / search / catalog, search / Not visible individually)
      $product->setTaxClassId(1); // Tax class id
      $product->setTypeId('configurable'); // type of product (simple/virtual/downloadable/configurable)
      $product->setPrice(100); // price of product
      $product->setDescription('Geiles Teil');
      $attid = $this->createOrGetId('marke', $productoptions['marke']);
      $product->setData('marke', $attid);
      $product->setStockData(
                              array(
                                  'use_config_manage_stock' => 1,
                                  'manage_stock' => 1,
                                  'is_in_stock' => 1
                              )
                          );


      $product->save();
      return $product;
    }

A third helper function, were I can just give the configurable product id and the simple product ids (as an array) into, would be really nice. But all the functions I tried so far are failing. It looks like I need the Attribute(Id), that I want to be the Attribute, I want to be the "configurable" one - That problem is already solved by 2 other functions - so no worries, if those are needed in a possible solution.

Thanks in advance

Some solutions I tried so far:

How do I create a configurable product programmatically and assign simple products?

Magento 2: How to create configurable product Programmatically

https://firebearstudio.com/blog/how-to-programmatically-create-a-configurable-magento-2-product.html

https://www.mageplaza.com/devdocs/magento-2-create-product-programmatically.html

1

you will need to tell magento first on what attribute you need to create configurable product for example , if i need to create configurable product on the basis of size and color ,

  1. first i will do this .(Set on which attribute the configurable will be created)

     $attributeModel = $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute');
     $attributes = array(93,146);/*93 color | 146 size */
     foreach ($attributes as $key => $attributeId) {
     $data = array('attribute_id' => $attributeId, 'product_id' => $configurable_productId, 'position' => $key);
                 $attributeModel->setData($data)->save();
     }
    
  2. Assign simple product ids to your configurable product object

    $configurable_product->setAssociatedProductIds($simple_product_ids);

where $simple_product_ids is an array containing ids of separate simple product. i created simple product and configurable product at the same time so i did this

An important thing to note here is , for this to work both color and size attribute must have global scope and should be either dropdown or visual swatch

I had the exactly same requirement where i have to create custom product import functionality , through there was a lot extra things that i need to implement here i have tried to extracted the required code

$configurable_product = $objectManager->create('Magento\Catalog\Model\Product');
$configurable_product->setStoreId(0); 
$configurable_product->setPrice($configurable_product_row['price']);
$configurable_product->setWebsiteIds($website_ids);

/*add dropdown attribute value to products */
$additional_select_values_array=array("brand","season","celebrity_name","pattern","sleeve","neck","length","occasion"
);
foreach ($additional_select_values_array as  $option_name) {
    $attrid = creatOrGetId($option_name);
    $configurable_product->setData("$option_name",$attrid);
}

$configurable_product->setSku($configurable_product_row['sku']);
  $configurable_product->setAttributeSetId(13);
  $configurable_product->setStatus((int)$configurable_product_row['enable']);
  $configurable_product->setTypeId('configurable'); 
  $configurable_product->setWeight(0);
  $configurable_product->setVisibility(4);
  $configurable_product->setTaxClassId($tax_class);
  $configurable_product->setStockData(array('use_config_manage_stock' => 0,'manage_stock' => 1,'is_in_stock' => 1,));
  
  $urlKeyY = makeUrlKey($configurable_product_row['name']);
  $configurable_product->setUrlKey($urlKeyY);
  $configurable_product->setUrlPath($urlKeyY);
  $configurable_product->setIsMassupdate(true);
  $configurable_product->setExcludeUrlRewrite(true);
  $configurable_product->save();
  $configurable_productId = $configurable_product->getId();
  $attributeModel = $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute');
  $attributes = array(93,146);/*93 color | 146 size */
  foreach ($attributes as $key => $attributeId) {
      $data = array('attribute_id' => $attributeId, 'product_id' => $configurable_productId, 'position' => $key);
      $attributeModel->setData($data)->save();
  }
  $configurable_product->setTypeId("configurable");
  $configurable_product->setAffectConfigurableProductAttributes(13);
  $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable')->setUsedProductAttributeIds($attributes, $configurable_product);
  $configurable_product->setNewVariationsAttributeSetId(13); 
  $configurable_product->setAssociatedProductIds($simple_product_ids);
  $configurable_product->setCanSaveConfigurableAttributes(true);
  $configurable_product->setCategoryIds($category_ids);
  $configurable_product->save();
}

hope it helps.

| improve this answer | |
  • 1
    $attributeModel = $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute'); $attributes = array(93,146);/*93 color | 146 size */ foreach ($attributes as $key => $attributeId) { $data = array('attribute_id' => $attributeId, 'product_id' => $configurable_productId, 'position' => $key); $attributeModel->setData($data)->save(); } So this is the important part and it has to be done, while creating the configurable product? Have I understood that correctly? – Mario Jun 17 at 17:25
  • yes mario , the same is needed to be done when we create configurable product from backend , before adding simple products to configurable we have to tell magento on whic h attribute we are going to join the product ..after doing this we pass simple product ids to configurable one .. – Kumar Jun 19 at 10:18
  • i also have created full functionality to import product from csv from a php file ...but the code is now become complicated sue to client custom requirement ..if you want we can send you the whole code .. :) – Kumar Jun 19 at 10:20
  • 1
    Thank you, but Im already done with it, thanks for your answer - it helped a lot :D – Mario Jun 19 at 18:25

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.