Add A Checkout Block to Header - magento

I'm trying to add a block to my Header that displays Cart information.
I'm not getting any display from this block as of yet.
Here's my checkout.xml:
<reference name="header">
<block type="checkout/cart_sidebar" name="checkout.basket" as="checkoutBasket" />
</reference>
Here's my basket.phtml:
<script>alert("TEST");</script>
<div class="block checkout-basket">
TEST
</div>
Here's my header.phtml:
<?php echo $this->getChildHtml('checkoutBasket'); ?>
I'm fairly new to Magento and it's Block system so I'm not sure I've joined up all the dots correctly to get this block to display.

There template file is missing in the code of xml
<block type="checkout/cart_sidebar" name="checkout.basket" as="checkoutBasket" template="pathoftemplate/basket.phtml" />
if you want to show all cart item just like default
then
<block type="checkout/cart_sidebar" name="checkout.basket" as="checkoutBasket" template="pathoftemplate/basket.phtml">
<action method="addItemRender"><type>simple</type><block>checkout/cart_item_renderer</block><template>checkout/cart/sidebar/default.phtml</template></action>
<action method="addItemRender"><type>grouped</type><block>checkout/cart_item_renderer_grouped</block><template>checkout/cart/sidebar/default.phtml</template></action>
<action method="addItemRender"><type>configurable</type><block>checkout/cart_item_renderer_configurable</block><template>checkout/cart/sidebar/default.phtml</template></action>
<block type="core/text_list" name="cart_sidebar.extra_actions" as="extra_actions" translate="label" module="checkout">
<label>Shopping Cart Sidebar Extra Actions</label>
</block>
And code must be add in basket.phtml
<?php if ($this->getIsNeedToDisplaySideBar()):?>
<div class="block block-cart">
<?php $_cartQty = $this->getSummaryCount() ?>
<div class="block-title">
<strong><span><?php echo $this->__('My Cart') ?></span></strong>
</div>
<div class="block-content">
<?php if ($_cartQty>0): ?>
<div class="summary">
<?php if ($_cartQty==1): ?>
<p class="amount"><?php echo $this->__('There is 1 item in your cart.', $this->getUrl('checkout/cart')) ?></p>
<?php else: ?>
<p class="amount"><?php echo $this->__('There are %s items in your cart.', $this->getUrl('checkout/cart'), $_cartQty) ?></p>
<?php endif ?>
<p class="subtotal">
<?php if ($this->canApplyMsrp()): ?>
<span class="map-cart-sidebar-total"><?php echo $this->__('ORDER TOTAL WILL BE DISPLAYED BEFORE YOU SUBMIT THE ORDER'); ?></span>
<?php else: ?>
<span class="label"><?php echo $this->__('Cart Subtotal:') ?></span> <?php echo Mage::helper('checkout')->formatPrice($this->getSubtotal()) ?>
<?php if ($_subtotalInclTax = $this->getSubtotalInclTax()): ?>
<br />(<?php echo Mage::helper('checkout')->formatPrice($_subtotalInclTax) ?> <?php echo Mage::helper('tax')->getIncExcText(true) ?>)
<?php endif; ?>
<?php endif; ?>
</p>
</div>
<?php endif ?>
<?php if($_cartQty && $this->isPossibleOnepageCheckout()): ?>
<div class="actions">
<?php echo $this->getChildHtml('extra_actions') ?>
<button type="button" title="<?php echo $this->__('Checkout') ?>" class="button" onclick="setLocation('<?php echo $this->getCheckoutUrl() ?>')"><span><span><?php echo $this->__('Checkout') ?></span></span></button>
</div>
<?php endif ?>
<?php $_items = $this->getRecentItems() ?>
<?php if(count($_items)): ?>
<p class="block-subtitle"><?php echo $this->__('Recently added item(s)') ?></p>
<ol id="cart-sidebar" class="mini-products-list">
<?php foreach($_items as $_item): ?>
<?php echo $this->getItemHtml($_item) ?>
<?php endforeach; ?>
</ol>
<script type="text/javascript">decorateList('cart-sidebar', 'none-recursive')</script>
<?php else: ?>
<p class="empty"><?php echo $this->__('You have no items in your shopping cart.') ?></p>
<?php endif ?>
</div>
</div>
<?php endif;?>

Related

magento product attribute return NULL after a getChildHtml call

I am using Magento 1.9.2.2,
I have created a childhtml that takes care of reviews, it looks like:
<catalog_product_view translate="label">
<reference name="product.info">
<block type="review/product_view_list" name="productinfo.reviewinfo" as="hs_review" template="catalog\product\hs_review.phtml"></block>
</reference>
</catalog_product_view>
hs_reviw.ptml:
<?php $ratingSummary =Mage::getModel('rating/rating')->getEntitySummary($_p->getId()); ?>
<div class="review-container">
<?php if ($ratingSummary) { ?>
<div class="w ratting">
<div class="rating-value" style="width: <?php
echo $ratingSummary->getSum() / $ratingSummary->getCount(); ?>%"></div>
</div>
<div class="reviw-count w">
<span><?php echo $ratingSummary->getCount() ?> Reviews</span>
</div>
<?php } ?>
<div class="review-add w">
<a href="<?php echo Mage::getUrl('review/product/list',
array('id'=> $this->getProduct()->getId(),
'category' => $this->getProduct()->getCategoryId()
)); ?>">Write a Review</a>
</div>
</div>
now the strange thing happens in view.ptml
when i use
<?php var_dump( $_product->getShortDescription()); ?>
<?php echo $this->getChildHtml('hs_review')?>
it dumps product short description and reviews but when I use
<?php echo $this->getChildHtml('hs_review')?>
<?php var_dump( $_product->getShortDescription()); ?>
review works perfectly but product short description return Null

Show categories on left and not on top navigation

I tried implemented a code in my Navigation/left.phtml
The code is as follows
<!-- subcategory code -->
<?php
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
if(!$category->getChildren()){
//$parentcat_id = $category->getParentCategory()->getId();
//$category = Mage::getSingleton('catalog/category')->load($parentcat_id);
$category = $category->getParentCategory();
}
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'thumbnail'))
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren());
?>
<div class="block">
<div class="block-title">
<span><?php echo $category->getName() ?></span>
</div>
<div class="block-content clearfix">
<ul class="subcategories">
<?php foreach ($categories as $category): ?>
<li>
<a href="<?php echo $category->getUrl() ?>"><!--<img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getThumbnail() ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />-->
<span><?php echo $category->getName() ?>
<?php $collection = Mage::getModel('catalog/product')->getCollection()->addCategoryFilter($category);
echo "(".count($collection).")"; ?></span>
</a>
</li>
<?php endforeach; ?>
</ul></div>
</div>
<!-- subcategory code -->
<!-- List all categories and their second level subcategories -->
<div class="block block-list block-categories">
<div id="block-categories" class="block-title active">
<strong><span>Categories </span></strong>
</div>
<div id="leftnav" class="block-content" style="display:block">
<?php $helper = $this->helper('catalog/category') ?>
<?php $categories = $this->getStoreCategories() ?>
<?php if (count($categories) > 0): ?>
<ul id="leftnav-tree" class="level0">
<?php foreach($categories as $category): ?>
<li class="level0<?php if ($this->isCategoryActive($category)): ?> active<?php endif; ?>">
<?php //if ($this->isCategoryActive($category)): ?>
<?php $subcategories = $category->getChildren() ?>
<?php if (count($subcategories) > 0): ?>
<ul id="leftnav-tree-<?php echo $category->getId() ?>" class="level1">
<?php foreach($subcategories as $subcategory): ?>
<li class="level1<?php if ($this->isCategoryActive($subcategory)): ?> active<?php endif; ?>">
<?php echo $this->escapeHtml(trim($subcategory->getName(), '- ')) ?>
<?php $secondLevelSubcategories = $subcategory->getChildren() ?>
<?php if (count($secondLevelSubcategories ) > 0): ?>
<ul id="leftnav-tree-<?php echo $subcategory->getId() ?>" class="level2">
<?php foreach($secondLevelSubcategories as $secondLevelSubcategory ): ?>
<li class="level2<?php if ($this->isCategoryActive($secondLevelSubcategory )): ?> active<?php endif; ?>">
<?php echo $this->escapeHtml(trim($secondLevelSubcategory ->getName(), '- ')) ?>
</li>
<?php endforeach; ?>
</ul>
<script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
<?php endif; ?>
<?php //endif; ?>
</li>
<?php endforeach; ?>
</ul>
<script type="text/javascript">decorateList('leftnav-tree', 'recursive')</script>
<?php endif; ?>
</div>
</div>
But when i set Include in Navigation Menu to No it also disappears from here.
Is there any way I can hide from top menu and show in my side menu?
Demo link is http://infigic.com/ds4u/
If you don't have local.xml file just create one in the layout folder of your theme and populate it with this content:
<?xml version="1.0"?>
<layout>
<default>
<reference name="header">
<action method="unsetChild"><name>top.menu</name></action>
</reference>
<reference name="left">
<action method="insert"><child>top.menu</child></action>
</reference>
</default>
</layout>
Changing your collection request to this should work :
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'thumbnail'))
->addAttributeToFilter('is_active', 1)
->addAttributeToFilter('include_in_menu', 1) // there is the 'magic'
->addIdFilter($category->getChildren());
Although, I do not recommend you to do it like this.
Because:
You are breaking the MVC pattern of Magento when requesting a model from a view
You are doing over complicated things that you may probably achieve easier by doing a block which extends Mage_Catalog_Block_Navigation, where the category may already be filtered the right way, but not rendered as you would like.
Please have a look at base Magento template (well not only the one in /app/design/frontend/base, but the one coming with a fresh install of Magento) and you'll see that never ever are there going to be a direct call in a view to a model instance.
For Magento good practice, please refer to the perfect blog of Alan Storm (who also is a SO member) here and maybe more specifically to this chart showing how templates (view) should interact with blocks to render models.

Hiding or Showing Filters In Certain Categories in magento

on our highest level category there are about 50 shop-by options, i m trying to hide attribute filter with xml code in custom layout of categories. with this code
<reference name="em.catalog.leftnav">
<action method="setData">
<instruction>hide_attribute_code</instruction>
<value>1</value>
</action>
But not hide filter attribute in that category, check it on image
in catalog.xml
<catalog_category_layered translate="label">
<label>Catalog Category (Anchor)</label>
<reference name="left">
<block type="catalog/layer_view" name="catalog.leftnav" after="currency" template="catalog/layer/view.phtml"/>
</reference>
<catalog_category_layered translate="label">
follow the template ... catalog/layer/view.phtml
Open this file and made there conditions for filters to appear on frontend, by name or id
<?php $_filters = $this->getFilters() ?>
<?php foreach ($_filters as $_filter): ?>
<?php if($_filter->getName() == 'Price' || $_filter->getName() == 'Category' || $_filter->getName() == 'Manufacturer' ): ?>
<?php if($_filter->getItemsCount()): ?>
<dt><?php echo $this->__($_filter->getName()) ?></dt>
<dd><?php echo $_filter->getHtml() ?></dd>
<?php endif; ?>
<?php endif; ?>
<?php endforeach; ?>
I do not know your extension that is reliable for layered navigation, but this should work:
<reference name="em.catalog.leftnav">
<action method="hideAttributes">
<code>hide_attribute_code</code>
</action>
</reference>
Go to the file in app/code/POOL/YOUR/MODULE/Blocks/... that builds your filter. If your template has something like getFilters() you can try echo get_class($this) to get the correct class/file. There you have to do two things:
1.) add a new method (in this case you can set comma seperate the attribute codes)
public function hideAttributes($attributeCodes)
{
$attributeCodes = array_map('trim', explode(',', $attributeCodes));
$this->setData('hide_attributes', $attributeCodes);
}
2.) search for the function that collects the filters, e.g. getFilters() and add
$filterableAttributes = // some code
foreach ($filterableAttributes as $attribute) {
if (!in_array($attribute->getAttributeCode(), $this->getHideAttributes())) {
...
}
}
You will need to unset the attribute name on view layer, so find this file:
magento/app/design/frontend/base/default/template/catalog/layer/view.phtml
edit:
<?php if($this->canShowBlock()): ?>
<div class="block block-layered-nav">
<div class="block-title">
<strong><span><?php echo $this->__('Shop By') ?></span></strong>
</div>
<div class="block-content">
<?php echo $this->getStateHtml() ?>
<?php if($this->canShowOptions()): ?>
<p class="block-subtitle"><?php echo $this->__('Shopping Options') ?></p>
<dl id="narrow-by-list">
<?php $_filters = $this->getFilters() ?>
<?php foreach ($_filters as $_filter): ?>
<?php if($_filter->getItemsCount()): ?>
<!-- add this line start-->
<?php if($_filter->getName() != "Color"): ?>
<!-- add this line end-->
<dt><?php echo $this->__($_filter->getName()) ?></dt>
<dd>
<?php echo $_filter->getHtml() ?>
</dd>
<!-- add this line start-->
<?php endif; ?>
<!-- add this line end-->
<?php endif; ?>
<?php endforeach; ?>
</dl>
<script type="text/javascript">decorateDataList('narrow-by-list')</script>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
Hope you can get working now !

Setting variables in Magento blocks

I have the following code in my template file:
<?php foreach($collection as $product): ?>
<?php Mage::unregister('product'); ?>
<?php Mage::register('product', $product); ?>
<?php $_product = $this->getProduct(); ?>
<div class="product-view">
<div class="product-essential">
<form action="<?php echo $this->getSubmitUrl($_product); ?>" method="post" id="product_addtocart_form"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
<div class="no-display">
<input type="hidden" name="product" value="<?php echo $product->getId() ?>" />
<input type="hidden" name="related_product" id="related-products-field" value="" />
</div>
<div class="product-shop">
<div class="product-name">
<h1><?php echo $_product->getName(); ?></h1>
</div>
<?php echo $this->getChildHtml('alert_urls') ?>
<?php echo $this->getChildHtml('product_type_data') ?>
<?php echo $this->getTierPriceHtml() ?>
<?php echo $this->getChildHtml('extrahint') ?>
<?php if (!$this->hasOptions()):?>
<div class="add-to-box">
<?php if($_product->isSaleable()): ?>
<?php echo $this->getChildHtml('addtocart') ?>
<?php if( $this->helper('wishlist')->isAllow() || $_compareUrl=$this->helper('catalog/product_compare')->getAddUrl($_product)): ?>
<span class="or"><?php echo $this->__('OR') ?></span>
<?php endif; ?>
<?php endif; ?>
<?php echo $this->getChildHtml('addto') ?>
</div>
<?php echo $this->getChildHtml('extra_buttons') ?>
<?php endif; ?>
<?php if ($_product->getShortDescription()):?>
<div class="short-description">
<h2><?php echo $this->__('Quick Overview') ?></h2>
<div class="std"><?php echo $_helper->productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description') ?></div>
</div>
<?php endif;?>
<?php echo $this->getChildHtml('other');?>
<?php if ($_product->isSaleable() && $this->hasOptions()):?>
<?php echo $this->getChildChildHtml('container1', '', true, true) ?>
<?php endif;?>
</div>
<div class="product-img-box">
<?php echo $this->getChildHtml('media') ?>
</div>
<div class="clearer"></div>
<?php if ($_product->isSaleable() && $this->hasOptions()):?>
<?php echo $this->getChildChildHtml('container2', '', true, true) ?>
<?php endif;?>
</form>
</div>
</div>
<?php endforeach; ?>
In my layout XML I have:
<reference name="root">
<block type="catalog/product_view" name="test" template="path/to/products.phtml"/>
</reference>
I know it is called successfully as I've output static content from it. However, it appears as if
$product
isn't being recognised by the block, even though it is a valid product instance.
The code is lifted straight from default/template/catalog/product/view.phtml where it works fine. All my code does/tries to do is change the product. This is the first time I've worked with blocks and would be grateful for any help/tips/pointers as to where I've gone wrong.
Thanks in advance :-).
EDIT
It turns out my understanding of blocks/Magento was incorrect. The answer lies in reading the AlanStorm tutorials.
I had similar problems, first try replacing all double with single quotes in layout xml.
I hope you just dummy-quoted template="path/to/products.phtml" for presentation here and used real Magento path in your code.
Can't tell you more cause i'm also beginner in Magento. Just few useful links:
http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-1-introduction-to-magento
http://inchoo.net/category/ecommerce/magento/

Set breadcrumb for one of the pages of Magento?

I have this page for instance that doesn't have a proper breadcrumb set: http://www.princessly.com/checkout/cart/
It's just "Home >>" and that's it.
How can I make it "Home >> Shopping Cart"?
Thus far I can only find the breadcrumb template which is template/page/html/breadcrumbs.phtml but I have no idea how to make this change.
I suppose I should add a line in the shopping cart page template?
Try adding the following code to local.xml of your theme:
<checkout_cart_index>
<reference name="breadcrumbs">
<action method="addCrumb">
<crumbName>Home</crumbName>
<crumbInfo><label>Home</label><title>Home</title><link>/home</link></crumbInfo>
</action>
<action method="addCrumb">
<crumbName>Shopping Cart</crumbName>
<crumbInfo><label>Shopping Cart</label><title>Shopping Cart</title><link>/checkout/cart</link></crumbInfo>
</action>
</reference>
</checkout_cart_index>
For some reason sometimes we need to touch the template on code>page>html>breadcrumbs.phtml. Just remember to move before to your template.
<?php if($crumbs && is_array($crumbs)): ?>
<div class="breadcrumbs">
<ul>
<?php foreach($crumbs as $_crumbName=>$_crumbInfo): ?>
<li class="<?php echo $_crumbName ?>">
<?php if($_crumbInfo['link']): ?>
<?php echo $this->__($this->htmlEscape($_crumbInfo['label'])) ?>
<?php elseif($_crumbInfo['last']): ?>
<strong><?php echo $this->__($this->htmlEscape($_crumbInfo['label'])) ?></strong>
<?php else: ?>
<?php echo $this->__($this->htmlEscape($_crumbInfo['label'])) ?>
<?php endif; ?>
<?php if(!$_crumbInfo['last']): ?>
<span>/ </span>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
The translate is performed by
$this->__("someText");

Resources