我使用以下脚本(在控制器内部 - 暂时执行操作)以编程方式复制产品。
public function indexAction()
{
$data = $this->getRequest()->getParams();
$product = Mage::getModel('catalog/product');
$_product = $product->loadByAttribute('sku',$data['prod_sku']);
$clone = $_product->duplicate();
$clone->setSku($data['new_sku']);
$clone->setUrlKey('foo-bar-1');
$qty = 99;
$is_in_stock = 1;
$stockArray = array(
'use_config_manage_stock' => 0,
'manage_stock' => 1,
'qty' => $qty,
'is_in_stock' => $is_in_stock,
);
$storeid=0; // your store id 0 is for default store id
Mage::getModel('catalog/product_status')->updateProductStatus($clone->getId(), $storeid, Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
$clone->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
try{
$clone->getResource()->save($clone);
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($clone->getId());
foreach($stockArray as $key => $val){
$stockItem->setData($key, $val);
}
$stockItem->save();
} catch(Exception $e){
Mage::log($e->getMessage());
}
echo "new product ID is ".$clone->getId();
}
这很有效,产品与提供的SKU和表格中的覆盖价格重复。
答案 0 :(得分:0)
最有可能的是,您无法在前端看到产品,因为它不适用于所需的网站。提供的代码只能在管理区域中正确执行(在前端控制器"警告:在第1180行和第34行的app / code / core / Mage / Eav / Model / Entity / Abstract.php中为foreach()提供的参数无效;将生成),因此代码:Mage::app()->getStore(true)->getWebsite()->getId()
返回0
,这对于前端来说可能不是正确的网站。
你应该替换line:
$clone->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
带
$clone->setWebsiteIds($_product->getWebsiteIds());