我对可配置产品有类似的问题。我希望将分配给主要产品的图像也添加到所有选项中。所以我创建了一个扩展并观察了catalog_product_save_after事件。然后我在观察者中添加了这种代码:
$product = $observer->getEvent()->getProduct();
if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)
{
$main_image = $product->getImage();
if($main_image != "no_selection")
{
$productTypeIns = $product->getTypeInstance(true);
$childIds = $productTypeIns->getChildrenIds($product->getId());
$importDir = Mage::getBaseDir('media') . DS . 'catalog/product';
foreach ($childIds as $childId)
{
foreach($childId as $_childId)
{
$childProduct = Mage::getModel('catalog/product')->load($_childId); //You get your child products here
if ($childProduct->getImage()=="no_selection")
{
$childProduct->addImageToMediaGallery($importDir.$main_image,array ('image','small_image','thumbnail'),false,false);
$childProduct->save();
}
}
}
}
}
谢谢。唯一的问题是我不擅长Magento,有什么机会可以告诉我我可以把它放在哪里,在哪个文件中? – denislexic 2012-07-09 17:39:14
您不需要将此代码添加到任何现有文件。您需要创建自己的扩展并注册一个观察者。 – 2012-07-10 10:42:26
我该怎么做。对不起,我真的很不好。 – denislexic 2012-07-20 12:25:46