In Magento: get static block if product has specific attribute - magento

I am trying to call the cms block preorder_note IF the product has a value (doesn' matter what value) for the attribute preorder_note. But i cannot get it to work.
This is done on page template/catalog/product/view.phtml
<?php if ($_product->getAttribute('preorder_note')): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('preoder_short')->toHtml() ?>
<?php endif; ?>
What am I doing wrong?

I found the solution myself.
<?php $_howtouse = $this->getProduct()->getPreorder_note(); ?>
<?php if ($_howtouse): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('preoder_short')->toHtml() ?>
<?php endif; ?>

Related

Display all the Post Categories in Magento Fishpig

I've two Post Categories with two different layouts, But now both are displaying in the same view.phtml. I need to create a check in which category the post belongs and display the style accordingly.
By using below method, I can load a single category with ID 2.
<?php $test = Mage::getModel('wordpress/term')->load(2);?>
Is there any way to load all the post categories.?
Shyam is almost there. Here is a slightly cleaner version of the code:
<?php $categories = $post->getTermCollection('category') ?>
<?php if (count($categories) > 0): ?>
<?php foreach($categories as $category): ?>
<?php if ((int)$category->getId() === 1): ?>
// Category ID #1
<?php elseif ((int)$category->getId() === 2): ?>
// Category ID #2
<?php else: ?>
// All other categories
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
By this method, you can split posts according to category and display in the same view.phtml with different layouts, for adding different layouts paste your code inside the if($getCategory == cat_id) section as I mentioned below.
<?php $categories = $post->getTermCollection('category') ?>
<?php if (count($categories) > 0): ?>
<?php foreach($categories as $category): ?>
<?php
$getCategory = $this->escapeHtml($category->getId());
echo "Get cat: ".$getCategory;
if($getCategory == 2)
{
//your code here
}
if($getCategory == 3)
{
//your code here
}
<?php endforeach; ?>
<?php endif; ?>

Magento: How to show custom attribute on product page below description

I have a custom attribute for products in which I add a video URL.
I made this Embed video responsive using css.
Now I want to call the custom attribute on the product page, so it shows the video.
The file responsible for this is: description.phtml
I've tried the following:
?>
<?php $_description = $this->getProduct()->getDescription(); ?>
<?php if ($_description): ?>
<h2><?php echo $this->__('Details') ?></h2>
<div class="std">
<?php echo $this->helper('catalog/output')->productAttribute($this->getProduct(), $_description, 'description') ?>
</div>
<div class="std">
<?php echo $_product->getAttributeText('video') ?>
</div>
<?php endif; ?>
But I get the error:
Fatal error: Call to a member function getAttributeText() on a non-object in /data/web/public/app/design/frontend/base/default/template/catalog/product/view/description.phtml on line 40
The video does show however. I'm probably doing this all wrong. Can I fix this with a simple edit of the code, or do I have to use an entirely different approach?
Thanks.
<?php echo $this->getProduct()->getAttributeText('video'); ?>
Try this. Or on top of document add this
<?php $_product = $this->getProduct(); ?>

Displaying posts from specific categories with the AheadWorks blog extension for Magento

I'm using the AheadWorks blog extension for Magento, and my blog pages are working fine. But I want excerpts of my latest posts from certain categories to appear on my home page. I've successfully setup everything thus far by adding:
<block type="blog/blog" name="blog.latest" template="aw_blog/blog-home.phtml" />
to "layout.xml", by adding:
<?php echo $this->getChildHtml('blog.latest') ?>
to my home page's phtml file, and by creating "template/aw_blog/blog-home.phtml".
The problem is that I can't figure out how to limit what categories are shown. For example, you'll see in my "blog-home.phtml" file below that I'm trying to limit the posts to the "news" category. I've tried lots of solutions from other forums, but no matter what I do, I see posts from every category. Does anyone know what I need to add/take away from my code to limit the categories?
<?php $posts = $this->getPosts("news"); ?>
<div id="messages_product_view">
<?php Mage::app()->getLayout()->getMessagesBlock()->setMessages(Mage::getSingleton('customer/session')->getMessages(true)); ?>
<?php echo Mage::app()->getLayout()->getMessagesBlock()->getGroupedHtml(); ?>
</div>
<?php $numberOfPosts = 1 ?>
<?php $renderedPosts = 0 ?>
<?php foreach ($posts as $post): ?>
<div class="postWrapper">
<div class="postTitle">
<h2><a href="<?php echo $post->getAddress(); ?>" ><?php echo $post->getTitle(); ?></a></h2>
</div>
<div class="postContent"><?php echo $post->getPostContent(); ?></div>
<?php echo $this->getBookmarkHtml($post) ?>
<div class="tags"><?php echo $this->getTagsHtml($post) ?></div>
<div class="postDetails">
<?php if ($this->getCommentsEnabled()): ?>
<?php echo $post->getCommentCount(); ?> <a href="<?php echo $post->getAddress(); ?>#commentBox" >Comments</a> |
<?php endif; ?>
<?php $postCats = $post->getCats(); ?>
<?php echo "<h1>" . $postCats[2] . "</h1>"; ?>
<?php if (!empty($postCats)): ?>
<?php echo Mage::helper('blog')->__('Posted in'); ?>
<?php foreach ($postCats as $data): ?>
<?php echo $data['title']; ?>
<?php endforeach; ?>
<?php else: ?>
<?php endif; ?></div>
<?php $renderedPosts ++ ?>
<?php if ($renderedPosts = $numberOfPosts) {
break;
}
?>
</div>
<?php endforeach; ?>
<?php //$this->getPages(); ?>
Following is the quick solution:
Go to aw_blog frontend template, default might be this app/design/frontend/base/default/template/aw_blog/menu.phtml
check and replace:
if ($posts = $this->getRecent():
with
$currentBlogCat = Mage::getSingleton('blog/cat');
if ($posts = $this->getRecent($currentBlogCat['cat_id'])):
Now open up /app/code/community/AW/Blog/Block/Menu/Sidebar.php
check for the below function:
public function getRecent()
add a parameters to it:
public function getRecent($currentCat=false)
Also, replace the following code block
$collection = clone self::$_collection;
if (array_key_exists('categories',$data = $this->getData();) && count(explode(',', $data['categories'])) == 1) {
with
$collection = clone self::$_collection;
$data = $this->getData();
if($currentCat>0)$data['categories']=$currentCat;
# Category fix #
if (array_key_exists('categories',$data ) && count(explode(',', $data['categories'])) == 1) {
Thanks!

Hide certain Magento attribute filters

I have some lines of PHP code that will prevent certain attribute filters to be shown within the layered navigation block (i.e. Price and Category). This way I need to add all the filters that I don't want to be shown by hand which takes a lot of time and isn't the best solution.
What I want is the filter attributes "Price" and "Category" to be shown and if the filtername is different, don't display that filter at all. This way the price and category filters are always displayed, and the other filters get hidden. I don't have to add all the filters by hand which I do not want to show up in the list.
My question is, what needs to change within the PHP code to make it work the way I just described?
<?php $_filters = $this->getFilters() ?>
<?php foreach ($_filters as $_filter): ?>
<?php if($_filter->getItemsCount()): ?>
<?php if($_filter->getName() != "Price" AND $_filter->getName() != "Category"): ?>
<dt><?php echo $this->__($_filter->getName()) ?></dt>
<dd><?php echo $_filter->getHtml() ?></dd>
<?php endif; ?>
<?php endif; ?>
<?php endforeach; ?>
Ah I see. Funnily enough I was working on something virtually identical yesterday and this works for me (I mean to the change to the IF statement);
<?php foreach ($_filters as $_filter): ?>
<?php if (Mage::helper('catalog')->__($_filter->getName()) == 'Price' || Mage::helper('catalog')->__($_filter->getName()) == 'Category'): ?>
<dt><?php echo Mage::helper('catalog')->__($_filter->getName()) ?></dt>
<dd><?php echo $_filter->getHtml() ?></dd>
<?php endif; ?>
<?php endforeach; ?>
Hopefully it will work for you too.

what's those line meaning in magento?

<?php foreach ($this->getChildGroup('detailed_info', 'getChildHtml') as $alias => $html):?>
<div class="box-collateral <?php echo "box-{$alias}"?>">
<?php if ($title = $this->getChildData($alias, 'title')):?>
<h2><span><?php echo $this->escapeHtml($title); ?></span></h2>
<?php endif;?>
<?php echo $html; ?>
</div>
<?php endforeach;?>
<?php echo $this->getChildHtml('product_additional_data') ?>
what's those line meaning in magento?
foreach over each child (that is grouped by name detailed_info with method getchildhtml) and output the data from those blocks
get the html of product_additional_data block
This is the snippet of code from the catalog/product/view.phtml that displays Description, Custom Options, and other detailed product information in the front end view.

Resources