php – Magento 2, programmatically create configurable products

Hello I have the necessity of creating both simpe and configurable products.

This is what (following online guidelines) I have right now:

$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');
$salt = rand(10, 999);

//REGARDING SIMPLE PRODUCTS
$simple_product = $objectManager->create('MagentoCatalogModelProduct');
$simple_product->setSku('test-simple' . $salt);
$simple_product->setName('test name simple' . $salt);
$simple_product->setAttributeSetId(9); //my own attribute set 

$simple_product->setData('157', '9'); // my custom attribute flavor and it's assigned value

$simple_product->setStatus(1);
$simple_product->setTypeId('simple');
$simple_product->setPrice(10);
$simple_product->setWebsiteIds(array(1));
$simple_product->setCategoryIds(array(14)); //it's category
$simple_product->setStockData(array(
    'use_config_manage_stock' => 0, //'Use config settings'?
    'manage_stock' => 1, //manage stock
    'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
    'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
    'is_in_stock' => 1, //Stock Availability
    'qty' => 100 //qty
    )
);
$simple_product->save();

$productRepository = $objectManager->get('MagentoCatalogApiProductRepositoryInterface');
$product = $productRepository->getById($simple_product->getId());
$product->setFlavor('BBBB');  //my custom attribute is called flavor and BBBB it's is value
$product->getResource()->saveAttribute($product, 'flavor');

$simple_product_id = $simple_product->getId(); //get ID
echo "simple product id: ".$simple_product_id."n";

This works just perfect, now I’d like to create a configurable product and assign this product to it:

//REGARDING CONFIGURABLE PRODUCTS
$configurable_product = $objectManager->create('MagentoCatalogModelProduct');
$configurable_product->setSku('test-configurable' . ++$salt);
$configurable_product->setName('test name configurable' . ++$salt);
$configurable_product->setAttributeSetId(9);
$configurable_product->setStatus(1);
$configurable_product->setTypeId('configurable');
$configurable_product->setPrice(11);
$configurable_product->setWebsiteIds(array(1));
$configurable_product->setCategoryIds(array(14));
$configurable_product->setStockData(array(
    'use_config_manage_stock' => 0, 
    'manage_stock' => 1, 
    'is_in_stock' => 1, 
    'qty' => 999
    )
);

$configurable_product->getTypeInstance()->setUsedProductAttributeIds(array(157),$configurable_product); //attribute ID of attribute flavor

$configurableAttributesData = $configurable_product->getTypeInstance()->getConfigurableAttributesAsArray($configurable_product);

$configurable_product->setCanSaveConfigurableAttributes(true);
$configurable_product->setConfigurableAttributesData($configurableAttributesData);

$configurableProductsData = array();
$configurableProductsData($simple_product_id) = array( //our simple product ID
    '0' => array(
        'label' => 'gusto', //attribute label
        'attribute_id' => '157', //attribute ID of attribute 'flavor' in my store
        'value_index' => '9', //value of 'BBBB' index of the attribute 
        'is_percent'    => 0,
        'pricing_value' => '10',
    )
);


$configurable_product->setConfigurableProductsData($configurableProductsData);

$configurable_product->save();

echo "configurable product id: ".$configurable_product->getId()."n";

I successifully create both products, but the simple is not associated with the configurable one.

Of course I do something wrong…. can you please guide me in the right direction?

seo – Is uniting different variations of a same product under a single configurable product a good idea?

Maybe this isn’t fully related to Magento, but I don’t know where else to ask.

I work for an hardware store and we are showing more and more products on our online catalog.

It is obvious that catalog pages will become overcrowded with different variations of the same product, especially since we specialize in screws, nuts and bolts. Already, this can be seen with sockets, or screwdrivers, of different sizes.

The storefront has multiple filters to help the customers find what they need, but at the same time I wonder if filters are either not enough, or if they are going to scare away lazy customers who don’t want to click around too much.

So, I wish to unite those kind of items under configurable products, but will it have any impact on search engine results in the long run?

Thanks,

How to getSku for configurable products in magento 2

I want to get Sku for selected options.

enter image description here

Above image i have select blue and M. So this options product sku get in magento 2.

Please Help me.

THANKS.

magento2 – Export and Import configurable product price changes

I couldn’t anywhere find a solution to my question, so I decided to post here.

I have added a configureable product to my store with 2 attributes (dimensions and material) both have different sku’s for each combination and different prices.

Now I managed to export the data, edited the prices, uploaded it again, but when viewing the “configurable product” in configuration and shop frontend still the old prices show up.

Does anybody know what I might have overlooked?!

Thanks in advance!
Michael

Sorting by “Best selling” not working for configurable products M2.4.x (but for simple products)

the sorting by “Best selling” is not working for configurable products in M2.4.x

Its well working for categories with simple products.

When mixed category, first the best-selling simple products are shown, after that all the configurable products (in wrong order).

We use the plug-in from weltpixel. According weltpixel, their plugin cannot be the reason.

The whole thing was working till oct 2020 approx, it could be after the update from M2.3 to M2.4.1 ist stopped working but i am not sure..

Any ideas???

maganto2.3.1 Add configurable products,All product pages show the same attributes

enter image description here

Add configurable products, the size and color will appear on the new page of each product, which makes me very distressed

Configurable Product Options Randomly Empty in Frontend

On Magento 2.3.6-p1 we have an issue where customers cannot select a product’s options because the options list is empty.

This is very inconsistent since different products with the same option attribute sometimes show the options list and sometimes don’t.

Here is an example for an empty options list:
Product woth empty options list

This is a working options list for the exact same options attribute:
Working options dropdown list

Both products are identical besides the pictures and names.

It seems that just very seldomly options are shown for a product when most of the time it does not work; the reason for this is not known to me.

The problem exists since we upgraded from Magento 2.3.0 to 2.3.6-p1.

This issue has a heavy impact since most products sare sold as configurable products which customers are currently unable to buy. (My wife is killing me!)

Besides obvious hints like clearing caches, reindexing or making sure the products are in stock I found neither a solution nor any direction to look for to solve the problem. The server logs show no irregularities, the Magento cron job is running fine.

Any help is appreciated!

Edit: I was told that this issue already sometimes happened on Magento 2.3.0 but not as often.

magento2 – Images not showing on configurable products

I have all of the needed attributes in the attribute set, but none of the images are showing on configurable products.
They’re visible on the backend, and I see them when I add the images to the cart, they just won’t show up in gallery or product view.

What can I do about this?

Thanks

configurable product – “Only 0 Left” message has suddenly appeared after upgraded to 2.4.2

As wrote in the title, after I’ve upgraded from Magento 2.4.1 to 2.4.2 the message “Only 0 Left” has suddenly appeared in the product page (configurable product).

I checked in Stores->Configuration->Catalog->Inventory->Stock Options->Only X left Threshold and it’s set to 0. I tried to set to -1 but nothing’s changed.

enter image description here

Any solutions?

magento2 – Magento 2 – Configurable product, get attribute value from associated simple products

I’m looking for a way to grab the values of an attribute (let’s say color) from all simple products associated with a configurable.

So if my associated simple products are:

  • Simple1 – Red
  • Simple2 – Blue
  • Simple3 – Green

On my configurable, I’m looking for a way to output “Red, Blue, Green”.

Any suggestions would be appreciated.