Get bundle product items in cart of Magento - magento

Pls help me in my trouble. I need to get bundle products from cart of magento, and than I need to get the selected items of every bundle product. How can I do this? Thanx!

In magento 1.8.1
$cart = Mage::getModel('checkout/cart')->getQuote();
foreach ($cart->getAllItems() as $_item) :
$_product = Mage::getModel('catalog/product')->load($_item->getProduct()->getId());
if($_product->getTypeId()==='bundle') :
$options = $_item->getProduct()->getTypeInstance(true)->getOrderOptions($_item->getProduct());
?>
<dl class="item-options">
<?php foreach ($options['bundle_options'] as $option):?>
<dt><?php echo $option['label'] ?></dt>
<?php foreach ($option['value'] as $sub) :?>
<dd><?php echo $sub['qty'] . " x " . $sub['title'] . " " . Mage::helper('core')->currency($sub['price']) ?></dd>
<?php endforeach;
endforeach;
?>
</dl>
<?php endif;
endforeach

$optionCollection = $product->getTypeInstance()->getOptionsCollection();
$selectionCollection =$product->getTypeInstance()->getSelectionsCollection($product->getTypeInstance()->getOptionsIds());
$options = $optionCollection->appendSelections($selectionCollection);
foreach( $options as $option )
{
$_selections = $option->getSelections();
foreach( $_selections as $selection )
{
$product_simple = Mage::getModel('catalog/product')->load($selection->getId());
}
}
Hope this will work for you. :)
Best of luck.

$_product = Mage::getModel('catalog/product')->load($_item->getProduct()->getId());
if($_product->getTypeId()==='bundle') {
$options = $_item->getProduct()->getTypeInstance(true)->getOrderOptions($_item->getProduct());
?>
<dl class="item-options">
<?php
foreach ($options['bundle_options'] as $option) {?>
<dt><?php echo $option['label'] ?></dt>
<dd><?php echo $option['value'][0]['title'] ?></dd>
<?php
}
?>
</dl>
<?php }
This work for Input Type "Dropdown" for bundle items

Related

Very slow product list issue with magento

I have a category called new arrivals which has 5 subcategories. I created a custom view called new-arrivals.phtml. I created custom Static Block with the following code in magento
{{block type="core/template" template="custom/new-arrivals.phtml"}}
then from Category setting I changed the display setting>CMS Block to the this cms block that I created. And the display mode is static block only
This view bring all the subcategories of New Arrivals(Bags, Tops, Shoes etc..) with it's products.. Like on this website As you can see on this website it is very fast but on my website with my code it is not. Here it is...
How can I make this load faster. Cache and everything is enabled. I assume that there is something wrong with my code. thank you for your help.
and this is new-arrivals.phtml content:
Magento CE 1.8.1
<?php
//I load all the subcategories here
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'thumbnail'))
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
?>
<?php foreach ($categories as $category): ?>
<div class="page-title category-title">
<h1>
<a href="<?php echo $category->getUrl() ?>">
<span><?php echo $category->getName() ?></span>
</a>
</h1>
<?php
// here I load all the products for each category
$_helper = $this->helper('catalog/output');
$category = Mage::getModel('catalog/category')->load($category->getId());
$_productCollection = $category->getProductCollection()->addCategoryFilter($category)
->addAttributeToSelect('*') // add all attributes - optional
->addAttributeToFilter('status', 1) // enabled
->addAttributeToFilter('visibility', 4) //visibility in catalog,search
->addAttributeToSort('date_added', 'ASC');
?>
<div class="product-count"><?php echo $count; ?> <?php echo $this->__('Products'); ?></div>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
<?php // Grid Mode ?>
<?php
$_span = 'span3';
$_grid_pro = 4;
?>
<?php $_collectionSize = $_productCollection->count() ?>
<?php $_columnCount = $_grid_pro; //$this->getColumnCount(); ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php if ($i++%$_columnCount==0): ?>
<?php // all the others stuff ....... ?>
Load this file using ajax or jquery.
Second one you run your code on footer and make one variable then using jquery or java script, you will show your all data in particular div, for example like this
if (count($_subcategories) > 0){
$top_menu .= '<ul>';
foreach($_subcategories as $_subcategory){
$prodCollection = Mage::getModel('catalog/category')- >load($_subcategory->getId())
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('type_id', array('eq' => 'grouped'))
->addAttributeToFilter('no_to_show', array('F' => 78));
if($prodCollection->count() !=0){
$top_menu .= '<li'; if($current_category == $_subcategory->getId()){$top_menu .= ' id="activesub"';}$top_menu .= '>'.$_subcategory->getName().'';
$_subcategories = Mage::getModel('catalog/category')->load($_subcategory->getId());
$_childsub = $_subcategories->getChildrenCategories();
$CatTotalCount = count($_childsub);
if($cntr == 0){
$firstCount = $CatTotalCount;
}
if($firstCount >= 7){
$loopCount = ($firstCount-1);
}else{
$loopCount = 6;
}
if($CatTotalCount >0){
$top_menu .= '<ul>';
$secCntr = 0;
foreach($_childsub as $_child_subcat){
$prodCollection2 = Mage::getModel('catalog/category')->load($_child_subcat->getId())->getProductCollection()->addAttributeToSelect('*')->addAttributeToFilter('type_id', array('eq' => 'grouped'))->addAttributeToFilter('no_to_show', array('F' => 78));
if( $secCntr>$loopCount){
$secCntr = 0;
$top_menu .= '</ul><ul>';
}
$secCntr++;
if($prodCollection2->count() !=0):
$top_menu .= '<li'; if($current_category == $_child_subcat->getId()){$top_menu .= ' id="activesub"';}$top_menu .= '>'.$_child_subcat->getName().'</li>';
endif;
} }
}
$top_menu .= '</ul></li>';
$cntr++;
}
$top_menu .= '</ul>';
}
jQuery(document).ready(function() {
document.getElementById('top_menus').innerHTML = '';
jQuery('#mega-menu-8').dcMegaMenu({
rowItems: '3',
speed: 'fast',
effect: 'fade'
});
});

Strange replace on magento URL

For some reason, only on view-mode icons on the product list, the "?" of the urls are replaced to "#21". Ex.: "#%21mode=list"
Somebody can help me?
Some code:
app/design/frontend/default/themename/template/catalog/product/list/toolbar.phtml
<?php if( $this->isEnabledViewSwitcher() ): ?>
<p class="view-mode">
<?php $_modes = $this->getModes(); ?>
<?php if($_modes && count($_modes)>1): ?>
<label><?php echo $this->__('View as') ?>:</label>
<?php foreach ($this->getModes() as $_code=>$_label): ?>
<?php if($this->isModeActive($_code)): ?>
<strong title="<?php echo $_label ?>" class="<?php echo strtolower($_code); ?>"><?php echo $_label ?></strong>
<?php else: ?>
<?php echo $_label ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
</p>
<?php endif; ?>
app/code//core/Mage/Catalog/Block/Product/List/Toolbar.php
public function getModeUrl($mode)
{
return $this->getPagerUrl( array($this->getModeVarName()=>$mode, $this->getPageVarName() => null) );
}
and...
public function getPagerUrl($params=array())
{
$urlParams = array();
$urlParams['_current'] = true;
$urlParams['_escape'] = true; // I already tried set false, but didn't helps
$urlParams['_use_rewrite'] = true;
$urlParams['_query'] = $params;
return $this->getUrl('*/*/*', $urlParams); // I also tried not use the getUrl and concat the querystring, strangely the replace still happends...
}
I couldn't find the problem, but I make a dirt solution:
<?php echo str_replace('#%21', '&', $this->getModeUrl($_code)); ?>
I wish somebody find a better solution :)

Magento - Set category in layered navigation

Whenever I search a product using the search box, on the left side there is a filter by category. The code that creates this filter is this one:
<dl id="narrow-by-list">
<?php $_filters = $this->getFilters() ?>
<?php foreach ($_filters as $_filter): ?>
<?php if($_filter->getItemsCount()): ?>
<?php if($_filter->getName() != "Category"){ ?>
<dt><?php echo $this->__($_filter->getName()) ?></dt>
<dd>
<?php echo $_filter->getHtml() ?>
</dd>
<?php } endif; ?>
<?php endforeach; ?>
</dl>
This shows only the main category in the filter, and I'd like to show its subcategories. I tried to set another category programmatically with this code:
<?php
$layer = Mage::getSingleton('catalog/layer');
$_categ = Mage::getModel('catalog/category')->load(5);
$layer->setCurrentCategory($_categ);
?>
... but nothing changed. Any thoughts?
When you try to set the other category on the layer singleton from a template, it's too late as all the treatments were already applied.
What you can do is to copy the file app/code/core/Mage/CatalogSearch/Model/Layer.php into app/code/local/Mage/CatalogSearch/Model/ and to add a modified version of the base Mage_Catalog_Model_Layer::getCurrentCategory() method, looking like this :
public function getCurrentCategory()
{
$category = $this->getData('current_category');
if (is_null($category)) {
if ($category = Mage::registry('current_category')) {
$this->setData('current_category', $category);
}
else {
$category = Mage::getModel('catalog/category')->load(5); // Put here the ID of the category you want to use as basis for category filtering
$this->setData('current_category', $category);
}
}
return $category;
}

Get specific category level

How can I get a specific category level from Magento, my category setup looks like this now.
root_catalog
|-Shop
|-Shoes
|-T-shirts
|-Brands
|-Nike
|-Womens
|-Mens
|-Adidas
|-Asics
<?php if( $category = Mage::getModel('catalog/category')->load( $categories[1]) ): ?>
<?php echo $category->getName(); ?>
<?php endif ?>
When calling $category->getName(); I would like to only display the Brand Name, is that possible?
You can get category level from
$category = Mage::getModel('catalog/category')->load( $categories[1]) )->getLevel()
and then check with your brand name category level, if match then display name.
e.g. suppose brand category level is 3
<?php if( $category = Mage::getModel('catalog/category')->load( $categories[1]) ): ?>
<?php if($category->getLevel() == 3)
echo $category->getName(); ?>
<?php endif ?>
<?php endif ?>
ANKIT's answer is good, but it could be improved by actually query-ing the specific levels instead of loading the whole collection and doing a conditional. Take for example if you want to get all categories in a specific level:
<ul>
<?php $categories = Mage::getModel('catalog/category')
->getCollection()
// magic is prepared here..
->addAttributeToSelect('*')
// then the magic happens here:
->addAttributeToFilter('level', array('eq'=>2))
->load();
foreach($categories as $category):
?>
<li>$category->getName()</li>
<?php endforeach; ?>
</ul>
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
$categories = $product->getCategoryIds(); /*will return category ids array*/
foreach($categories as $category){
$cat = $objectManager->create('Magento\Catalog\Model\Category')->load($category);
if ($cat->getLevel() == 2) {
$catName = $cat->getName().","; ?>
<div class="brand_bg">
<label><?php /* #escapeNotVerified */ echo __('Category :') ?></label>
<?php echo $catName; ?>
</div>
<?php } ?>
<?php } ?>
?>

I need to get a list of sibling categories in magento 1.7

So far I think i am working at something like this
<?php
$parentId = $_category->getParentId();
$cats = Mage::getModel('catalog/category')->load($parentId)->getChildred();
$catIds = explode(',',$cats);
?>
<ul>
<?php foreach($catIds as $catId): ?>
<li>
<?php
$category=Mage::getModel('catalog/category')->load($catId)
echo $category->getName();
?>
</li>
<?php endforeach; ?>
</ul>
I am trying to have lateral movement in subcategories without having to go up a layer.
Let me know if that made sense.
The below code will helps you to load 2nd level (sub)category. you can set the $_rootCatId as dynamic also by Mage::app()->getStore()->getRootCategoryId();.
$_rootCatId = 2;//Root Category
$_rootCategory = Mage::getModel('catalog/category')->load($_rootCatId);
$_catName = $_rootCategory->getName();
if($_rootCategory->hasChildren())
{
$_collection = Mage::getResourceModel('catalog/category_collection')
->addAttributeToFilter('is_active', 1)
->addAttributeToSort('position', 'asc')
->joinUrlRewrite()
->addIdFilter($_rootCategory->getChildren())
->load();
foreach($_collection AS $_sub)
{
$_subCat = Mage::getModel('catalog/category')->load($_sub->getId());
if($_subCat->hasChildren())
{
echo '<li class="leve10 nav-'. $_subCat->getId() .'">';
echo '<a id="subCatLink">';
echo '<span>'. $this->htmlEscape($_subCat->getName()) .'</span>
</a>
</li>';
echo '<ul id="subCatUl" class="no-display" style="padding:0px 13px;">';
$__collection = Mage::getResourceModel('catalog/category_collection')
->addAttributeToFilter('is_active', 1)
->addAttributeToSort('position', 'asc')
->joinUrlRewrite()
->addIdFilter($_subCat->getChildren())
->load();
foreach($__collection AS $__sub)
{
$__subCat = Mage::getModel('catalog/category')->load($__sub->getId());
echo '<li class="leve20 nav-'. $__subCat->getId() .'">
<a href="'. $this->getCategoryUrl($__subCat) .'">
<span>'. $this->htmlEscape($__subCat->getName()) .'</span>
</a>
</li>';
}
echo '</ul>';
}
else
{
echo '<li class="leve10 nav-'. $_subCat->getId() .'">
<a href="'. $this->getCategoryUrl($_subCat) .'">
<span>'. $this->htmlEscape($_subCat->getName()) .'</span>
</a>
</li>';
}
}
}
else
echo 'No Categories Found...';
Assuming that your $_category is a valid instance of Mage_Catalog_Model_Category:
<?php $_siblings = $_category->getParentCategory()->getChildrenCategories(); ?>
<ul>
<?php foreach($_siblings as $_sibling): ?>
<li>
<?php echo $_sibling->getName(); ?>
</li>
<?php endforeach; ?>
</ul>

Resources