【问题标题】:magento - custom block not recognizedmagento - 无法识别自定义块
【发布时间】:2016-01-30 18:06:36
【问题描述】:

在我的起始页上,我想添加一个新块。所以我将它添加到 CMS 中,例如

<div>{{block type="productview/productview" name="productview_productview" template="productview/productview.phtml"}}</div>

定义的块代码中一定有错误。因为如果我将类型从productview/productview 更改为core/template。模板将被调用。

这是我的插件配置文件([magento]/app/code/local/AAA/Productview/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <AAA_Productview>
      <version>1.0</version>
    </AAA_Productview>
  </modules>
  <global>
    <blocks>
      <productview>
        <class>AAA_Productview_Block</class>
      </productview>
    </blocks>
  </global>
</config>

这是区块([magento]/app/code/local/AAA/Productview/Block/Productview.php

<?php
class AAA_Productview_Block_Productview extends Mage_Core_Block_Template {
  public function getRecentProducts() {
    Mage::log('test');
    $arr_products = array();
    $products = Mage::getModel("catalog/product")
                -­>getCollection()
                ­->addAttributeToSelect('*')
                ­->setOrder('entity_id', 'DESC')
                ­->setPageSize(5);

    foreach ($products as $product) {
      $arr_products[] = array(
        'id' => $product-­>getId(),
        'name' => $product­->getName(),
        'url' => $product­->getProductUrl(),
      );
    }

    return $arr_products;
  }
}

编辑1:

这是我的模板文件:

<?php
$products = $this­->getRecentProducts();
?>

<div id="product_list">
  <h1>Recent Products</h1>
  <?php if (is_array($products) && count($products)) { ?>
    <?php foreach($products as $product) { ?>
      <div>
        <a href="<?php echo $product['url'] ?>"><?php echo $product['name'] ?></a>
      </div>
    <?php } ?>
  <?php } ?>
</div>

知道我做错了什么吗?

【问题讨论】:

    标签: php magento magento-1.8


    【解决方案1】:

    你的 config.xml 很好。您在块文件中制作了以下代码 - /app/code/local/AAA/Productview/Block/Productview.php

        public function getRecentProducts() {
                $products = Mage::getModel("catalog/product")
                        -­>getCollection()
                        ­->addAttributeToSelect('*')
                        ­->setOrder('entity_id', 'DESC')
                        ­->setPageSize(5);
                return $products;
    
                }
    

    比你调用块函数 $this->getRecentProducts();在productview/productview.phtml

    然后您删除模型文件。 谢谢

    【讨论】:

    • 我删除了模型文件并将内容添加到块文件中。但该函数永远不会被调用。
    • 您在块文件中添加了以下代码: getCollection() ->addAttributeToSelect('*') ->setOrder('entity_id', 'DESC') ->setPageSize(5);返回$产品;我希望这段代码对你有帮助。谢谢
    • 我添加了那个代码,但我在模板文件中得到了Undefined variable: this
    • 你能给我所有截图错误的代码吗?
    猜你喜欢
    • 2012-08-04
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多