我想在Magento的购物车页面上显示产品的自定义图像。
配置.xml文件
<checkout>
<rewrite>
<cart_item_renderer>ProductCustomizer_ProductCustomizer_Block_Checkout_Cart_Item_Renderer</cart_item_renderer>
</rewrite>
</checkout>
我在块/结帐/购物车/项目/渲染器.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();
}
}
}
我已经尝试使用图像"URL","路径"和"数据(data:image/png;base64,iVBORw0KG....
)"进行$customize_image,但它不起作用。
所以这就是这个想法。您正在调用的 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)
。
我已经找到了解决方案。你已经覆盖了核心类,我在下面添加了类。
在自定义模块的帮助程序中创建帮助程序"自定义图像.php"
<?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;
}
}
在自定义模块的模型/产品目录中创建模型"图像.php"。
<?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();
}
}
}