简体   繁体   中英

magento - custom block not recognized

On my startpage I want to add a new block. So I added it to the CMS like

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

There must be some error in the defined block code. because if I change the type from productview/productview to core/template . the template will get called.

This is my config file of the plugin (in [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>

This is the Block (in [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;
  }
}

EDIT1:

here is my template file:

<?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>

Any idea what I am doing wrong?

your config.xml fine. you have make below code in block file - /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;

            }

than you call the block function $this->getRecentProducts(); in productview/productview.phtml

and you remove model file. Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM