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。
哪些文件,我應該寫在上面的代碼? Block
或Model
或Helper
或controllers
非常感謝。
可能我建議從艾倫風暴的文章? http://alanstorm.com/category/magento#magento_for_php_developers – benmarks