Question

Based on the helper catalog there is a function that returns a path for the breadcrumb to be displayed in Magento when viewing a Product.

I want to enhance this method to return the full path of the categories back to me.

Current implementation:

...         
if ($this->getProduct()) {
    $path['product'] = array('label'=>$this->getProduct()->getName());
}
$this->_categoryPath = $path;

Here I want to append a process that load the categories of the product via getCategoryIds(), which is available trough the getProduct() method.

I'd like to load a conditional collection, that only loads the respective ID of the categories but only the following data:

  • url
  • name
  • title
  • my_custom_eav_param

I assume something like:

if ($product = $this->getProduct()) {
    $cats = $product->getCategoryIds();
    foreach ($cats as $category_id) {
        $attributesToSelect = [''];
        $_cat = Mage::getModel('catalog/category')
            ->getCollection()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter(['name', 'id', 'url', 'description'])
            ->load()
            ->getSelect()->__toString();
        var_dump($_cat); die;
    }
    $path['product'] = array('label'=>$this->getProduct()->getName());
}
$this->_categoryPath = $path;

but this does not work. How can I specify what entities are loaded in order to keep load to DB as low as possible for this requests? To load the whole category all the time is a lot of traffic, or can it be ignored completely?

Was it helpful?

Solution

Solution was to use it the following way:

$_cat = Mage::getModel('catalog/category')->getCollection()
                        ->setStoreId(Mage::app()->getStore()->getId())
                        ->addAttributeToSelect(['url_path', 'name', 'entity_id', 'url_key', 'breadcrumb_name'], 'left')
                        ->addAttributeToFilter('entity_id', ['eq' => $category_id])
                        ->getFirstItem();

This way I can load the collection faster and create cheaper requests to the DB.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top