简体   繁体   中英

Magento set product image for multistore view programatically

There i am creating a product dynamically from a Helper function. this creates fine. and set image work fine for default store view. but for multistore view, image not selected as 'thumbnail', 'small_image' or '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();
        }

ANY Idea where i can change.

Try to assign image for each store id:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM