Magento, trouble with getting product collection by manufacturer - magento

Good afternoon!
I am a beginner in magento, so have a question connected with getting product collection by manufacturer.
The code I used is bellow:
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('manufacturer');
$collection->addFieldToFilter(array(
array('attribute' => 'manufacturer', 'eq' =>'adidas'),
));
return $collection;
I checked that products with such attribute and value exist, but the size of returned array is 0.
Maybe, I have missed something important with attribute options or something else.
So I will be very thankful for every advice or idea.

If manufacturer dropdown value id is 10 then you can filter like this
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('manufacturer',10);
return $collection;

Related

How to add new value for SKU price in Magento?

I am new in Magento , I created my code to remove the special offer
and to add new price for SKU.
I could remove the special offer but I could not add the new price for SKUs. How can this be done?
My code is as follows:
$productIds[][] = array('DF-12''200','DF-98''300','DF-87''400');
$products = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*') // <- careful with this
->addAttributeToFilter(
'sku', array('in' => $productIds[0])
)
->load();
foreach ($products as $_product){
$product = Mage::getModel('catalog/product')->load($_product->getId());
$product->setprice($productIds[1])
$product->setOffertext('');
$product->setSpecialFromDate('');
$product->setSpecialToDate('');
$product->setSpecialPrice('');
$product->save();
}
I'm not quite sure how you're forming the array containing your products' SKU and prices, it seems off to me. But since I can only speculate on that part with the given information, an example of how I'd programmatically update smaller product collections might be of more help. This snippet works for me in a backend model:
// array('sku' => price )
$productPricesBySku = array('DF-1234' => 180.00, 'DF-1235' => 99.95);
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('price') // optional
->addAttributeToSelect('special_from_date') // optional
->addAttributeToSelect('special_to_date') // optional
->addAttributeToFilter('sku', array('in'=>array_keys($productPricesBySku)));
foreach($collection as $_product){
if(!($price=$productPricesBySku[$_product->getSku()])) continue;
// no need to load the product again
// just set the data directly on our collection-item and save it
$_product->setPrice((float)$price)
->setSpecialFromDate('')
->setSpecialToDate('')
->setSpecialPrice('')
->save();
}
(To prevent memory exhaustion on large collections you should use the Mage_Core_Model_Resource_Iterator rather than foreach-loops. You can read up on that here for instance.)

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;
}

Get product sku in Magento using product ID for big collections

I have a catalog with near 90000 products. I need to retrieve all the products sku that have no image associated. With a sql query I can get the id list of all the products without image. From that ID I need to get the product sku. So, I have an $ids array with all the products without image (near 60000).
I'm trying to get all the corresponding skus by using something easy with the magento api:
foreach ($ids as $id){
$product = Mage::getModel('catalog/product')->load($id);
echo $product->getSku()."\n";
}
But this causes a PHP Fatal error: Allowed memory size... (memory size is 1024Mb and I cannot change it).
My question is: from this $ids array, how can I get all the corresponding sku without causing a memory size error? Is there a lighter way of getting a product attribute having the product id?
Currently you are loading a lot of not needed product data and this causes a fatal error. In your case you need only product sku. I am suggestion to use Magento collections.
I guess, that the following code snippet will work for you:
$productsCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToFilter('entity_id', array('in' => $ids));
foreach($productsCollection as $product) {
echo $product->getSku();
}
Magento automatically adds the sku to the select, but pay attention, that sometimes you may want to get some custom product attribute, e.g. attribute with code "color". Then you need to add ->addAttributeToSelect('color') and the code will look like:
$productsCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToFilter('entity_id', array('in' => $ids))
->addAttributeToSelect('color');
foreach($productsCollection as $product) {
echo $product->getSku();
echo $product->getColor();
}
If you want to get all product attributes you can use ->addAttributeToSelect('*')
To get separate attribute without loading product
$resource = Mage::getResourceSingleton('catalog/product');
$sku = $resource->getAttributeRawValue($productId, 'sku', $storeId)

Magento Products Sort by Category

I need to sort the product list in productCollection by Category.
I need this facility because in my site i have to display New Products and Special Products in separate page(tab).
I could even use addAttributetoSort() for sku, news_to_date, news_from_date, name, price etc. But could not use category_id. It has no effect.
I know I can set it from admin panel in Manage Attribute. In Manage Attribute price, name is available to sort. Even I can create sku, news_to_date etc as attribute in from Manage Attribute. But when I create attribute code as category or category_id, it comes with an error like
System reserved category/category_id
Means I could not use category or category_id as attribute from Manage Attribute (As they are reserved by the System).
Can anyone suggest how to do this?
I'm sure this will sort by category:
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSort('category_ids', 'ASC')
->addAttributeToSelect(array('name', 'sku', 'status', 'visibility', 'is_saleable'))
;
That works for me:
$category ='1503'; // Is in my case Categoryid of New Products Category
$_products = Mage::getResourceModel('catalog/product_collection')
->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
->addAttributeToFilter('category_id', array('in' => $category))
->addAttributeToSelect('*');
$_products->load();
Regards

Filtering products by attributes in magento

How do i list products with a specific attribute? for example if Size is an attribute in my default attribute set. Then how do i list all the products which has Size = 22?
I am searching google for the answer, will post as answer if i find any.
Thanks,
Balan
I just did this not too long ago
To get the store id:
$storeId = Mage::app()->getStore()->getId();
To get all the products filtering out everything but Size
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter("Size", "1");
Then simply in a foreach loop add the following(this will loop through the array $_productCollection)
foreach($_productCollection as $_product)
{
$attr = Mage::getResourceModel('catalog/product')->getAttributeRawValue($_product->getId(), 'Size', $storeId);
if($attr == '22')
{
//Echo names of the products
echo $_product->getName();
}
}
This way you can still do a lot with the product list without having a strict listing of products. So you can still add names, prices, and more attributes if you wish. Hope this helped!
I don't think the catalog product is made for this. The best would be to move the attribute Size in the static table (catalog_product_entity). Then you will be able to search by size with keeping the catalog fast. After if you want to do this on different attribute, I guess you can give it as parameter in the collection. But I am not sure about it.
For example:
$storeId = Mage::app()->getStore()->getId();
$products = Mage::getResourceModel('catalog/product_collection');
$products->addAttributeToFilter(array(
array('attribute' => 'attribute_code', 'eq' => 22)))
->addAttributeToSelect('*')
->setStoreId($storeId)
->addStoreFilter($storeId)
->setOrder('name', 'desc');
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
$this->setProductCollection($products);
for me this works fine.

Resources