magento show tab if content exists - magento

i'm using the tab.phtml from the modern theme to create product tabs, however i've switched this to using jquery and jquery-ui as i needed to link to a tab directly.
So in a nut shell the code is pretty much the same as the one found in the modern theme.
<div id="tabs">
<ul>
<?php foreach ($this->getTabs() as $_index => $_tab): ?>
<?php if($this->getChildHtml($_tab['alias'])): ?>
<li class="<?php echo !$_index?' active first':(($_index==count($this->getTabs())-1)?' last':'')?>"><?php echo $_tab['title']?></li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<div class="clearer"></div>
<?php foreach ($this->getTabs() as $_index => $_tab): ?>
<?php if($this->getChildHtml($_tab['alias'])): ?>
<div class="product-tabs-content" id="<?php echo $_tab['alias'] ?>"><?php echo $this->getChildHtml($_tab['alias']) ?></div>
<?php endif; ?>
<?php endforeach; ?>
</div>
and i'm adding the custom tabs using the same method under catalog.xml:
<action method="addTab" translate="title" module="catalog"><alias>how-to-use</alias><title>How to Use</title><block>catalog/product_view</block><template>catalog/product/view/how-to-use.phtml</template></action>
however i've noticed that the tab 'upsells' only appears when there are upsell products assigned. I'm wanting to use this same functionality to display a custom product attribute if there is content to display.
So what i'm asking is how does the upsell detect that there are no products assigned so no tab is displayed so i can do this for my custom tab. my custom tab phtml file looks like this:
<?php $_howtouse = $this->getProduct()->getHowToUse(); ?>
<?php if ($_howtouse): ?>
<div class="std">
<?php echo $this->helper('catalog/output')->productAttribute($this->getProduct(), $_howtouse, 'howtouse') ?>
</div>
any help greaty received thanks :)

The first line of code in upsell.phtml controls the appearance:
<?php if(count($this->getItemCollection()->getItems())): ?>
I am speculating your code is simply evaluating to true and always showing that section. What is your output from $this->getProduct()->getHowToUse() ?

It turned out i had 1 line above my:
<?php $_howtouse = $this->getProduct()->getHowToUse(); ?>
which meant it registered as there being some content all be it white space.
got rid of the whitespace it now works.

Related

How I get product options block in catalog

I did an quick view in product catalog (list.phtml) using Magento 1.7, but I'm unable to get the FULL product options block so that it is identical to the product page (showing select, radio button, select multiple, etc.). Can someone help me?
*Sorry for my bad English.
I tried put the view.phtml' code, but this don't work.
I also tried some tutorials that I found on google, but what worked converted all options in select, but I need to get entire block.
...
<?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 endif; ?>
<?php echo $this->getChildHtml('other');?>
<?php if ($_product->isSaleable() && $this->hasOptions()):?>
<?php echo $this->getChildChildHtml('container1', '', true, true) ?>
<?php endif;?>
...

Display specific category products on no results search page

When we search for a wrong keyword like "sdfsdf" in magento site, it displays that "your search returns no results". Here I want to display any category products like "similar products" category as we display "best sellers" on home page. I have tried by calling the block in the catalogsearch.xml. BUt catalogsearch.xml doent contain any block for no results. So how can I display any category products on no results page.
I have an idea like can we display a specific category products on .phtml page? If we can display specific category of products then we can call that category from "result.phtml". Any help?
my result.phtml
<?php if($this->getResultCount()): ?>
<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<div class="page-title category-title">
<?php if ($this->helper('rss/catalog')->getTagFeedUrl()): ?>
<?php echo $this->__('Subscribe to Feed') ?>
<?php endif; ?>
<h1><?php echo ($this->getHeaderText() || $this->getHeaderText() === false) ? $this->getHeaderText() : $this->__("Search results for '%s'", $this->helper('catalogsearch')->getEscapedQueryText()) ?></h1>
</div>
<?php if ($messages = $this->getNoteMessages()):?>
<p class="note-msg">
<?php foreach ($messages as $message):?>
<?php echo $message?><br />
<?php endforeach;?>
</p>
<?php endif; ?>
<?php echo $this->getProductListHtml() ?>
<?php else: ?>
<div class="page-title category-title">
<h1><?php echo ($this->getHeaderText() || $this->getHeaderText() === false) ? $this->getHeaderText() : $this->__("Search results for '%s'", $this->helper('catalogsearch')->getEscapedQueryText()) ?></h1>
</div>
<p class="note-msg">
<?php echo ($this->getNoResultText()) ? $this->getNoResultText() : $this->__('Your search returns no results.') ?>
<?php if ($messages = $this->getNoteMessages()):?>
<?php foreach ($messages as $message):?>
<br /><?php echo $message?>
<?php endforeach;?>
<?php endif; ?>
</p>
<div class="search-noresults">
<h1>Meanwhile, You may go through our featured categories:</h1>
</div>
<?php echo $this->getLayout()->CreateBlock('catalog/product_list')->setCategoryId(18)->setTemplate('catalog/product/list.phtml')->toHtml();?>
<?php endif; ?>
A simple solution is within catalogsearch/result.phtml template you would need to setup what happens when there are no results
<?php if($this->getResultCount()): ?>
SHOW RESULTS
LEAVE DEFAULT
<?php else: ?>
NO RESULTS
<?php echo $this->getLayout()->createBlock('catalog/product_list')->setCategoryId(4)->setTemplate('catalog/product/list.phtml')->toHtml() ?>
<?php endif; ?>

Magento get categories and list in navigation in .phtml file

Ok so I've copied and pasted like 10 inserts already for php code to list the main categories or the sub categories under the Default root category.
None of them are displaying my categories and I have all my categories as root categories AND sub-categories under the Default root category to see if both scenarios will work..
Here is my php code I have in my theme .phtml file (1-column so far)
<?php $_categories = Mage::helper('catalog/category')->getStoreCategories(); ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
I know I have them setup right in the admin panel because I have them both as root and subcategories which shouldn't be necessary of course. I'm just trying everything to get them to display and nothing seems to be working..
Anyone know?
EDIT
Ok I found it in topmenu.phtml in the html directory under the template directory in the page directory:
<?php $_menu = $this->getHtml('level-top') ?>
<?php if($_menu): ?>
<div class="nav-container">
<ul id="nav">
<?php echo $_menu ?>
</ul>
</div>
<?php endif ?>
Where is the getHtml('level-top') code at so I can alter the markup of the li elements?
For change markup, you need rewrite protected _getHtml method in Mage_Page_Block_Html_Topmenu class

Magento Add url link to open a specific product tab

Magento 1.7.0.0
ok i'm using the product tabs that are available in the modern theme. I've added a custom tab to add reviews as a tab [sucess].
Now where it says:
'Be the first to review this product'
i want this link to go to the tab on that page and not go off to the reviews page.
i realise i need to some javascript but it's just i can't work out how to call a tab.
any ideas?
thanks.
Andy.
if you already using jQuery... you could just simply "hack" it out like this
jQuery(document).ready(function($){
$("#addreview").attr("href", "#review-form");
$("#addreview").click(function(){
$(".product-tabs").children("li").removeClass("active");
$("#product_tabs_reviews").addClass("active");
$(".product-tabs-content").css("display", "none");
$("#product_tabs_reviews_contents").css("display", "block");
});
});
be aware to change the selector according to your own markup
My code looks like this in tabs.phtml:
<div id="tabs">
<ul>
<?php foreach ($this->getTabs() as $_index => $_tab): ?>
<?php if($this->getChildHtml($_tab['alias'])): ?>
<li class="<?php echo !$_index?' active first':(($_index==count($this->getTabs())-1)?' last':'')?>"><?php echo $_tab['title']?></li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<div class="clearer"></div>
<?php foreach ($this->getTabs() as $_index => $_tab): ?>
<?php if($this->getChildHtml($_tab['alias'])): ?>
<div class="product-tabs-content" id="<?php echo $_tab['alias'] ?>"><?php echo $this->getChildHtml($_tab['alias']) ?></div>
<?php endif; ?>
<?php endforeach; ?>
</div>
and then adding:
jquery-ui-tabs.js and the relevant .css

Magento displays subcategories description on category list.phtml

I got a category page with list of subcategories in Magento. The list has an image and name, but I also want to display those subcategories' description. I tried simply to add
<strong><?php echo $this->htmlEscape($_category->getDescription()) ?></strong>
but it's not working.
EDIT: I get subcategories in a traditional way:
<?php if (!$_categoryCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no subcategories matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
<?php $_collectionSize = $_categoryCollection->count() ?>
<?php $_columnCount = $this->getColumnCount(); ?>
<?php $i=0; foreach ($_categoryCollection as $_category): ?>
<?php if ($i++%$_columnCount==0): ?>
<ul class="products-grid">
<?php endif ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<a href="<?php echo $_category->getUrl() ?>" class="product-image"><img class="photo" src="<?php echo $this->getCategoryImage($_category, 214, 184); ?>" width="214" height="184" alt="<?php echo $_category->getName() ?>" />
<strong><?php echo $this->htmlEscape($_category->getName()) ?></strong>
<strong><?php echo $_category->getDescription() ?></strong>
</a>
</li>
<?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
</ul>
<?php endif ?>
<?php endforeach ?>
</div>
I've tried to update the public function getChildrenCategories($category) in the category.php file by adding ->addAttributeToSelect(’description’), but it's not working.
I can't see what exactly it is that you're doing wrong, but perhaps I can still help. I have successfully displayed the child category descriptions in list.phtml and you may be able to adapt what's working for me to your own purposes. The following is a stripped-down version of the code which works for me:
<?php $children = explode( ",", $this->getCurrentCategory()->getChildren() ); ?>
<div class="category-products">
<ul class="products-grid">
<?php foreach( $children as $child ): ?>
<?php $_child = Mage::getModel( 'catalog/category' )->load( $child ); ?>
<li class="item"><?php echo $_child->getDescription(); ?></li>
<?php endforeach; ?>
</ul>
</div>
The big difference between what you're doing and my sample above is that the getChildren() method on the catalog model object returns an array of category Ids and I then use the category Ids to load the corresponding child category model instances. My memory may be wrong here, but I seem to remember that the items returned from a Magento collection don't contain the full data that you get when you load by id.
I'm not sure if this will affect performance significantly or not (I would assume that loading a collection is faster than loading individual models) but it works so I'm not complaining...
Hope this helps.
Cheers,
Zac
I have relatively the same idea on the website I'm working on, where I display sub-categories in a grid view. While I used to use the method of individually loading category/product information by id's, I sort of fell in love with using the "Mage::getModel('')->getCollection()" method.
This is what I've used on my sub categories, and I've been quite happy with it, as it grabs all the information at once:
<?php
if(Mage::registry( 'current_category' )->getId()) {
$_currentCategoryId = Mage::registry( 'current_category' )->getId();
} else { //Get Root Category Id if not in a category
$_currentCategoryId = Mage::app()->getStore()->getRootCategoryId();
}
$_subCategories = Mage::getModel( 'catalog/category' )->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('parent_id',array('eq' => $_currentCategoryId))
->addFieldToFilter('include_in_menu',array('eq' => '1'))
->addFieldToFilter('is_active', array('eq' => '1'))
->addAttributeToSort('position', 'asc');
?>
<?php if(count($_subCategories) > 0): ?>
<!--This is where the sub-category layout would go.-->
<?php else: ?>
<!--Do something else if there are no sub-categories.-->
<?php endif; ?>
This will grab all visible sub-categories of the current category, OR grab the base categories (from Root Category Id) of the store, if you choose to have the template show on any other page. You could go further and define specific attributes with addAttributeToSelect as well.

Resources