Magento - 将变量/参数传递给静态块


Magento - Pass a variable/parameter to Static Block

我正在开发Magento CE 1.6.2,并希望显示特定类别的"新"产品。为此,我创建了一个这样的静态块,并传递了我想要的类别。

 {{block type="catalog/product_new" category_id="20" template="catalog/product/new.phtml"}} 

但是类别 Id 需要从我的代码在其中一个 .phtml 文件中动态传递。有没有办法创建一个我可以在这个静态块中使用的变量。就像我的.phtml代码是:

 $_categoryId = $this->getCurrentCategory()->getId(); //Store my category id

可以做一些事情,以便我可以像这样将这个变量传递给我的静态块,

 {{block type="catalog/product_new" category_id="var $_categoryId" template="catalog/product/new.phtml"}} //Which is nothing but 20 so that it displays new products from only category 20

请帮忙!

编辑:我找到了一个做类似事情的链接。我跟着它,但没有成功。http://magentophp.blogspot.co.uk/2011/08/passing-paramters-to-magento-cms-static.html

任何人都可以帮我点击链接并使其工作吗?

它可能不完全是你所要求的,但可能会为你指明一个正确的方向:

在 phtml 文件中,您可以这样做:

$category = "23";
$this->getChild('home.catalog.product.new')->setData('category_id', $category);
echo $this->getChildHtml('home.catalog.product.new', false);

注意最后一个"假"是告诉它不要缓存块。

您还需要将其包含在布局文件中,类似于以下内容:

<block type="catalog/product_new" name="home.catalog.product.new" alias="product_new" template="catalog/product/new.phtml"/>
您需要

     $this->getChild('testpage')->setData("test", "xyz")
     echo $this->getChildHtml('testpage');

然后,您可以通过以下方式呼叫/访问它

    $this->test; 

在 CMS 页面中使用,我们可以将变量放入一个块中,例如:-

{{block type="yourmodule/testblock" product_id=10 template="module/yourmodule/bestproduct.phtml"}}

或与订单对象相同:-

{{block type="yourmodule/testblock" order=$order template="module/yourmodule/bestproduct.phtml"}}