我想在Magento的购物车页面上显示产品的自定义图像。
Config.xml 文件
<checkout>
<rewrite>
<cart_item_renderer>ProductCustomizer_ProductCustomizer_Block_Checkout_Cart_Item_Renderer</cart_item_renderer>
</rewrite>
</checkout>
我在阻止/结帐/购物车/物品/ Renderer.php 文件中添加了以下代码
<?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();
}
}
}
我尝试使用图片“网址”,“路径”和“数据(data:image/png;base64,iVBORw0KG....
)” $ customize_image 但是它不起作用。
答案 0 :(得分:1)
所以这就是主意。您正在调用的init函数正在为您创建问题。做这样的事情,因为你已经完成了该类的覆盖。
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');
}
在已覆盖的类Mage_Checkout_Block_Cart_Item_Renderer
中添加此函数。现在,您的app/design/frontend/rwd/default/template/checkout/cart/item/default.phtml
文件或文件中的任何内容都会调用此函数,而不是此echo $this->getProductThumbnail1()
,而不是echo $this->getProductThumbnail()->resize(180)
。
答案 1 :(得分:1)
我找到了解决方案。你已经覆盖了Core类,我在下面添加了类。
创建助手&#34; Customezerimage.php &#34;在自定义模块中的帮助程序
<?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;
}
}
创建模型&#34; Image.php &#34;在自定义模块中的模型/产品目录中。
<?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);
}
}
现在,您可以设置要在购物车中显示的自定义图像路径,如下所示
<?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();
}
}
}