如何将函数放入magento中的模块中?
对不起,我是magento的新手。现在,下面的代码可以获得相同类别的兰德产品。将代码放入视图.phtml时如何将函数放入magento中的模块中?,magento,Magento,对不起,我是magento的新手。现在,下面的代码可以获得相同类别的兰德产品。将代码放入视图.phtml时 <!--for show other product--> <?php $categories = $_product->getCategoryIds(); ?> <?php $result = array(); foreach($categories as $cat_id) { $cate
<!--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-->
-
-
现在,我想把函数放在一个模块中,我该怎么做?假设我已经创建了模块的框架。模块名称为Rand。packagename是Web
<!--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-->
我应该在哪个文件中写入上述代码?块
或模型
或助手
或控制器
非常感谢。在Magento中,要调用函数,我们应该在块中定义该函数。这是更好的方法。我们可以使用$this调用该函数
$this->functionName()
但是我们也可以在helper中定义该函数,为此我们需要像下面那样调用该函数
Mage::helper('yourmodule/yourclassfile')->prtHelloWorld();
调用模型函数将影响MVC模式。所以不要尝试这个。我可以建议从艾伦·斯托姆的文章开始吗?