我正在尝试编写一个自定义模块,该模块能够将产品复制到只有不同SKU的多个产品中。我尝试在自定义模块中的/app/code/core/Mage/Catalog/Model/Product.php下使用function duplicate()
。但它不起作用。
我在我的自定义Obesrever.php文件中使用以下代码进行复制,但没有发生重复
$product = $observer->getEvent()->getProduct();
$newProduct = $product->duplicate();
任何人都可以建议我这样做的任何链接或任何代码格式都会有所帮助。
由于
答案 0 :(得分:0)
如果您可以发布您尝试调试的完整函数或创建重复产品和config.xml(您尝试调用该事件的位置),那将是很棒的。
以下代码适用于CE 1.9.2.2,没有任何问题。此功能执行以下任务:
- 创建原始产品的副本
- 将库存设置为“有库存”,将数量设置为“100”(现在硬编码)
- 自动重建索引
醇>
public function indexAction() //change the function name
{
$productId = $observer->getEvent()->getProduct()->getId();
$productObject = Mage::getModel('catalog/product');
$_product = $productObject->load($productId);
$newProduct = $_product->duplicate();
//new product status is disabled - to view in the frontend you will need to set the status as enabled
$newProduct->setStatus(1);
$newProduct->setName('Duplicate-' . $_product->getName());
$newProduct->setSku('value-' . $productId);
$newProduct->setWebsiteIds($_product->getWebsiteIds());
//set the product stock and qty to display product in the frontend
//while creating duplicate product, it will update the new product to have qty 0 and out of stock
$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);
//automate reindexing - to display the product in the frontend
$indexers = Mage::getSingleton('index/indexer')->getProcessesCollection();
foreach ($indexers as $indexer)
{
$indexer->reindexEverything();
}
}
希望这有帮助。
快乐编码......