2

I am creating simple product programmatically, but for displaying it on frontend I need to edit product from admin side and set its view from

All Store Views

to

Default Store View

Mannually, then it appears on the frontend.

I am creating product as:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
        $product = $objectManager->create('\Magento\Catalog\Model\Product');
        $product->setSku('my-sku11'); // Set your sku here
        $product->setName('Sample Simple Product11'); // 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(0); // Tax class id
        $product->setTypeId('simple'); // type of product (simple/virtual/downloadable/configurable)
        $product->setPrice(100); // price of product
        $product->setStockData(
                                array(
                                    'use_config_manage_stock' => 0,
                                    'manage_stock' => 1,
                                    'is_in_stock' => 1,
                                    'qty' => 99999
                                )
                            );
        $product = $product->save();

I have tried this,

$product->setStoreId(0); //1,2,3,4

but it is not working. How can I achieve it ? Thanks

Ajwad Syed
  • 1,591
  • 25
  • 61

1 Answers1

1

I'm not sure of this, I just found it but you can give it a try
Add $product->setWebsiteIds(array(1));

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
$product = $objectManager->create('\Magento\Catalog\Model\Product');
$product->setSku('my-sku11'); // Set your sku here
$product->setName('Sample Simple Product11'); // 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(0); // Tax class id
$product->setTypeId('simple'); // type of product (simple/virtual/downloadable/configurable)
$product->setPrice(100); // price of product
$product->setStockData(
         array(
              'use_config_manage_stock' => 0,
              'manage_stock' => 1,
              'is_in_stock' => 1,
              'qty' => 99999
              )
             );
$product->setWebsiteIds(array(1));
$product = $product->save();

// Adding Custom option to product
$options = array(
    array(
        "sort_order" => 1,
        "title" => "Option 1",
        "price_type" => "fixed",
        "price" => "10",
        "type" => "field",
        "is_require" => 0
    )
);
foreach ($options as $arrayOption) {
    $product->setHasOptions(1);
    $product->getResource()->save($product);
    $option = $objectManager->create('\Magento\Catalog\Model\Product\Option')
        ->setProductId($product->getId())
        ->setStoreId(0)
        ->addData($arrayOption);
    $option->save();
    $product->addOption($option);
}
fmsthird
  • 4,592
  • 4
  • 17
  • 41