Magento static Blocks is not displaying properly. - php

I have created few static blocks just based on the category Id trying to display different blocks .
The problem being some times the block is displayed while some other times it is not .
I guess there is prob with code ? or the way magneto displays static blocks not sure ?
CODE:
<?php
<?php $_description = $this->getProduct()->getDescription(); ?>
<?php if ($_description): ?>
<div class="std">
<?php echo $this->helper('catalog/output')->productAttribute($this->getProduct(), $_description, 'description') ?>
<?php $category = Mage::getModel('catalog/layer')->getCurrentCategory();?>
<?php if($category->getId()==14): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==15): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==16): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==18): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==19): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==86): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==25): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==13): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==98): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information_fbyz')->toHtml();?>
<?php else: ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information_others')->toHtml();?>
<?php endif; ?>
</div>
<?php endif; ?>
please resolve my problem.

If you put this code in product view page then it it not properly way to get current category in product page..
Used Mage::registry("current_category") instead of Mage::getModel('catalog/layer')->getCurrentCategory();
If you come to category directly then you can get current category id product page.then you used to fetch current product categories ids.
$categoryIds = Mage::registry('current_product')->getCategoryIds();
if(Mage::registry("current_category"))
{
$category = Mage::getModel('catalog/category')
->load(Mage::registry("current_category")->getId());
}elseif(is_array($categoryIds )){
//multiple categories id of product. that way i was taken on list category
$category = Mage::getModel('catalog/category')->load($categoryIds[0]);
}
elseif(!is_null($categoryIds)){
$category = Mage::getModel('catalog/category')->load($categoryIds);
}
else{
//no categoies
}

Related

Notification with alert

I am trying to notify the user for their expired products. This works well. but i am not being able to display the product name in alert which is the variable $product->name.
<?php foreach($productsofuser as $product):?>
<?php if($product->status="Expired")
{
?><script>alert('Product expired!');</script><?php
}
?>
<?php endforeach;?>
Just add the product name to the string:
<?php foreach($productsofuser as $product):?>
<?php if($product->status="Expired")
{
?><script>alert('Product <?php echo $product->name; ?> expired!');</script><?php
}
?>
<?php endforeach;?>
Use echo
<?php foreach($productsofuser as $product):?>
<?php if($product->status="Expired")
{
echo"<script>alert('Product expired!');</script>";
}
?>
<?php endforeach;?>

Magento get CMS pages based on layout

So I'm building a custom breadcrumb nav to list all fundraising options. The pages are using a unique layout called "fundraising_page." Is there a way to grab the pages only if they have the "fundraising_page" layout? So far I have this, which is grabbing every page regardless of the template it is using.
So what I need is only to list the pages that are using the "fundraising_page" template.
<?php $collection = Mage::getModel('cms/page')->getCollection()->addStoreFilter(Mage::app()->getStore()->getId());?>
<?php $collection->getSelect()->where('is_active = 1'); ?>
<ul>
<?php foreach ($collection as $page): ?>
<?php $PageData = $page->getData(); ?>
<?php if($PageData['identifier']!='no-route'){ ?>
<li>
<?php echo $PageData['title'] ?>
</li>
<?php } ?>
<?php endforeach; ?>
Here's some well formatted code and using proper methods.
<?php
$collection = Mage::getModel('cms/page')->getCollection()
->addStoreFilter(Mage::app()->getStore()->getId())
->addFieldToFilter('is_active', 1)
->addFieldToFilter('root_template', 'fundraising_page');
?>
<?php foreach ($collection as $page): ?>
<?php if ($page->getIdentifier() != 'no-route'): ?>
<li>
<?php echo $page->getTitle() ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
Instead of if($PageData['identifier']!='no-route') try
if($PageData['root_template']=='fundraising_page')

Magento PHP if statement "load both blocks" , else load only one?

maybe it is something simple but i am not really good with php
With this code i am able to load the static blocks styled in top of category in Magento and the subcategories in the bottom when the top category has child
What i am trying to do is that when there is no child category it should only load the block
otherwise because of styling it will hold the place that the subcat had position.
Please have a look on my code and maybe you understand:
<?php
$_helper = $this->helper('catalog/output');
$_category = $this->getCurrentCategory();
$_imgHtml = '';
if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '<div class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" /></div>';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
$_catHtml = $this->getChildHtml('catalog.inline.subcat');
?>
<?php if (!Mage::registry('current_category')) return ?>
<?php $_categories = $this->getCurrentChildCategories() ?>
<?php $_count = is_array($_categories)?count($_categories):$_categories->count(); ?>
<?php if($_count): ?>
// load the blocks styled with ul and il backgrounds
<ul class="inline-categories">
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId( $this->getCurrentCategory()->getLandingPage() )->toHtml() ?>
<?php foreach ($_categories as $_category): ?>
<li<?php if($_i == 1):?> class="first"<?php endif ?>><a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>>
<?php echo $this->htmlEscape($_category->getName()) ?></a> (<?php echo $_category->getProductCount() ?>)</li>
<?php endforeach ?>
<?php endif; ?>
</ul>
// here i need something like an "else" because the if($count) allready understand that this is not on count so should only load this that is without style //
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId( $this->getCurrentCategory()->getLandingPage() )->toHtml() ?>
//end of this else thing, now should only load the cms/block alone without anything / end of page we are done...
You can use PHP's if/else statements like this:
<?php if ($_count): ?>
stuff if $_count is non-zero
<?php else: ?>
other stuff if $_count is zero
<?php endif; ?>

Pulling Default WordPress Category Name in Widget

I am trying to add the default category name on my site to a WordPress widget. Is there a way to pull this string (not the ID) automatically? I only have one category so i thought it would be easy, but none of the following are working:
<?php bloginfo('category'); ?>
<?php get_category(); ?>
<?php get_the_category(); ?>
<?php get_category([0]); ?>
<?php get_the_category([0]); ?>
<?php get_category([0]->cat_name); ?>
<?php get_the_category([0]->cat_name); ?>
<?php echo get_category(); ?>
<?php echo get_the_category(); ?>
<?php echo get_category([0]); ?>
<?php echo get_the_category([0]); ?>
<?php echo get_category([0]->cat_name); ?>
<?php echo get_the_category([0]->cat_name); ?>
Any help is appreciated...
UPDATE: 08/01/13 - Amal pushed me onto the right path. I ended up using the following and it worked perfectly...
<?php echo get_cat_name(get_option('default_category'));?>
Try this:
$category = get_category(get_option('default_category'));

Magento Proper way to show Main and one level of sub categories

Just to give you an idea. I hae 3 levels of categories.
Top Level > Sub Categories > Children of Sub Categories
What I don't want to display is the children of the sub categories.
The below code shows every category. (Sorry the paste didn't format very well.)
<?php
// ---- PRODUCT NAVIGATION ---- \\
$nav_obj = new Mage_Catalog_Block_Navigation();
$store_cats = $nav_obj->getStoreCategories();
$current_cat = $nav_obj->getCurrentCategory();
?>
<ul>
<?php foreach ($store_cats as $cat):?>
<?php
$ids = strtolower(str_replace(" ", "_", $cat->getName()));
$ids = str_replace("&", "", $ids);
?>
<li class="parent" id="<?php echo $ids; ?>"><?php echo $cat->getName()?>
<ul>
<?php foreach (Mage::getModel('catalog/category')->load($cat->getId())->getChildrenCategories() as $childCategory): ?>
<li><a href="<?php echo $childCategory->getUrl(); ?>">
<?php echo $childCategory->getName()?></a></li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
I've got a similar thing I'm working on. I'm basically passing over each category and rendering the stuff I want to render:
<?php # get category list ?>
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php foreach($_categories as $_category) : ?>
<?php # turn it into the proper model object ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php echo $_category->getName() ?>
<?php foreach($_category->getChildrenCategories() as $_subCategory) : ?>
<?php echo $_subCategory->getName() ?>
<?php endforeach ?>
<?php endforeach ?>

Categories