2014-01-30 65 views
2

你好我工作的網站上,我需要添加的產品從前端,對於我使用下面的代碼集產品畫廊圖片基地,縮略圖和小圖像的Magento

<?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"; 
} 

?> 

這工作正常,但有一個問題 它不會將圖像設置爲基本默認和小圖像。

它添加了產品的圖像,但圖像前面的複選框
沒有被選中。我需要檢查框。請建議我在哪裏做錯了。

感謝

+0

請嘗試鏈接。 [http://stackoverflow.com/questions/21455166](http://stackoverflow.com/questions/4349592/make-all-store-images-the-base-small-and-thumbnail-images-in- Magento的) – raushan

回答

1

添加

法師::應用程序() - > setCurrentStore(Mage_Core_Model_App :: ADMIN_STORE_ID);

的foreach($ mediaArray爲$ IMAGETYPE => $文件名){