cconur
cconur

Reputation: 107

MAGENTO - Get Last child category of shopping cart items

I have added this code in a .phtml file where I have the info of an additional checkout step I have created and where I want to set certain logic to be triggered by a category filter based on selecting the last children category for each item:

$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();  
foreach($items as $item) {  
$productId = $item->getProduct()->getCategoryIds(); 
$_category = Mage::getSingleton('catalog/category')->load($value);
$cartProductCatId = $_category->getChildrenCategory();

echo 'Category ID: '.$cartProductCatId.; 

But, no way.

Hierarchy tree of the categories I have is:

1) Parent_categ_ID=1 
1.1) Child_categ_ID=3 
1.2) Child_categ_ID=4 

2) Parent_categ_ID=2 
2.1) Child_categ_ID=5 
2.2) Child_categ_ID=6 

The categories I want to filter are: ID=3 and ID=5.

Upvotes: 1

Views: 1076

Answers (1)

cconur
cconur

Reputation: 107

I finally solved it in this way:

   $items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();  
   $filter_cats = array(3,5);
   $found_cat = false;
   foreach($items as $item){  
   $categories_array = $item->getProduct()->getCategoryIds(); 
   foreach($categories_array as $cat){
   if( in_array($cat, $filter_cats) ){
   $found_cat = true;
   break;
   }}}

I hope this is helpful for someone. Thanks!!

Upvotes: 1

Related Questions