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

1 Answer 1

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);
}
12
  • same..product is creating.. but now with this exception . 1 exception(s): Exception #0 (Magento\Framework\Validator\Exception): Missed values for option required fields Invalid option type Invalid option value
    – Ajwad Syed
    May 23, 2019 at 9:06
  • try updated answer if you get the same error or not
    – fmsthird
    May 23, 2019 at 9:10
  • Exception has gone but store view is same.. i need default store view..
    – Ajwad Syed
    May 23, 2019 at 9:15
  • change ->setStoreId(0) to this ->setStoreId($product->getStoreId()) and check
    – fmsthird
    May 23, 2019 at 9:19
  • 1
    Yes it worked, please update the answer so that I will accept it.
    – Ajwad Syed
    May 23, 2019 at 9:56

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .