2012-09-07 27 views
0

所以我一直在研究一個Magento模塊,當用戶沒有登錄時隱藏價格。我得到它的工作感謝@AlanStorm,但我只是想確保它我正在爲最好的方法而努力。最好在模板中調用模板Magento

我所做的是設定不同的模板爲* catalog_product_price_template *塊,並從那裏我做了所有的邏輯

<?php $_message = Mage::getStoreConfig('catalog/pricehideconfig/title'); 
     $_enabled = Mage::getStoreConfig('catalog/pricehideconfig/active'); 
     $_current_template = Mage::getBaseDir('design') 
         . '/frontend/' 
         . Mage::getSingleton('core/design_package')->getPackageName() . '/' 
         . Mage::getSingleton('core/design_package')->getTheme('frontend') .'/' 
         . 'template/catalog/product/price.phtml'; 

     $_default_template = Mage::getBaseDir('design') . '/frontend/base/default/template/catalog/product/price.phtml'; 
?> 

<p> 
    <?php if ( $_enabled && !($this->helper('customer')->isLoggedIn())) { ?> 
     <?php echo $_message; ?> 
    <?php } else { 

     if (file_exists($_current_template)){ 
      include $_current_template; 
     } else{ 
      include $_default_template; 
     } 

    } ?> 

</p> 

然而,兩個部分似乎真的不自然

  1. 調用「原始'或默認模板代碼的價格感覺不對,Magento是否提供任何功能來做到這一點,在模板中調用默認模板,同時檢查模板是否存在於當前包中,然後恢復爲默認模板,如果沒有?

  2. 我認爲模板應該只用於演示文稿,所以變量賦值應該移動到一個塊,但我不能這樣做,因爲我只是設置模板,而不是擴展* Mage_Catalog_Block_Product_Price_Template *

+0

首先,這邏輯屬於塊類定義,而不是一個模板。 – benmarks

回答

2

我真的不明白上面的代碼!

如果你想隱藏的非登錄的客戶我已經使用最簡單,最好的辦法價格

是:

模塊將只有一個街區,config.xml文件

擴展 - 重寫類Mage_Catalog_Block_Product_Price

class Namespame_ModuleName_Block_Product_Price extends Mage_Catalog_Block_Product_Price 
{ 
    // and Override _toHtml Function to be 
    protected function _toHtml() 
    { 
     if(!$this->helper('customer')->isLoggedIn()){ 
      $this->getProduct()->setCanShowPrice(false); 
     } 

     if (!$this->getProduct() || $this->getProduct()->getCanShowPrice() === false) { 
      return ''; 
     } 
     return parent::_toHtml(); 
    } 
} 

這工作完全不增加更多的代碼來查看/模板/佈局!

如果你仍然想設置的模板,你可以做到這一點也爲:

class Namespame_ModuleName_Block_Product_Price extends Mage_Catalog_Block_Product_Price 
{ 
    // and Override _toHtml Function to be 
    protected function _toHtml() 
    { 
     if(!$this->helper('customer')->isLoggedIn()){ 
       $this->setTemplate('mymodule/price_template.phtml'); 
     } 

     if (!$this->getProduct() || $this->getProduct()->getCanShowPrice() === false) { 
      return ''; 
     } 
     return parent::_toHtml(); 
    } 
} 

感謝

+0

更簡單,更清潔,謝謝,很高興看到你積極上stackoverflow –

+0

感謝兄弟:)很高興見到你身邊也!很長的時間:) – Meabed

+0

你能指定如何擴展/重寫模塊 –