简体   繁体   English

将产品图库图像设置为基本,缩略图和小图像Magento

[英]Set product gallery images to base , thumbnail and small image Magento

Hi i am working on the site in which i need to add the products from the frontend , for that i am using the following code 嗨,我在需要从前端添加产品的网站上工作,为此,我正在使用以下代码

<?php //$product = Mage::getModel('catalog/product');
$product = new Mage_Catalog_Model_Product();
echo time();
// Build the product
$product->setAttributeSetId(4);// #4 is for default
$product->setTypeId('simple');

$product->setName('Some cool product name');
$product->setDescription('Full description here');
$product->setShortDescription('Short description here');
$product->setSku(2);
$product->setWeight(4.0000);
$product->setStatus(1);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);//4
print_r(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);

$product->setPrice(39.99);// # Set some price
$product->setTaxClassId(0);// # default tax class

$product->setStockData(array(
'is_in_stock' => 1,
'qty' => 99999
));

$product->setCategoryIds(array(3));// # some cat id's,

$product->setWebsiteIDs(array(1));// # Website id, 1 is default

//Default Magento attribute

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



// Add three image sizes to media gallery
$mediaArray = array(
    'thumbnail'   => "me.jpg",
    'small_image' => "me.jpg",
    'image'       => "me.jpg",
);

// Remove unset images, add image to gallery if exists
$importDir = Mage::getBaseDir('media') . DS . 'import/';

foreach($mediaArray as $imageType => $fileName) {
    $filePath = $importDir.$fileName;
    if ( file_exists($filePath) ) {
        try {
            $product->addImageToMediaGallery($filePath, $imageType, false);

        } catch (Exception $e) {
            echo $e->getMessage();
        }
    } else {
        echo "Product does not have an image or the path is incorrect. Path was: {$filePath}<br/>";
    }
}



//print_r($product);
try {
    $product->save();

    echo "Product Created";
}
catch (Exception $ex) {
    //Handle the error
    echo "Product Creation Failed";
}

?>

this is working fine , but there is one problem it doesn't set the image to base default and small image. 这工作正常,但是存在一个问题,它没有将图像设置为默认的基本图像和较小的图像。

It Added the images for the product but the checkbox in front of image 它添加了产品的图像,但图像前面的复选框
is not selected . 未选择。 I need to check the boxes .Please suggest where i am doing mistake. 我需要选中复选框。请提出我在哪里做错了。

thanks 谢谢

Add

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 法师::应用程序() - > setCurrentStore(Mage_Core_Model_App :: ADMIN_STORE_ID);

before 之前

foreach($mediaArray as $imageType => $fileName) { foreach($ mediaArray as $ imageType => $ fileName){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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