get the parent category ids in magento - magento

i put the following code into magento base template file view.phtml.
$categories = $_product->getCategoryIds();
print_r($categories)
with the sample data,when on the Ottoman product page,which breadcrumb is
Home / Furniture / Living Room / Ottoman
the output is Array ( [0] => 22 ) the 22 is Living Room id. how to get Furniture and Living Room id. thank you

$categories=$_product->getCategoryIds();
foreach ($categories as $category)
{
$info = Mage::getModel('catalog/category')
->load($category)
->getParentCategory();
echo $info->getId(); //display category ID
//var_dump($info->debug()); # display model contents
}

Here is how you can get all categories id's:
//load root category
$rootCategoryId = Mage::app()->getStore()->getRootCategoryId();
$rootCategory = Mage::getModel('catalog/category')
->load($rootCategoryId);
//load all children categories of the root category
$childrenCategories = $rootCategory->getChildrenCategories();
Now you can have access to their ids like so:
foreach ($childrenCategories as $category) {
var_dump($category->getId());
}

Related

Laravel 5 - How to Query if DB Value is Array

In database, I have "categories" and "products" table. In "products" table, there is a category_ids column which is an array, because the product has more than one categories.
What I want is to query the product that has category_ids of 1. I am trying to do like this, it is works, but I want a simpler way how to do.
$catId = 1; //category ID
$allProducts = Product::all(); // get all products
foreach ($allProducts as $product) { //loop all the products
if(in_array($catId, $product->category_ids)) { // check if category ID is in array of its product categories
$products[] = Product::find($product->id);
}
}
As you can see above,I must loop through all the products to check if category ID is exist in product categories array.
Is there a better way so it is not checking all the products?
All code is OK except one line .
$catId = 1; //category ID
$allProducts = Product::all(); // get all products
foreach ($allProducts as $product) { //loop all the products
$cats = explode(',',$product->category_ids);
if(in_array($catId, $cats)) { // check if category ID is in array of its product categories
$products[] = Product::find($product->id);
}
}
I added this line. I assume that multiple categories of a product will be stored in a single field with comma separated (Although its not the correct way, you need to create a M2M table)
$cats = explode(',',$product->category_ids);
So In in_array function, 2nd element should be an array. and Because of output of explode, $cats is an array.
in_array($catId, $cats)

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 Catalog Product SubCategory Name

I am implementing the Magento catalog product view page, and I need the name of subcategory the product is added to.
The code to display the category is like: $_product->category->name
but I am unable to get the sub category name.
to get the subcategory of a category, use the below code
$your_category_id = '2334'; // category_id whose subcategory you want to fetch
$subcats = Mage::getModel('catalog/category')->getCategories($your_category_id);
foreach ($subcats as $sub) {
echo $sub->getName();
}
Well I got the sub categories the following way:
<?php
if (Mage::registry('current_product')) {
if ($_product) {
$categoryIds = $_product->getCategoryIds();
$cat_id = $categoryIds[0]; ----> pass the level of sub catgeory to the array index
$category = Mage::getModel('catalog/category')->load($cat_id);
$cat_name = $category->getName();
}
}
?>

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

Get 'active from' attribute from category in magento 1.7

There are pre-defined attributes in Mangento 1.7 categories named
active from
active to
I can fetch Name of the current product's category using this:
$productId=$_helper->productAttribute($_product, $_product->getId(), 'id');
$product = Mage::getModel('catalog/product')->load($productId);
$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
$_cat = Mage::getModel('catalog/category')->load($category_id) ;
echo $_cat->getName();
} ?>
but I need to find the active from date of current category also.
How do I fetch this?
It is probably worth noting that these values are for the active state of a custom design for the category, and not the actual active state of the category itself...
As an array together:
$_cat = Mage::getModel('catalog/category')->load($category_id);
$_customDesignDates = $_cat->getCustomDesignDate();
Or individually:
$_cat = Mage::getModel('catalog/category')->load($category_id);
$fromDate = $_cat->getData('custom_design_from');
$toDate = $_cat->getData('custom_design_to');

Resources