2017-01-02 36 views
2

我想在Magento的購物車頁面上顯示產品的自定義圖像。如何在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(); 
     } 
    } 
} 

我已經試過$添加以下代碼customize_image帶圖像「URL」,「路徑」和「數據(data:image/png;base64,iVBORw0KG....)」但它不工作。

回答

1

我找到了解決辦法。你有覆蓋核心類,我已經添加到類下面。

助手在自定義模塊

<?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; 
    } 
} 

創建幫手 「Customezerimage.php」 在型號/產品目錄中的自定義模塊創建模型 「Image.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(); 
     } 
    } 
} 
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)

+0

我正在開發的擴展顯示,所以我不能在'應用程序/設計/前端/ RWD修改自定義圖像路徑/缺省的/模板/結帳/車/項目/ default.phtml'。如果您有任何其他方式,請讓我知道。 –

相關問題