Categorize Related Products in Magento

Categorize Related Products in Magento

Would it be easier when you but stuff at a particular website that shows you all of the related products you might want to add in your shopping cart. Take for instance buying a inkjet printer and you might want to buy of course additional cartridges that comes with it.

In this post, the setOrder function comes into place

Copy Crosssell.php into app/code/local/Mage/Checkout/Block/Cart

Then, at line 150: _getCollection() function, insert setOrder function and set the attribute code and order parameter you desire.

protected function _getCollection() {
    $collection = Mage::getModel('catalog/product_link')->useCrossSellLinks()
        ->getProductCollection()
        ->setStoreId(Mage::app()->getStore()->getId())
        ->addStoreFilter()
        ->setOrder('attribute_set_id', 'asc') // Order by attribute set
        ->setPageSize($this->_maxItemCount);
    $this->_addProductAttributesAndPrices($collection);

    Mage::getSingleton('catalog/product_status')
    	->addSaleableFilterToCollection($collection);
    Mage::getSingleton('catalog/product_visibility')
    	->addVisibleInCatalogFilterToCollection($collection);
    Mage::getSingleton('cataloginventory/stock')
    	->addInStockFilterToCollection($collection);

    return $collection;
}

This also applies if you’re going to use the Related.php, instead of Crosssell.php.In my case, I’m stuck and okay with using the latter.

And my template would be this:

foreach($this->getItems() as $_item): ?>
    if ( $setId != $_item->getAttributeSetId() ) :
        $setId = $_item->getAttributeSetId();
  • getAttributeSetId() ) ?>
  • Now, I cant’ figure out how to get the attribute set name by id, so product_attribute_set.list SoapCleint service method. Hope someone will shed some light.

    global $attribute_code, $attributeSets;
    
    $proxy = new SoapClient($this->getBaseUrl().'api/soap/?wsdl');
    $sessionId = $proxy->login('your apiUser', 'your apiKey');
    $attributeSets = $proxy->call($sessionId, 'product_attribute_set.list');
    
    //Get Attrinute Name
    if (!function_exists('_getAttributeSet')) {
        function _getAttributeSet($id) {
            global $attributeSets;
            $setName = '';
            foreach($attributeSets as $set) {
                if ($set['set_id'] == $id ) {
                    $setName = $set['name'];
                }
            }
            return $setName;
        }
    }
    

    That’s it. Enjoy!


    Tagged as , , + Categorized as Magento

    Enjoyed this post?

    Subscribe to our RSS Feed, Follow us on Twitter or simply recommend us to friends and colleagues!

    • 0

    Facebook comments:

    3 Comments

    1. Rhuel,

      I run a printers and supplies website and I’d love to be able to implement this functionality in the latest 1.5 release of our Magento store, but it seems that a lot of the core has been rewritten since you wrote this article, do you have an updated version available?

      Kind regards,
      Mike

    2. Thank you for your interest.

      I apologize for the delay. Currently, I made this to my client which is probably upgrading to 1.5 in a very few months. I will get back to you as soon as I finish with other stuff I’m doing.

    3. Hi Rhuel, thanks for getting back to me. That would be much appreciated.

      Thanks for your time.

      Regards,
      Mike

    Leave a Comment