'Magento2: product not visible after import

I'm using this piece of code:

$product->setSku($csvProduct[0]); // Set your sku here $product->setName($csvProduct[6]); // Name of Product $product->setData('icecat_id',$csvProduct[0]); $product->setData('is_featured',0); $product->setData('is_salable',1); $product->setData('gift_message_available',0); $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(0); // Tax class id $product->setTypeId('simple'); // type of product (simple/virtual/downloadable/configurable) $product->setPrice(100); // price of product $product->setCategoryIds($categoryIds); $product->setStockData( [ 'use_config_manage_stock' => 0, 'manage_stock' => 1, 'is_in_stock' => 1, 'qty' => 999 ] ); $product->save();

The problem is that it's visible in backend not in frontend. When I save product in admin product is starting to be visible in frontend also. The qestion is what' wrong with my code or can You give me any idea how it should be done properly. I spent to much hour trying to debug it. Pls help.

Magento version 2.4.3

I spent a lot of hours trying to debug it. I tried to figure out what is the difference before and after save. No luck.



Solution 1:[1]

Probably you need to reindex:

bin/magento indexer:reindex

Or check and reindex required:

bin/magento indexer:status

More info: https://devdocs.magento.com/guides/v2.4/config-guide/cli/config-cli-subcommands-index.html

Solution 2:[2]

Website ids were missing ... So simple, so stupid, but maybe useful for someone else.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sebollo4
Solution 2 Shack Ro