Magento图片上传问题

时间:2010-10-11 05:58:30

标签: magento

提前致谢。

我是Magento的新手,我创建了一个直接向Magento前面展示产品的模块。

在本单元中,我在客户帐户部分创建了一个表单,客户可以在该表单中填写产品所需的数据。产品数据已成功保存,但我无法在前面看到图像。我已经检查了客户部分上传产品的所有内容,并将其转到管理部分进行审批。

我只想展示我上传的产品图片。我的代码如下所示,用于产品保存和图片上传。

抱歉我的英文。

-Jeet

$magentoProductModel = Mage::getModel('catalog/product');
$magentoProductModel->setWebsiteIds(array(Mage::app()->getStore()->getId()));
$magentoProductModel->setAttributeSetId(4);//old
//$magentoProductModel->setAttributeSetId(1);
$magentoProductModel->setTypeId('simple');
$magentoProductModel->setName($data[name]);
$magentoProductModel->setDescription($data[description]);
$magentoProductModel->setPrice($data[price]);
$magentoProductModel->setShortDescription($data[short_description]);
$magentoProductModel->setWeight($data[weight]);
$magentoProductModel->setStatus(1);
$magentoProductModel->setCategoryIds(array(3));
$magentoProductModel->setTaxClassId('None');
//$magentoProductModel->setSku('rand-sku-' .rand(1,20000) );

/* upload product image*/
$customerProductId=34;
$mediDir = Mage::getBaseDir('media');
$imagesdir = $mediDir . '/customersproducts/' . $customerProductId . '/';

if(!file_exists($imagesdir)){ 
    return false;
}

foreach (new DirectoryIterator($imagesdir) as $fileInfo) {
    //print_r($fileInfo);

    if($fileInfo->isDot() || $fileInfo->isDir()) continue;

    if($fileInfo->isFile()) {
        //echo $fileInfo->getPathname();         
        $magentoProductModel->addImageToMediaGallery($fileInfo->getPathname(),array('image','small_image','thumbnail'), true, false, true);
    }
    //else {
        //echo "no";
    //}
}
print_r($magentoProductModel);

/*upload script end */


$saved = $magentoProductModel->save();
$lastId = $saved->getId();
$customerProduct = Mage::getModel('customerpartner/customerpartner_product')->load($lastId);
$customerProduct->setProductId($lastId);
$logged_in_user=Mage::getSingleton('customer/session')->getCustomer()->getId();
$customerProduct->setCustomerId($logged_in_user);
$customerProduct->setCustomerId();
$customerProduct->save();

1 个答案:

答案 0 :(得分:0)

谢谢那些访问过这个问题的人我已经解决了问题,我自己只有一个成像阵列需要

以下代码正常

$magentoProductModel = Mage::getModel('catalog/product');
$magentoProductModel->setWebsiteIds(array(Mage::app()->getStore()->getId()));
$magentoProductModel->setAttributeSetId(4);//old
//$magentoProductModel->setAttributeSetId(1);
$magentoProductModel->setTypeId('simple');
$magentoProductModel->setName($data[name]);
$magentoProductModel->setDescription($data[description]);
$magentoProductModel->setPrice($data[price]);
$magentoProductModel->setShortDescription($data[short_description]);
$magentoProductModel->setWeight($data[weight]);
$magentoProductModel->setStatus(1);
$magentoProductModel->setCategoryIds(array(3));
$magentoProductModel->setTaxClassId('None');
//$magentoProductModel->setSku('rand-sku-' .rand(1,20000) );

/* upload product image*/
$customerProductId=34;
$mediDir = Mage::getBaseDir('media');
$imagesdir = $mediDir . '/customersproducts/' . $customerProductId . '/';

if(!file_exists($imagesdir)){ 
  return false;
}

foreach (new DirectoryIterator($imagesdir) as $fileInfo) {

  //print_r($fileInfo);

  if($fileInfo->isDot() || $fileInfo->isDir()) continue;

  $visibility = array (
      'thumbnail',
      'small_image',
      'image'
      );

  if($fileInfo->isFile()) {
    $fileInfo->getPathname();         
    $magentoProductModel->addImageToMediaGallery($fileInfo->getPathname(), $visibility, false, false);
  }
  //else
  //{
  //echo "no";
  //}

}    

/*upload script end */


$saved = $magentoProductModel->save();
$lastId = $saved->getId();
$customerProduct = Mage::getModel('customerpartner/customerpartner_product')->load($lastId);
$customerProduct->setProductId($lastId);
$logged_in_user=Mage::getSingleton('customer/session')->getCustomer()->getId();
$customerProduct->setCustomerId($logged_in_user);
$customerProduct->setCustomerId();
$customerProduct->save();

} catch(Exception $e){
  echo $e;
  Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  $this->_redirectReferer();
}