php - Magento duplicating a product programtically is not displaying in the frontend -


i new magneto have tried duplicate product programmatically , have succeeded problem duplicated product showing in magneto admin side while in frontend product not displaying below code please tell me issue helpful me. have created separate module below code.

class magentotutorial_helloworld_indexcontroller extends mage_core_controller_front_action {             public function indexaction() {         $final = $_post['value'];         $obj = mage::getmodel('catalog/product');         $_product = $obj->load($final);          $newproduct = $_product->duplicate();          $newproduct->setstatus(1);           $newproduct->setsku('value'.$final);          $newproduct->setwebsiteids($_product->getwebsiteids());         $newproduct->getresource()->save($newproduct);     }  } 

this function posted creates duplicate product. however, not set below attribute (due it's not visible in frontend):

  1. navigate catalog > manage products > duplicated product > inventory

qty 0 , stock "out of stock" - need write below piece of code in function set product stock: "in stock" , qty: [some default value] say, 100.

after line calls out $newproduct->setwebsiteids($_product->getwebsiteids());, can insert below lines:

$stockitem = mage::getmodel('cataloginventory/stock_item')->loadbyproduct($newproduct->getid()); if ($stockitem->getid() > 0 && $stockitem->getmanagestock()) {     $qty = 100; //set default max value     $stockitem->setqty($qty);     $stockitem->setisinstock((int)($qty > 0));     $stockitem->save(); } 
  1. you need run re-index either manually or automate it

the product display in frontend. see screenshot below:

enter image description here

[edit]

use below code , let me know if works you:

public function indexaction() {     $productid      = $this->getrequest()->getparam('value');     $productobject  = mage::getmodel('catalog/product');      $_product   = $productobject->load($productid);      $newproduct = $_product->duplicate();      $newproduct->setstatus(1);     //$newproduct->setname('duplicate-' . $_product->getname());     $newproduct->setsku('value' . $productid);     $newproduct->setwebsiteids($_product->getwebsiteids());      $stockitem = mage::getmodel('cataloginventory/stock_item')->loadbyproduct($newproduct->getid());     if ($stockitem->getid() > 0 && $stockitem->getmanagestock())     {         $qty = 100;         $stockitem->setqty($qty);         $stockitem->setisinstock((int)($qty > 0));         $stockitem->save();     }      $newproduct->getresource()->save($newproduct);      $indexers = mage::getsingleton('index/indexer')->getprocessescollection();     foreach ($indexers $indexer)     {         $indexer->reindexeverything();     } } 

hope helps.

happy coding...


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -