magento product collection - magento

In extention I'm getting product collection on catalog_product_view page like this:
if (!$product = Mage::registry('product')) {
return new Varien_Data_Collection();
}
$category = new Mage_Catalog_Model_Category();
$category->load($product->getCategoryId());
$collection = $category->getProductCollection();
and how can I add additional attributes to this collection?
for example I can't get something like this
$collection->addAttributeToSelect('manufacturer');
I wish to add some attribute by code (not id, because this may be different attributes added in layout) and then group data by this attribute
thnx

You could instantiate a product collection and filter it instead of getting the products of a specific category directly:
if (!$product = Mage::registry('product')) {
return new Varien_Data_Collection();
}
// get a product collection and filter it by category
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addCategoryFilter($product->getCategoryId());
// add the attributes you need
$collection->addAttributeToSelect('manufacturer');
$collection->setOrder('manufacturer', 'asc');
// load the collection and return it
$collection->load();
return $collection;
Be careful: You can not add attributes to the collection after loading it!
Additionally, you do not have to explicitely call load() - the collection will be loaded on demand.
Hope this helps.

Try this
<?php echo $_product->getResource()->getAttribute('manufacturer')->getFrontend()->getValue($_product); ?>

Related

Sort order by Name collection.php (magento)

I have a custom module with change the default CATALOG/RESOURCE/PRODUCT/COMPARE/ITEM/collection.php, but the line below:
->order('ai.sort_order ASC');
Change the order for position with attributes position, but a few of is (about 10 attributes) is just a text (not Dropdown, Select or Price) and cannot allow to orders correctly position iquals a product page order. (i need to leave the page to compare products in the same order from the product page of the attribute list)
how i can make this?
To create sort by position write below code after getting product collection in app/design/frontend/<theme_name>/default/template/catalog/product/list.phtml
$_productCollection = new Varien_Data_Collection();
$sortedCollection = array();
foreach ($_defaultProductCollection as $key => $_product) {
if(!isset($sortedCollection[$positions[$_product->getId()]])){
$sortedCollection[$positions[$_product->getId()]] = array();
}
$sortedCollection[$positions[$_product->getId()]][] = $_product;
}
ksort($sortedCollection);
foreach ($sortedCollection as $_products) {
foreach ($_products as $_product) {
$_productCollection->addItem($_product);
}
}
Hope it will work for you.

Magento: How do I get a list of categories from a product collection?

On a certain pages I have a large product collection with products of multiple categories. How do I get a list of categories that are included in the currently loaded product collection?
I can't think of another way other than iterating over the collection and keeping track of the categories of each product. Something like this:
$categories = [];
foreach ($productCollection as $product) {
$categories[] = $product->getCategory();
}
// To get rid of the duplicates
$categories = array_unique($categories);
This way you get the individual product categories, and you can mount the array as it sees best
<?php
$categoryIds = $_product->getCategoryIds();
foreach($categoryIds as $catId){
$category = Mage::getModel('catalog/category')->load($catId);
}
?>

getStoreCategories() to get categories by ids

I would like to get an array of categories for my magento store.
I need to use following unit for my website to work:
$categories = $helper->getStoreCategories('name', true, true);
However this lists all categories.
I would like to select categories that are important for me. I think I could do that by selecting ids of the categories or names of the categories but I don't know how to do it.
Could anybody help me please?
Let's say you have an array with category ids like this:
$ids = array(6,8,99);
You can get the category objects like this:
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', $ids);
if you want only active categories add this line also
$collection->addAttributeToFilter('is_active', 1);
Use this function and just pass category Id:
function getCategoryData($_categoryId=null) {
// For category Collection
$category = Mage::getModel('catalog/category')->load($_categoryId);
// For product Collection category wise
$prodCollection = $category->getProductCollection();
return $prodCollection;
}

Product Collection not find in module observer file

I have created a module. it is working properly but when we get product collection in Observer.php file then this is not return any objects and collection. it is empty and also not create any expection or log file. please help me.
I have this code in observer.php file
class GWB_ClearOrphan_Model_Observer
{
public function disableProducts(Varien_Event_Observer $observer)
{
try{
$collection = Mage::getModel('catalog/product')->getCollection();
}
catch(Exception $e) {
Mage::log($e->getMessage(), null, 'collection.log');
}
}
}
I am also trying both product collection model method but collection not found.
$collection = Mage::getResourceModel('catalog/product_collection');
$collection = Mage::getModel('catalog/product')->getCollection()->load();
Using
$collection = Mage::getModel('catalog/product')->getCollection();
will not load anything. You have to call load() method, like this:
$collection = Mage::getModel('catalog/product')->getCollection()->load();
It would be better to know what you want to achieve, which event do you observe and why you want to load all products in your observer? You do not use any filters on collection, so in fact statement above will load all products, which can kill your request.

Magento - get a list of bundled product ids from a product id

Lets say I load my product object:
$product = Mage::getModel('catalog/product')->load($productId);
Is there a function or some way to extract the bundled ids related to this product?
e.g.
$product->getBundledProductIDs()
The following should work:
$product->getTypeInstance(true)->getChildrenIds($product->getId(), false)
The result is a multi-dimensional array with the top level being options and the children of options being products.
Also, you can change the false to a true and it will only return required options of the bundle.
Try this-
$collection = $product->getTypeInstance(true)
->getSelectionsCollection(
$product->getTypeInstance(true)
->getOptionsIds($product), $product);
foreach ($collection as $item) {
# $item->product_id has the product id.
}

Resources