Selective HTML Blocks on Product Pages in Magento? - magento

I want to display specific HTML blocks on all product pages of a specific category in Magento.
For example:
Categories - dress, phone, gps
HTML blocks for these categories: dress.phtml, phone.phtml, gps.phtml
On all the pages of products in dress, dress.phtml is displayed - dress size charts, color charts, how to wash, etc.
On all the pages of products in phone, phone.phtml is displayed - battery care, how we package the phones, etc.
On all the pages of products in gps, gps.phtml is displayed - GPS usage, installation, etc.
You get the idea. How can I do this in Magento? How can I call different PHTML files (dress.phtml, phone.phtml or gps.phtml) in catalog/product/view.phtml by product category?
Thanks!

This is easily achieved by making a few modificaitions to your catalog/product/view.phtml file and your custom theme's layout/local.xml file.
Let's begin by modifying local.xml. Open it up and type / copy & paste the following code:
<catalog_product_view>
<reference name="content">
<reference name="product.info">
<block type="catalog/product_view" name="product.dress" as="productDress" template="wherever/you/keep/dress.phtml" />
<block type="catalog/product_view" name="product.phone" as="productPhone" template="wherever/you/keep/phone.phtml" />
<block type="catalog/product_view" name="product.gps" as="productGps" template="wherever/you/keep/gps.phtml" />
</reference>
</reference>
</catalog_product_view>
What we're essentially doing by modifying local.xml is making your new templates available to the catalog/product/view.phtml file within the product.info block. The block type is *catalog/product_view* so we have access to all the same information and methods available within catalog/product/view.phtml. We can then write a bit of code in view.phtml to decide what block to output
$_currentCategory = Mage::registry('current_category');
if ($_currentCategory->getId() == id of dress category) {
echo $this->getChildHtml('productDress');
} elseif ($_currentCategory->getId() == id of phone category) {
echo $this->getChildHtml('productPhone');
} elseif ($_currentCategory->getId() == id of gps category) {
echo $this->getChildHtml('productGps');
}
All we're doing here is checking the current category of the product and outputting the associated block. Of course you'll need to replace "id of ___ category" with the appropriate category ID value. This won't work if you're in a child category of one of the categories you'd like to find. For example, if your structure is as follows Phone > Accessories > Phone Charger Product we'll only find the latest category (Accessories) in the Phone Charger Product. There's other ways of finding a parent category which are scattered across Google and StackOverflow.
Hope that helps. I don't have means to test this code unfortunately but it should work. Good luck!

$product->getCategoryIds() can give you the category ids to which the product belong.

Related

List 8 latest products on homepage - Magento 1.9.2.1

I know this question gets asked quite a bit and seems straight forward but I can't seem to find a post which states an answer that works.
There a few with solutions on how to list products marked with a 'new from' date in the backend and there are solutions which list the latest products from a specific category but I just need the 8 most recently added products to the catalog full stop.
Currently I am manually adding the products to a 'latest products' category with an ID of 116
and have this in the content of my home page CMS page:
{{block type="catalog/product_list" name="home.catalog.product.list" alias="products_homepage" category_id="116" template="catalog/product/list-latest.phtml"}}
Then in the list-latest.phtml template file, I have some code to get the collection:
<?php
$_productCollection=$this->getLoadedProductCollection()->setPageSize(10);
$_productCollection->clear(); //this will unset the loaded items.
$_productCollection->getSelect()->limit(10); //set a new limit
$_productCollection->getSelect()->reset(Zend_Db_Select::ORDER); //reset the order
$_productCollection->getSelect()->order('cat_index_position asc');//force order by position in category
$_productCollection->load();
$_helper = $this->helper('catalog/output');
?>
And then it loops through the collection and works fine but I need a way to get the latest products from ALL categories and automatically put the latest 8 products on the homepage.
Is there a way to alter this code to do this?
Please try the following code
Place below code in you home page via admin login cms->select your home page->
design->Layout Update XML
<block type="catalog/product_new" name="home.catalog.product.new" alias="product_new" template="catalog/product/new.phtml" after="cms_page">
<action method="addPriceBlockType">
<type>bundle</type>
<block>bundle/catalog_product_price</block>
<template>bundle/catalog/product/price.phtml</template>
</action>
<action method="setColumnCount"><columns>4</columns></action>
<action method="setProductsCount"><count>8</count></action>
</block>
The above code will show you new product with four columns per row and total 8 product limit.
Let me know if you have any query
I did this using a widget. Create a widget with:
type:Catalog new products list
Display On: where you want it
In the Widget Options tab: Display Type: all products (you can select New Products but then you have to mark products as new rather than the latest you added)
There are other options that are self-explanatory.
You will probably have to theme your widget but it shouldn't be too different to a normal product displays.

Magento search result links to product pages use the wrong template

I've modified my search results page to use the 2columns-right template. But all of my product pages use the 1column template.
When I click on any of the links to products in my Magento search results, the product page is displayed in the left side of the 2columns-right template. Since product pages should use the 1column template, the result looks broken.
The product pages, when linked to from search results, don't seem to retain their heirarchy in the url, either. They appear as standalone pages instead of pages within the catalog. I'm guessing that this is part of the problem.
Is there any way to retain the page heirarchy in search results? Or any way to get the different types of pages that may show up in search results to use their respective templates?
I solved it by editing this block in layout/catalog.xml:
<catalog_product_view translate="label">
<label>Catalog Product View (Any)</label>
<!-- Mage_Catalog -->
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
I just changed the template there & now the results look correct.

Magento : sort by category problems?

When I have products in categories ( I have sub-categories in this category) I don't have sort by category just sort by price. I want to add sort by category with sort by price.
For example :
if i have computer category and i have laptop and Desktop sub-categories. I have many products also in computer category. When i am in computer catalog i wish to show also in navigation layer :
Sort by
Category :
Laptop(1)
Desktop(2)
What is the problem ?
Magento base theme provides this feature by default
using these 2 files
base/default/layout/catalog.xml
<catalog_category_default translate="label">
<reference name="left">
<block type="catalog/navigation" name="catalog.leftnav" after="currency" template="catalog/navigation/left.phtml"/>
</reference>
....
</catalog_category_default>
And base/default/template/catalog/navigation/let.phtml and of course there is Block class to provide data.
To use this feature your product should belong to child as well parent category or your parent category's display setting Is Anchor should be set "Yes", then it will display all products including products in children categories as well with category filter.

Showing custom static block on the custom page Magento

I have a some question about Magento cms static block showing on the custom page.
For example, I have 3 static block (block 1, block 2, block 3) and I need to display block 1 at the Category 1 and subcategories 1, block 2 at the Category 2 and subcategories 2, and block 3 at other pages (home, about, etc)
I try to use Mage::app()->getFrontController()->getRequest()->getRequestUri()
But I received request like "category1.html" and if we going to subcategory of this category - block is changed to default.
If use Mage::app()->getFrontController()->getRequest() I received "catalog/category/view/id/id_number"
I really don't understand how to solve this issue.
Thanks for answer!
You can add a block to a specific part of the page, for a specific category using the Custom Layout Update functionality.
NOTE: If you have a custom theme, the reference name for the footer could be different. This method has been tested to work on the Modern theme included with Magento
Go to Catalog > Manage Categories
Choose the category you want to assign your block to.
Go to the Custom Design tab.
Set Use Parent Category Settings to No
In the Custom Layout Update, insert the following XML
<reference name="bottom.container">
<block type="cms/block" name="my_footer_block">
<action method="setBlockId">
<block_id>my_footer_block</block_id>
</action>
</block>
</reference>
Replace my_footer_block with the Identifier (block_id) of your static block.
Clear your Magento Caches under System > Cache Management and refresh the Category page.
If this does not work, the reference name may not be correct for the theme you are using. You can check the reference name by looking under app/design/frontend/[THEME PARENT]/[THEME CHILD]/layout/page.xml and searching for page/html_footer within the file.
In the file, you will find something like this:
<block type="page/html_footer" name="footer" as="footer" template="page/html/footer.phtml">
<block type="page/html_wrapper" name="bottom.container" as="bottomContainer" translate="label">
<label>Page Footer</label>
<action method="setElementClass"><value>bottom-container</value></action>
</block>
<block type="page/template_links" name="footer_links" as="footer_links" template="page/template/links.phtml"/>
<block type="page/switch" name="store_switcher" as="store_switcher" template="page/switch/stores.phtml"/>
</block>
<block type="core/text_list" name="before_body_end" as="before_body_end" translate="label">
<label>Page Bottom</label>
</block>
</block>
Note the name attribute for the page/html_wrapper block. This is the name references used in the code provided in step Step 5. If it's different than bottom.container, change bottom.container to match what is in your page.xml file.
this might help you
Strategy : Check current page is whether current category is available on current page or not, If available then you can check it's level and depending on that you can show you block
Solution :
$_current_category=$this->getCurrentCategory();
1)get current category and if category not available then show default block
2)if category found get it's level $_current_category->getLevel() and then you can place your block accordingly
Override catalog.xml in you local module
Add in
in content reference
in you catalog/category/voew.phtml
add this code
$_current_category=$this->getCurrentCategory();
if(cond == '1st category') {
echo $this->getChildHtml('block1');
}
Similarly for other blocks
You can simply assign a static block to specific categories using the built in functionality Magento provides.
Go to Catalog > Manage Categories
Click the category you want to assign the block to on the left.
Go to the Display Settings tab
Set Display Mode to either Static block only or Static block with products.
Set CMS Block to the static block you want to display on this category.
Click the Save Category button.
Repeat this step for different categories. You can choose a unique static block, or assign the same one to multiple categories this way.

Hide toolbar elements on specific Magento category page

I have a custom homepage on my Magento store which displays the latest 150 added products, with the most recent first.
The paging on the toolbar is needed here (30 per page, 5 pages) but the "sort by" and the "Show" number to display are both irrelevant.
The page is constructed using Tridian New Arrivals code (google it), with a minor tweak to sort the collection by entity_id descending, as the created_at sort (suggested in the article) seems to not work correctly.
So this is basically a home page redirect to a category page, which has an over-ride in place to display a specific product collection (the new arrivals).
Could someone show me how to hide those two select lists on just this page please?
Cheers,
Rob
Theoretically this is possible with a layout update in the category design, but it has not been so easy:
http://www.magentocommerce.com/boards/v/viewthread/46823/#t331000
You can create a new theme that consists of just the files needed to be changed to get rid of the toolbar (hacked phtml file) and then set the custom design for this category to this theme. It will use your default theme to fill in the gaps, e.g. header, footer etc.
I resolved this in code, in toolbar.phtml, as follows:
First I set a current category variable at the start of the code (mine is at line 36).
<?php
if (Mage::registry('current_category') != NULL) {
$curr_category = Mage::registry('current_category')->getName();
$curr_category = strtolower($curr_category);
} else {
$curr_category = '';
}
?>
Then I test the variable value in an IF statement around the code blocks that build and output the select lists.
<?php if($curr_category != 'new arrivals'): ?>
--- Select list code here ---
<?php endif; ?>
I hope that helps someone!
Cheers!
I did this and it worked for me. Copy /app/design/frontend/base/default/template/catalog/product/list/toolbar.phtml and put it in your /app/design/frontend/default/[your template]/template/catalog/product/list/ as toolbar_new.phtml .
Create a cms page and add this to the layout update :
<reference name="content">
<block type="catalog/product_list" name="product_list" template="catalog/product/ga_gallery.phtml">
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbarnosort.phtml">
<block type="page/html_pager" name="product_list_toolbar_pager"/>
</block>
<action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
<action method="setCategoryId"><category_id>76</category_id></action>
</block>

Resources