1

I have found a script that adds a 'sample request' button that should allow me to add a sample of every product, I'm getting an error when I click the sample request button.

This is the error I get:

Fatal error: Uncaught exception 'Mage_Core_Exception' with message 'Image does not exist.' in /var/www/vhosts/webcontrolcentre.co.uk/httpdocs/perfume/app/Mage.php:595 Stack trace: #0 /var/www/vhosts/webcontrolcentre.co.uk/httpdocs/perfume/app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php(274): Mage::throwException('Image does not ...') #1 /var/www/vhosts/webcontrolcentre.co.uk/httpdocs/perfume/app/code/core/Mage/Catalog/Model/Product.php(1047): Mage_Catalog_Model_Product_Attribute_Backend_Media->addImage(Object(Mage_Catalog_Model_Product), '/var/www/vhosts...', Array, false, false) #2 /var/www/vhosts/webcontrolcentre.co.uk/httpdocs/perfume/getSample.php(82): Mage_Catalog_Model_Product->addImageToMediaGallery('/var/www/vhosts...', Array, false, false) #3 {main} thrown in /var/www/vhosts/webcontrolcentre.co.uk/httpdocs/perfume/app/Mage.php on line 595

The script that allows the sample to be created is in a file called getSample.php and it includes:

<?php

 ini_set('display_errors', 1);
 error_reporting(E_ALL);

 //print_r($_POST);

 require_once 'app/Mage.php';
 Mage::app();



 Mage::getSingleton("core/session", array("name" => "frontend"));
 Mage::getSingleton('core/session')->setSampleError("");
 // Store users session
 $session = Mage::getSingleton("customer/session");

 //Store cart items
 $totalNumOfCartItem = Mage::getModel('checkout/cart')->getItemsQty();

 //Store cart items
 $theitems = Mage::getModel('checkout/cart')->getItems();

 $thecount = 0;

 foreach($theitems as $ti){

 $thename = $ti['name'];

 $test = strpos($thename,'Free Sample');

 if($test !== false){
 $thecount = $thecount+1;
 }

 }

 if(isset($_POST['name'])){
 $name = $_POST['name'];

 $id = $_POST['id'];
 if(isset($_POST['simpleid']) && !empty($_POST['simpleid'])){
 $simpleid = $_POST['simpleid'];
 }
 }
 if(isset($_GET['name'])){
 $name = $_GET['name'];
 if(isset($_GET['simpleid']) && !empty($_GET['simpleid'])){
 $simpleid = $_GET['simpleid'];
 }
 $id = $_GET['id'];
 }

 // retrict shopping cart to only allow 5 samples
 if ($totalNumOfCartItem <= 4)
 {
 $theproduct = Mage::getModel('catalog/product')->load($id);

 if($simpleid){
 $thesimple = Mage::getModel('catalog/product')->load($simpleid);
 }

 if($thecount < 2)
 {
 // instatiate Product
 $product = Mage::getModel('catalog/product');
 if($simpleid){
 $product->setSku($thesimple->getSku()."*".rand());
 } else {
 $product->setSku($theproduct->getSku()."*".rand());
 }
 $product->setName("Sample of ".$name);
 // $product->setDescription("Sample of ".$name);
 $product->setShortDescription($name." – Sample");
 $product->setPrice(0.00);
 $product->setTypeId('simple');

 $product->setImageUrl($theproduct->getImageUrl()); // set the images from the original product
 $product->setThumbnailUrl($theproduct->getImageUrl());
 $product->setImage($theproduct->getSmallImage());
 $product->setMediaGallery (array('images'=>array (), 'values'=>array ()));
 $product->addImageToMediaGallery($_SERVER['DOCUMENT_ROOT']."/media/catalog/product".$theproduct->getSmallImage(), array('image','small_image','thumbnail'), false, false);
 $product->setAttributeSetId(9); // need to look this up
 $product->setCategoryIds("15"); // need to look these up
 $product->setWeight(21212121.21);
 $product->setTaxClassId(2); // taxable goods
 $product->setVisibility(1); // catalog, search
 $product->setStatus(1); // enabled
 // assign product to the default website
 $product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));


 // get product’s general info such price, status, description

 $stockData = $product->getStockData();

 // update stock data using new data
 $stockData['qty'] = 1;
 $stockData['is_in_stock'] = 1;
 $stockData['manage_stock'] = 1;
 $stockData['max_sale_qty'] = 5;

 // then set product’s stock data to update
 $product->setStockData($stockData);
 //die();

 // call save() method to save your product with updated data
 $product->save();


 header("Location: /checkout/cart/add/product/".$product->getId()."/");
 }
 }

 else
 {
 Mage::getSingleton('checkout/session')->addError("You are only allowed a maximum of 5 sample products");

 header("Location: /checkout/cart");
 }
 ?>

I'm hoping someone can help as I really would like an 'add sample' button but cannot find a solution anywhere so I'm hoping I can get this script to work.

Thankyou.

0

You would better use Mage::getBaseDir('media') instead of $_SERVER['DOCUMENT_ROOT']."/media in your addImage function. If things remain the same, try check what is the path to the image and if the image exists there.

  • Thanks for the reply, this maybe a stupid question but how would I use the Mage::getbaseDir('media') in this line of code?$product->addImageToMediaGallery($_SERVER['DOCUMENT_ROOT']."/media/catalog/product".$theproduct->getSmallImage(), array('image','small_image','thumbnail'), false, false); I've tried a couple of ways but still getting the error and the image does exist in that file directory. – Duggy G Oct 13 '14 at 18:11
  • you replace $_SERVER['DOCUMENT_ROOT']."/media by Mage::getBaseDir('media')." – Christophe Ferreboeuf Oct 13 '14 at 18:23
  • I have done that and now I get a different error which is a Uncaught exception 'PDOException, so I think your solution has fixed the first error, thankyou for your help. – Duggy G Oct 13 '14 at 18:34
  • can you tick as resolved ? – Christophe Ferreboeuf Oct 13 '14 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.