getStoreCategories() to get categories by ids - magento

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

Related

JoomShopping. How to show other products on product page?

Well. I hate JoomShopping.
In template of product page i do smth like that:
$category_id = $this->category_id;
$category = JSFactory::getTable('category', 'jshop');
$category->load($category_id);
$products = $category->getProducts();
I got an array of products with everything I need. Except Links.
Show me another way (for examle, explain how it works on category page.
If you need objects with links, so try:
$category_id = $this->category_id;
$category = JSFactory::getTable('category', 'jshop');
$category->load($category_id);
$products = $category->getProducts();
addLinkToProducts($products, $categorys->$category_id);
var_dump($products);

Magento - assign multiple product ids to a category

I have a category "new" that I want to assign to, via a daily cron, the most recent 120 products.
I am trying to override the product ids assigned to a category.
Is there an easy way , like :
$category->setProductIds(array())
In PHP code you can put them into the category while you are importing them.
Say you have a product called $product and a category ID called $category_id
You can set the categories which a product belongs to by doing the following
$categories = array($category_id);
$product->setCategoryIds($categories);
$product->save();
f the product already has categories and you'd like to add one more then you can use getCategoryIds() like this:
$categories = $product->getCategoryIds();
$categories[] = $categoryId;
$product->setCategoryIds($categories);
$product->save();
it works like this
$category = Mage::getModel('catalog/category')->load($categoryID);
$product_array = $category->getProductsPosition()
... make changes ... format : $item[$product_id] => $position_in_category
$category->setPostedProducts($product_array);
$category->save();
To Remove product from category:
Mage::getSingleton('catalog/category_api')->removeProduct($category->getId(),$p‌​roduct->getId());
To Add Product to Category:
Mage::getSingleton('catalog/category_api')->assignProduct($category->getId(),$p‌​roduct->getId());
Now you can loop through all products and assign those to category using category id.
Note: This will not overwrite any categories the product is already in.

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

Laravel paginate items in multiple categories

I have 2 tables, products and categories. Products belong to different categories, and categories are formed in tree structure that allows for parent category and child category relationship. So given a specific category, how should I write query to get paginated products that belong to this category and all its child categories?
I forgot to mention that in my settings, products and categories are many to many relationship, there is a pivoting table named product_catrgory.
$ids = $category->children()->lists('id');
$ids[] = $category->id;
$products = Product::whereIn('category_id', $ids)->paginate(20);
Maybe there is other better method but you could do it this way:
$ids = [];
$ids[] = $category->id;
foreach ($category->children as $child) {
$ids[] = $child->id;
}
$products = Product::whereIn('cat_id', $ids)->paginate(15);
In my opinion the simplest way to do this is to first get category id and its all child category id recursively. And query the database using whereIn(array of category id) to get all the products. I do not think you can do it in one single query.

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