0

I want to display in the related products items from the same brand in order from the latest added. I was looking for an extension that can do it and didn't find it. So I figured out that the solution for me would be to edit an already existing extension. I found this 'iWeb Auto Related Products' and installed it. It automatically creates 5 random products from the same category. I'm not good with php so here's the main question do I just need to change all 'category' to 'brand' in the code below or what changes should I do?

public function getRelatedProducts($limit = false) {
    $products = $this->getData('related_products');
    if (!$products) {
        $product = Mage::registry('current_product');

        if ($category = Mage::registry('current_category')) {

        } elseif ($product) {
            $ids = $product->getCategoryIds();

            if (!empty($ids)) {
                $category = Mage::getModel('catalog/category')->load($ids[0]);
            }
        }

        if ($category) {
            if ($limit === false) {
                $limit = Mage::getStoreConfig('autorelated/general/limit');
            }

            $products = Mage::getResourceModel('reports/product_collection')
                ->addAttributeToFilter('visibility', array(
                    Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                    Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                ))
                ->addAttributeToFilter('status', 1)
                ->addCategoryFilter($category)
                ->addAttributeToSelect('*')
                ->setPageSize($limit);

            if ($product) {
                $products->addAttributeToFilter('entity_id', array(
                    'neq' => Mage::registry('current_product')->getId())
                );
            }

            $products->getSelect()->order(new Zend_Db_Expr('RAND()'));
            Mage::getModel('cataloginventory/stock')->addInStockFilterToCollection($products);

            $this->setData('related_products', $products);
        } else {
            return false;
        }
    }

    return $products;
}

If you have a different idea how to make it happen please let me know

thank you

1

Editing an already existing extension might be a bit of overkill for what you're trying to do. I would suggest creating your own extension (this tutorial will show you how).

Your block would look something like this.

class [Namespace]_[Module]_Block_Related extends Mage_Core_Block_Template
{
   public function getCollection()
   {
      $current_product = Mage::registry('current_product');

      $collection = Mage::getModel('catalog/product')->getCollection()
         ->addAttributeToSelect('name')
         ->addAttributeToSelect('thumbnail')
         ->addAttributeToSelect('...') // whatever you need
         ->addAttributeToFilter('brand', $current_product->getData('brand'))
         ->setOrder('created_at', 'DESC')
         ->setPageSize(5);

      return $collection;
   }
}

and your PHTML file something like

<?php
$collection = $this->getCollection();
?>
<table class="products-grid" id="upsell-product-table">
    <tbody>
<?php foreach ($collection as $item):?>
        <tr>
            <td>
                <img src="<?php echo Mage::getUrl('media') . $item->getData('thumbnail');?>" width="125" height="125" alt="<?php echo $this->getData('name');?>">
                <h3 class="product-name"><?php echo $this->getData('name');?></h3>
            </td>
        </tr>
<?php endforeach;?>
</tbody></table>
<script type="text/javascript">decorateTable('upsell-product-table')</script>

Bringing it all together on product page using the layout XML. Add the following to your local.xml

<reference name="content">
  <block type="[module]/related" name="custom_related" template="[module]/related.phtml"/>
</reference>

Haven't tested this code but it should work give or take a tweak or two

  • Thanks for such a fully described reply How do I insert a stock filter to make sure that products that are out of stock won't appear? – AntK Mar 27 '14 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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