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;
}
}
?>
(1) first create a new module with a custom eav for your product content (this is just the InstallData.php
content):
<?php namespace <vendor>\<module_name>\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
/**
* EAV setup factory
*
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* Init
*
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
/* assign object to class global variable for use in other class methods */
}
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
/**
* Add attributes to the eav/attribute
*/
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'my_attribute',
[
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'my custom content',
'input' => 'textarea',
'class' => '',
'source' => '',
'global' => 1,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => 0,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'wysiwyg_enabled' => true, // if you want a wysiwyg editor
'unique' => false,
'apply_to' => 'simple,configurable,virtual,bundle,downloadable,grouped',
'group' => 'General'
]
);
$setup->endSetup();
}
}
(2) in the module extend catalog_product_view.xml
to insert your attribute:
[I added mine as a new product tab]
<referenceBlock name="product.info.details">
<block class="Magento\Catalog\Block\Product\View\Description" name="{SOME_NAME}" template="product/view/attribute.phtml" group="detailed_info">
<arguments>
<argument name="at_call" xsi:type="string">getMyAttribute</argument>
<argument name="at_code" xsi:type="string">my_attribute</argument>
<argument name="css_class" xsi:type="string">{some class if you like}</argument>
<argument name="at_label" xsi:type="string">none</argument>
<argument name="title" translate="true" xsi:type="string">My tab label</argument>
</arguments>
</block>
</referenceBlock>
(3) now you can go to your magento admin and insert custom content (even html) into the attribute content like for every other attribute
Best Answer
Use
after_body_start
ortop.container
instead ofcontent
as a reference.after_body_start
is a container placed immediately after the body tagtop.container
is a container within the header element, below the top menu.If you use CMS widget instances instead of layout XML, the containers are referenced by "Page Top" and "Page Header".