2012-12-21 39 views
15

我需要幫助,幫助我熟悉幫助者,他們的方法和產品屬性。具體做法是:$_helper->productAttribute($product, $attributeHtml, $attributeName)需要幫助瞭解產品頁面的productAttribute()方法

下面是我使用/審查文件:

app\code\core\Mage\catalog\helper\output.php 
app\design\frontend\[theme name]\template\catalog\product\view\media.phtml 

下面的代碼行生成的產品圖像的HTML。

echo $_helper->productAttribute($_product, $_img, 'image'); 

幫助程序類代碼描述了以下幾段代碼中的方法。什麼是返回,什麼是步驟,以及爲什麼我會使用此方法而不是簡單地回顯模板文件的上一行中描述的img html?

public function getHandlers($method) 
{ 
    $method = strtolower($method); 
    return isset($this->_handlers[$method]) ? $this->_handlers[$method] : array(); 
} 

public function process($method, $result, $params) 
{ 
    foreach ($this->getHandlers($method) as $handler) { 
     if (method_exists($handler, $method)) { 
      $result = $handler->$method($this, $result, $params); 
     } 
    } 
    return $result; 
} 

public function productAttribute($product, $attributeHtml, $attributeName) 
{ 
    /* Code that is not relevant to this example */ 

    $attributeHtml = $this->process('productAttribute', $attributeHtml, array(
     'product' => $product, 
     'attribute' => $attributeName 
    )); 

    return $attributeHtml; 
} 

任何幫助表示讚賞。

+0

它可能添加處理程序,並且正在執行這些處理程序。你不能調試,看看還有更多的事情發生嗎?它可能是一個抽象的方式,可能會更有用的其他元素:) – EricG

+0

優秀的建議。我仍然在想出一個有效的方法來調試magento,並接受建議。 至於抽象的方式,這是有道理的。對於發生的事情的步驟也將不勝感激。 :) – MSD

+0

谷歌瀏覽器有一個很好的調試器。打開檢查器(Ctrl-Shift-J)並轉到源。您可以使用Ctrl-Shift-F在其中的所有源中搜索。或者手動打開src,並放置一個斷點。然後通過它:) – EricG

回答

27

非常好的問題!

所以實際上有一點關於這個幫手的目的。從它的名字你可以得出結論,它用於輸出數據。方法名稱也是自解釋的,它只是輸出產品屬性值取決於處理程序。目前有兩種方法,productAttribute(),用於輸出產品屬性值和categoryAttribute(),用於類別1。從類別和產品的核心模板中的所有數據通過此方法輸出(價格屬性除外),據我所知,它是在1.4.x版本中添加的,但不確定。主要想法是可以過濾屬性的數據。例如,您可以在類別描述中使用{{widget ... }}結構,它是通過特殊方法實現的。

這兩種方法實際上都具有相同的功能,但對於不同的實體。它們兩者接收3個參數的:

  • 實體(類或產品,取決於方法名)
  • 屬性值 - 用於檢索屬性模型
  • 代碼 - 被過濾
  • 屬性代碼值

首先在這個方法裏面,Magento檢查值中的html標記的寬度,如果沒有,它會用escapeHtml()方法轉義文本。另外,如果屬性在管理員中有一個textarea作爲輸入,則所有換行符都將替換爲<br />標記。

如果允許html,Magento會在配置中檢查特殊結構的配額(如{{widget ...}})(此結構的正式名稱是指令)。如果指令被允許,特殊指令處理器被實例化並且值得到處理。

所有核心處理完成後,Magento調用處理程序。

該處理程序是核心模塊不使用的附加功能,但您可以使用自己的自定義來實現一些很好的自定義。這裏是一個例子:你想以大寫形式輸出產品名稱的所有輸出。然後,你可以添加自己的處理程序,爲此,按照這個簡單的步驟:

  1. 定義一個觀察員catalog_helper_output_construct

    <config> 
        <frontend> 
         <events> 
          <catalog_helper_output_construct> 
           <observers> 
            <your_module> 
             <class>your_module/observer</class> 
             <method>handleHelperOutputInitialization</method> 
            </your_module> 
           </observers> 
          </catalog_helper_output_construct> 
         </events> 
        </frontend> 
    </config> 
    
  2. 創建您的觀察器類,我也將它作爲處理程序,以及。該代碼非常簡單:

    class Your_Module_Model_Observer 
    { 
        public function handleHelperOutputInitialization($observer) 
        { 
         $helper = $observer->getEvent()->getHelper(); 
         $helper->addHandler('productAttribute', $this); 
        } 
    
        public function productAttribute($helper, $value, $parameters) 
        { 
         $attribute = $parameters['attribute']; 
         if ($attribute->getAttributeCode() == 'name') { 
          return strtoupper($value); 
         } 
         return $value; 
        } 
    } 
    
  3. 確保在處理類的方法名是絕對相同的值處理器的方法的名稱,在本例中爲productAttribute()

享受學習Magento!

+0

非常感謝您的全面回覆。絕對學到了一噸。我希望我能對此讚不絕口。 – MSD

+2

@MSD你總是可以接受一個答案,如果你也不能贊成它。考慮接受答案,如果它幫助你解決問題。 – Kalpesh

+1

自從magento 2.0.0.0-dev47(該事件已被刪除)以來,此解決方案已過時。現在應該使用一個插件。 – Quisse