Hi i want to load multiple category id in magento, i used in this but it fetch only 1st category of subcategory not rest of category.
$category = $model->load(79,80,91);
You can use this:
$categories = array(1,2,3);
$category = Mage::getModel('catalog/category')->getCollection()->addAttributeToFilter('entity_id', array('in'=>$categories));
foreach($category as $categorys) {
//or do Somthing
}
you have to use collection instead of load
$collection = Mage::getModel('catalog/category')->getCollection()
->setStoreId(Mage::app()->getStore()->getId())
->addAttributeToSelect('name')
->addIdFilter(array(79,80,91))
->addAttributeToFilter('is_active', 1)//get only active categories if you want
->addAttributeToSort('position', 'desc'); //sort by position
and then you can use loop throw
foreach($collection as $category) {
echo $category->getName()
}
hope this will work for you.
you can use this,
$categories = array(10,13);
$_category = Mage::getModel('catalog/category');
$cats = $_category->getCollection()->addAttributeToFilter('entity_id', array('in'=>$categories));
foreach($cats as $cat) {
Zend_Debug::dump($cat);
//or
// do Somthing
}
Related
I want to get all products and get each product category name from the category table .. anyone can help ???
public function viewProducts(Request $request){
$products = Product::get();
foreach($products as $key => $val){
$category_name = Category::where(['id' => $val- >category_id])->first();
$products[$key]->category_name = $category_name->name;
}
$products = json_decode(json_encode($products));
//echo "<pre>"; print_r($products); die;
return view('admin.products.view_products')->with(compact('products'));
}
You can use join
$products = Product::join('categories','categories.id', 'products.category_id')->get();
I'm trying to limit order for admin users by category, I can find the collection of product id by category and collect sales/order but how can I use this with an event observer?
$category_id = 44;
$category = Mage::getModel("catalog/category")->load($category_id);
$products = Mage::getModel("catalog/product")->getCollection()
->addCategoryFilter($category);
Next I collect just the product ids so I can use them:
$product_ids = array();
foreach ($products as $product)
$product_ids[] = $product->getId();
$items = Mage::getModel("sales/order_item")->getCollection()
->addFieldToFilter("product_id", array("in" => $product_ids));
You can use SetPageSize and SetCurPage for that
$col = Mage::getModel('YOUR MODEL')
->getCollection()
->setPageSize(17)
->setCurPage(1);
HTH
I had a main category (parent category) whose id = 5 & 37. I want to collection of its sub-categories. How can I do that?
$catid = array(5,37);
$_category = Mage::getModel('catalog/category')->load(5);
$_subcategories1 = $_category->getChildrenCategories();
$_category = Mage::getModel('catalog/category')->load(37);
$_subcategories2 = $_category->getChildrenCategories();
i want the collection which have children categories from both category id(5,37)
You can get that from one select:
$subcategories = Mage::getModel('catalog/category')
->setStoreId(Mage::app()->getStore()->getId())
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('parent_id', array(5, 37))
->setOrder('parent_id', 'ASC');//if you want them somehow grouped by parent_id
foreach ($subcategories as $category){
//do something with $category
}
here i am giving you one example to merge two collection of category in to one collection
$storeId = Mage::app()->getStore()->getId();
$categoryOneId = 5;
$categoryTwoId = 37;
$categoryOne = Mage::getModel('catalog/category')->load($categoryOneId);
$categoryTwo = Mage::getModel('catalog/category')->load($categoryTwoId);
$collectionOne = Mage::getModel('catalog/product')->getCollection()
->setStoreId($storeId)
->addCategoryFilter($categoryOne);
$collectionTwo = Mage::getModel('catalog/product')->getCollection()
->setStoreId($storeId)
->addCategoryFilter($categoryTwo);
$merged_ids = array_merge($collectionOne->getAllIds(), $collectionTwo->getAllIds());
$mergedCollection = Mage::getModel('catalog/product')->getCollection()
->addFieldToFilter('entity_id', $merged_ids);
hope this will sure help you.
If I were to create a collection that would retrieve all products belonging to category with a given ID, as below:
$storeId = Mage::app()->getStore()->getId();
$product = Mage::getModel('catalog/product');
$category = Mage::getModel('catalog/category')->load(39);
$catName = $category->getName();
$visibility = array(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
);
$products = $product->setStoreId($storeId)
->getCollection()
->addAttributeToFilter('visibility', $visibility)
->addCategoryFilter($category)
->addAttributeToSelect(array('name'), 'inner')
->setOrder('name', 'asc')
;
$collection = $products;
How would I then iterate over the collection and access each item's data, item being a product in this case.
I would have expected something like the below to give me each items available data but doesn't seem to work:
foreach ($collection as $key => $value) {
var_dump($value->getData());
}
You're close. Try doing
foreach ($collection as $obj) {
echo $obj->getName();
}
I know Magento categories can be attained with
$categories = Mage::getResourceModel('catalog/category_collection')
but then how do I get a list of these categories and their respective item SKUs?
If you simply require an array containing the category id and an array of skus...
$skuArray = array();
$categoryCollection = Mage::getModel('catalog/category')->getCollection();
foreach ($categoryCollection as $category) {
$skuArray[$category->getId()] = $category->getProductCollection()->getColumnValues('sku');
}
Alternatively, add a new field to each of the collection objects to contain the skus...
$categoryCollection = Mage::getModel('catalog/category')->getCollection();
foreach ($categoryCollection as $category) {
$skus = $category->getProductCollection()->getColumnValues('sku');
$category->setData('skus', $skus);
}
This would be an option if you have further work to do on the collection later in your code and you still need to access the product sku array.
foreach($categoryCollection as $category) {
$categorySkus = $category->getData('skus');
}
You can use :
$categories = Mage::getResourceModel('catalog/category_collection');
foreach ($categories as $category) {
$products = $category->getProductCollection();
foreach ($products as $product) {
$sku[$product->getId()] = $product->getSku();
}
}
I store all the sku in $sku as an array. Yours may be different.