2012-08-27 31 views
0

對不起,我是一個magento的新手。現在,下面的代碼可以在同一類別上獲得rand產品。當把代碼放在view.phtml如何將函數放入magento模塊中?

<!--for show other product--> 
<?php $categories = $_product->getCategoryIds(); ?> 
    <?php 
        $result = array(); 
        foreach($categories as $cat_id) { 
            $category = Mage::getModel('catalog/category'); 
            $category->load($cat_id); 
            $collection = $category->getProductCollection(); 
            foreach ($collection as $product) { 
                $result[] = $product->getId(); 
            } 
  
        } 
    ?> 
    <div class="box-others-also-like"> 
        <ul> 
        <?php 
        if(sizeof($result) >= 5) 
        { 
           $ourneed = array_rand($result,5); 
           foreach($ourneed as $cc) 
            { 
             $thisproduct= Mage::getModel('catalog/product')->load($result[$cc]); 
             ?> 
             <li> 
            <a href="<?php echo $thisproduct->getProductUrl(); ?>" title="<?php echo $thisproduct->getName(); ?>" ><img src="<?php echo $this->helper('catalog/image')->init($thisproduct, 'small_image')->resize(200) ?>" width="200" height="200" alt="<?php echo $thisproduct->getName(); ?>" /></a> 
            </li> 
            <?php } ?> 
        <?php 
        }else 
        { 
           foreach($result as $cc) 
            { 
             $thisproduct= Mage::getModel('catalog/product')->load($cc); 
             ?> 
  
                <li> 
                <a href="<?php echo $thisproduct->getProductUrl(); ?>" title="<?php echo $thisproduct->getName(); ?>" ><img src="<?php echo $this->helper('catalog/image')->init($thisproduct, 'small_image')->resize(200) ?>" width="200" height="200" alt="<?php echo $thisproduct->getName(); ?>" /></a> 
                </li> 
            <?php 
            } 
            } 
            ?> 
        </ul> 
    </div> 
    <!--for show other product--> 

現在,我想把這個功能放在一個模塊中,我該怎麼辦?咋辦我創建了module.the模塊名稱的骨架是蘭德。包名是Web

哪些文件,我應該寫在上面的代碼? BlockModelHelpercontrollers

非常感謝。

+0

可能我建議從艾倫風暴的文章? http://alanstorm.com/category/magento#magento_for_php_developers – benmarks

回答

0

在Magento,調用一個函數,我們應該定義在阻止該功能。這是更好的方法。我們可以使用$ this調用該函數。

$this->functionName() 

但是我們可以在helper中定義這個函數,因爲我們需要像下面那樣調用這個函數。

Mage::helper('yourmodule/yourclassfile')->prtHelloWorld(); 

調用模型函數將影響MVC模式。所以不要試試這個。