Magento以编程方式为多商店视图设置产品图像

时间:2012-12-10 11:51:45

标签: image magento product

我正在使用Helper功能动态创建产品。这创造了很好的。并设置图像工作正常的默认商店视图。但对于多线圈视图,图像未选择为“缩略图”,“small_image”或“图像”。

       $product = new Mage_Catalog_Model_Product();

        // Build the product
        //$product->setSku('pkg-sku-1');
        $product->setAttributeSetId(4); //default attribute-set
        $product->setTypeId('simple');
        $product->setName($pkg_data['pkg_name']);
        $product->setCategoryIds(array(7)); # some cat id's, my is 7

        $product->setWebsiteIDs(array(1)); # Website id, my is 1 (default frontend)
        $product->setDescription('EventPackageForOrder');
        $product->setShortDescription($pkg_id);
        $product->setPrice($pkg_data['pkg_price']); # Set some price


        //Default Magento attribute
        $product->setWeight(4.0000);
        $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
        $product->setStatus(1);
        $product->setTaxClassId(2); # Taxable goods
        $product->setStockData(array(
            'is_in_stock' => 1,
            'qty' => 5
        ));

        $im_url_path = Mage::getBaseUrl('media').'eventdiscpkgs/event_logo.jpeg'; 
        $im_absolute_path = Mage::getBaseDir('media') . DS . 'eventdiscpkgs/event_logo.jpeg';

        $product->setMediaGallery (array('images'=>array (), 'values'=>array ()));
        $product->addImageToMediaGallery($im_absolute_path, array ('thumbnail'), false, false);
        $product->addImageToMediaGallery($im_absolute_path, array ('small_image'), false, false);
        $product->addImageToMediaGallery($im_absolute_path, array ('image'), false, false);



        $product->setHasOptions(true);








        $product->setCreatedAt(strtotime('now'));
        try {

            $product->save();
        }
        catch (Exception $ex) {

            $ex->getMessage();
        }

任何我可以改变的想法。

1 个答案:

答案 0 :(得分:1)

尝试为每个商店ID分配图片:

foreach($product->getStoreIds() as $storeId) {
    $product->setStoreId($storeId)
        ->setImage($im_absolute_path)
        ->setSmallImage($im_absolute_path)
        ->setThumbnail($im_absolute_path);
    $product->save()
}