tags:

views:

41

answers:

2

Hi ,

Thanks in advance.

I am new to Magento, and I have created a module that directly shows products to the front of Magento.

In this module, I have created a form in customer account section where customers can fill out necessary data for product. Product data is saved successfully but I can't see the image on front. I have checked all the things the uploaded product from customer section also go to admin section for approval.

I just want to show image of product which I upload. My code is as follows for product save and image upload.

Sorry for my English.

-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();
A: 

Thanks guys who visited this question i have solved the problem my self there is only a array of imaged required in the

following code works fine

$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();
}
Jitendra
A: 

I want to add image upload on review form. Can you please help me?

Thanks in advance

Jai