2

I've created a custom block, however when calling the block from cms page displays nothing. Even the content of cms page does not displat that is above the call to custom block

Here is How i'm calling the block

{{block type="brandproducts/brandproducts"  template="brandproducts/brandproducts.phtml"}}

Custom Block Code is As Follows enter image description here

Block File

<?php
// app/code/local/Compx/Brandproducts/Block/Recentproducts.php
class Compx_Brandproducts_Block_Brandproducts extends Mage_Core_Block_Template {
  public function getBrandProducts() {
    // call model to fetch data
    $arr_products = array();
    $products = Mage::getModel("brandproducts/brandproducts")->getBrandProducts();

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

    return $arr_products;
  }
}
?>

Model File

<?php
// app/code/local/Compx/Brandproducts/Model/Brandproducts.php
class Compx_Brandproducts_Model_Brandproducts extends Mage_Core_Model_Abstract {
  public function getBrandProducts() {
  $products = Mage::getModel("catalog/product")->getCollection();
                $products->addAttributeToSelect('*')
                //->setOrder('entity_id', 'DESC')
                ->setPageSize(5);
    return $products;

  }
}

?>

Config file

<!-- app/code/local/Compx/Brandproducts/etc/config.xml -->
<?xml version="1.0"?>
<config>
  <modules>
    <Compx_Brandproducts>
      <version>1.0</version>
    </Compx_Brandproducts>
  </modules>
  <global>
    <blocks>
      <brandproducts>
        <class>Compx_Brandproducts_Block</class>
      </brandproducts>
    </blocks>
    <models>
      <brandproducts>
        <class>Compx_Brandproducts_Model</class>
      </brandproducts>
    </models>
  </global>
</config>

This just outputs blank. If i remove the custom block placeholder from cms page, other static content loads fine

I've been calling direct template files from other pages and that just works fine, like {block type="core/template" .. } however not works at all with custom block

It just display nothing, not even print_r or echo strings

Template File

  <?php
// app/design/frontend/compx_package/compx/template/brandproducts/brandproducts.phtml


print_r("hi 123");

//exit;
$products = $this->getBrandProducts();
?>

<div id="product_list">
  <h1>Brand 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>

xml file under app/etc directory

/app/etc/modules/Compx_Brandproducts.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Compx_Brandproducts>
            <active>true</active>
            <codePool>local</codePool>
        </Compx_Brandproducts>
    </modules>
</config>
0

2 Answers 2

2

Your block should extend from product list I believe.

<?php
// app/code/local/Compx/Brandproducts/Block/Recentproducts.php
class Compx_Brandproducts_Block_Brandproducts 
  extends Mage_Catalog_Block_Product_List{ 
  //extends Mage_Core_Block_Template {

  public function getBrandProducts() {
    // call model to fetch data
    $arr_products = array();
    $products = Mage::getModel("brandproducts/brandproducts")->getBrandProducts();

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

    return $arr_products;
  }
}
?>

And your model file

<?php
// app/code/local/Compx/Brandproducts/Model/Brandproducts.php
class Compx_Brandproducts_Model_Brandproducts 
extends Mage_Catalog_Model_Abstract{
//extends Mage_Core_Model_Abstract {
  public function getBrandProducts() {
  $products = Mage::getModel("catalog/product")->getCollection();
                $products->addAttributeToSelect('*')
                //->setOrder('entity_id', 'DESC')
                ->setPageSize(5);
    return $products;

  }
}

?>
9
  • Updated, doesn't work either Not even the template file when i remove everything and just put print_r('123') even that is not being displayed.
    – echoashu
    Commented Jan 25, 2016 at 3:57
  • Is your module loaded? you can check in "system > config > advance", does your module present in the list? Commented Jan 25, 2016 at 3:59
  • The template file get called if i place that inside template/catalog/ directory but not sure if i can call block methods from that directory also on first place the template files should atleast get called ryt ? I've followed code.tutsplus.com/tutorials/…
    – echoashu
    Commented Jan 25, 2016 at 3:59
  • allryt, nice spotted Module is not loaded, but config.xml is correct
    – echoashu
    Commented Jan 25, 2016 at 4:01
  • can you add your app/etc/modules/your_module.xml part as well? Commented Jan 25, 2016 at 4:08
4

Two things you really need to check here.

  1. You need to whitelist your custom block brandproducts/brandproducts. See this answer for more information. This is required for Magento 1.9 and greater.

  2. Make sure your phtml file is in the correct package theme directory. To make sure you can put your phtml file here : app/design/frontend/base/default/template/brandproducts/brandproducts.phtml

Dont forget to remove all cache after you have done all changes. Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.