Reputation: 537
I want to display custom image of product on cart page in Magento.
Config.xml file
<checkout>
<rewrite>
<cart_item_renderer>ProductCustomizer_ProductCustomizer_Block_Checkout_Cart_Item_Renderer</cart_item_renderer>
</rewrite>
</checkout>
I have added below code in Block/Checkout/Cart/Item/Renderer.php file
<?php
class ProductCustomizer_ProductCustomizer_Block_Checkout_Cart_Item_Renderer extends Mage_Checkout_Block_Cart_Item_Renderer{
public function getProductThumbnail()
{
$item = $this->_item;
$customize_data = $item->getData('customize_data');
$customize_image = $item->getData('customize_image');
$results_data = $item->getOptionByCode("customizer_data")->getValue();
if(!is_null($results_data)){
$results = unserialize($results_data);
$path = Mage::getBaseDir()."/skin/";
$_product = $item->getProduct()->load();
$customize_image = $this->helper('catalog/image')->init($_product, 'thumbnail',$path.'adminhtml/default/default/images/logo.gif');
//$customize_image = $this->helper('catalog/image')->init($_product, 'thumbnail',$results['image']);
}
if (!empty($customize_image)) {
return $customize_image;
} else {
return parent::getProductThumbnail();
}
}
}
I have tried $customize_image with image "URL", "Path" and "Data (data:image/png;base64,iVBORw0KG....
)" but it is not working.
Upvotes: 2
Views: 2104
Reputation: 537
I have found solution. You have override Core class, I have added below class.
Create helper "Customezerimage.php" in Helper in custom module
<?php
class ProductCustomizer_ProductCustomizer_Helper_Customezerimage extends Mage_Catalog_Helper_Image {
public function setCustomeImage($path){
$this->_imageFile = $path;
$this->_setModel(Mage::getModel('productcustomizer/product_image'));
$this->_getModel()->setCustomeBaseFile($this->_imageFile);
return $this;
}
public function __toString() {
try {
$model = $this->_getModel();
$url = $model->getUrl();
} catch (Exception $e) {
$url = Mage::getDesign()->getSkinUrl($this->getPlaceholder());
}
return $url;
}
}
Create Model "Image.php" in Model/Product directory in custom module.
<?php
class ProductCustomizer_ProductCustomizer_Model_Product_Image extends Mage_Catalog_Model_Product_Image{
public function setCustomeBaseFile($baseFile){
if (!file_exists($baseFile)) {
throw new Exception(Mage::helper('catalog')->__('Image file was not found.'));
}
$this->_newFile = $this->_baseFile = $baseFile;
return $this;
}
public function saveCustomeImage($file = ""){
if(empty($file)){
$file = $this->_newFile;
}
Mage::log($file);
$this->getImageProcessor()->save($file);
return ;
}
public function getUrl(){
$baseDir = Mage::getBaseDir();
$path = str_replace($baseDir . DS, "", $this->_newFile);
return Mage::getBaseUrl() . str_replace(DS, '/', $path);
}
}
Now you can set custom image path that you want to display in cart like below
<?php
class ProductCustomizer_ProductCustomizer_Block_Checkout_Cart_Item_Renderer extends Mage_Checkout_Block_Cart_Item_Renderer{
public function getProductThumbnail()
{
$item = $this->_item;
$customize_data = $item->getData('customize_data');
$customize_image = $item->getData('customize_image');
$results_data = $item->getOptionByCode("customizer_data")->getValue();
if(!is_null($results_data)){
$results = unserialize($results_data);
$imagePathFull = $customize_image;
$_product = $item->getProduct();
$image = $imagePathFull;
return $this
->helper('productcustomizer/customezerimage')
->init($_product, 'thumbnail')->setCustomeImage($image);
}else{
return parent::getProductThumbnail();
}
}
}
Upvotes: 1
Reputation: 1720
So here is the idea. The init function you are calling is creating the issue for you. Do something like this as you have already done the override of that class.
public function getProductThumbnail1()
{
if (!is_null($this->_productThumbnail)) {
return $this->_productThumbnail;
}
return $this->getSkinUrl('images/jhonson.jpg');
//return $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail');
}
Add this function in that class you have already overridden which is Mage_Checkout_Block_Cart_Item_Renderer
. Now in your app/design/frontend/rwd/default/template/checkout/cart/item/default.phtml
file or whatever your file is call this function like this echo $this->getProductThumbnail1()
rather than this echo $this->getProductThumbnail()->resize(180)
.
Upvotes: 1