Changing layout/css of product detail page dynamically - magento

I am in a situation where I need to have a different layout for a specific Product Type.
It'll be okay if I could load a different CSS for this Product Type.
Is anything like this possible through code/xml?
I am able to identify the product type in catalog/product/view.phtml, will it be helpful?
I also noticed a node in catalog.xml named <PRODUCT_TYPE_grouped> or <PRODUCT_TYPE_configurable> etc.
will something like this help!?

Your idea is a valid one, something like this should be possible:
<PRODUCT_TYPE_grouped>
<reference name="head">
<action method="addCss"><stylesheet>css/your_file.css</stylesheet></action>
</reference>
</PRODUCT_TYPE_grouped>
Take a look at page.xml to see what other sorts of tricks the same block can perform.

You want to look at catalog.xml not page.xml for product type.
This is if you are using magento 1.7 and up.

Related

Magento - Change Product Page Layout for only one Product in the define Layout Section

This is for sure a quicky, but nothing i found so far is working.
I would like to set a different product view for a choosen Product only. Not for a category. I tried to add this to the custom layout tab without any luck so far.
I tried any variantions of this:
<reference name="product.info">
<action method="setTemplate"><template>catalog/product/newview.phtml</template></action>
</reference>
But Magento will not read it!
Is there any easy solution to this ? Or maybe how i can get the template added to the layout dropdown in the Design Tab of the Product ? I mean the Tab where you can choose between 1-2 or 3 Columns Layout.
Thank you very much for your help. I am using Magento 1.9.1.
In your local.xml file put the following,
<layout>
...
<PRODUCT_86>
<reference name="root">
<action method="setTemplate"><template>catalog/product/newview.phtml</template></action>
</reference>
</PRODUCT_86>
...
</layout>
Here 86 is a product_id. Flush the cache and then check.
The Solution is quite easy: Make the new File readable by the Webserver.
For what its worth, maybe it helps someone.

Magento all products set to noindex

this is not so much a development question, rather something (hopefully) to set in the backend. I want to set all my products (all configurable ones) within magento 1.7.0.2 to the meta-tag "noindex,nofollow".
But doing that for 450 products one after another is quite heavy. Isn't there an easier way to set the default value differently or to use phpmyadmin therefore?
Thanks
So, for my thing this process worked out well:
1: Go to System > General > Design > Robots
And set all pages globally to noindex,nofollow
2. Then - in my scenario - if you want to have categories though listed, then do that to the panel "Own design" exactly like #pankijs mentioned:
<reference name="head">
<action method="setRobots"><value>INDEX,FOLLOW</value></action>
</reference>
For setting individual products now to a different robots-tag, you can go to Product > Meta Information and edit stuff within the dropdown-box there.
you can edit your templates local.xml file to
<catalog_product_view>
<reference name="head">
<action method="setRobots"><value>NOINDEX,FOLLOW</value></action>
</reference>
</catalog_product_view>
for only configurable product use:
<PRODUCT_TYPE_configurable>
</PRODUCT_TYPE_configurable>
this will add meta tag you need for all products.
Maybe something like this would give you more flexibility:
http://inchoo.net/magento/per-product-meta-robots-tag-control-in-magento/
Then you could just write a simple script/query to set all (or a subset) products to NOINDEX,NOFOLLOW.

Magento: show custom attribute in body class

I have a custom category attribute that i want to add to the body class. As far as I could find out what people do is
Override the CategoryController and add something like $root->addBodyClass($category->getMyAttribute()); But I don't want to override core classes...
In the admin panel they add something like <reference name=”root”><action method=”addBodyClass”><className>caravan-motorhome-lighting</className></action></reference> to each and every category not using the attribute itself but adding the class directly. As I already have an attribute, I surely don't want do clone it and add the class this way.
So what my favourite solution would be is some layout update I can add to the local.xml that says
<reference name=”root”>
<action method=”addBodyClass”>
<className>
get value of my custom attribute here dynamically
</className>
</action>
</reference>
Does anyone have an idea how this could work or another idea that I didn't even think of?
You can use a really cool feature of Magento layout XML to achieve this. You'll need a module to achieve it. Either create a module specifically for this or use a theme module if you have one — this is up to you to decide what you think is best.
I'll show you an example where I'll add a class containing the ID of the category to the body tag:
In my layout XML, I'm going to add via the catalog_category_default handle. This way, I can use Mage::registry('current_category') later to retrieve the current category. So, in your layout XML do something similar to this:
<catalog_category_default>
<reference name="root">
<action method="addBodyClass">
<className helper="mymodule/my_helper/getCategoryClass" />
</action>
</reference>
</catalog_category_default>
This attribute is the important part: helper="mymodule/my_helper/getCategoryClass". This is equivalent to calling Mage::helper('mymodule/my_helper')->getCategoryClass(); in code.
Whatever is returned from that function will be used as the value for the <className> node. You may want to use a different helper that you deem more appropriate, this is up to you to decide.
Carrying on the with the example, here's the function:
public function getCategoryClass() {
return 'category-id-' . Mage::registry('current_category')->getId();
}
You'll want to change the code so that it retrieves the value of your attribute. e.g getMyAttribute() on the category returned by Mage::registry('current_category').
Also, you'll need to ensure that the return is something that is suitable as a CSS class. In this example we don't need to do anything as the ID will always be just number which will be appended to category-id-. If the value of your attribute is not always going to be safe you might want to consider using something like this

Where can I find reference names to find out where to put my block?

For example, I want to put my block on the product page before the description block. How would I find these reference names?
If I understand correctly, based on your question and subsequent comments, you are looking for a way to position blocks before other blocks in the layout?
If so, the layout system provides you with before and after block attributes, for this exact purpose.
The general use is to add a before or after attribute containing the block you would like to position - before or after.
Before Example
<block type="yourmodule/block_type" name="yourblock" before="the_block_name_to_position_before" />
After Example
<block type="yourmodule/block_type" name="yourblock" after="the_block_name_to_position_before" />
Positioned before product description
In your particular scenario, where you would like a block to be positioned before the product description block, you need a little extra xml due to how the product description block is being included into the layout:
<!-- file: app/design/frontend/your_package/your_theme/layout/local.xml -->
<catalog_product_view>
<reference name="product.info">
<block type="yourmodule/block_type" name="yourblock" template="yourmodule/template.phtml" before="product.description">
<action method="addToParentGroup"><group>detailed_info</group></action>
</block>
</reference>
</catalog_product_view>
The important thing to note here though, in terms of you original question, is the use of the before attribute for positioning.
In the Magento Control Panel set your Current Configuration Scope to a specific website. Then, under System -> Configuration -> Advanced -> Developer -> Debug` turn on Template Path Hints and Add Block Names to Hints.
After that, when you reload the page it will tell you what blocks to override to get the desired effect.

Magento Store - Remove Block using Update XML

I am using this code in my template file to display a static block in my left sidebar:
<?= $this->getLayout()->createBlock('cms/block')->setBlockId('leftSB1')->toHtml() ?>
I would like to exclude the block from one of my CMS pages. How do I do this?
I think it requires adding code to the 'Layout Update XML' section but I'm not sure what exactly.
Someone else can correct me here, but I'm fairly sure that you're going to have trouble trying to accomplish this given the way you called the block. Normal layout updates allow you to remove blocks, but those are blocks that were also created with the layout (e.g. the Layout object knows about them after you call loadLayout()).
In your case, you create the block on the fly and then immediately use it to echo some HTML. If you want to be able to delete it with layout updates, try moving it into the layout files first, then use the normal layout block removal method:
<reference name="your_parent_block_name">
<remove name="leftSB1"/>
</reference>
Otherwise, you could hide it either in the PHP (By setting some global variable and checking it before outputting the block. Poor form but it might work.) or in CSS. Let me know if any of these work for you.
Thanks,
Joe
Include the block in your layout instead:
<cms_page>
<reference name="left">
<block type="cms/block" name="leftSB1">
<action method="setBlockId"><id>leftSB1</id></action>
</block>
</reference>
</cms_page>
And then $this->getChildHtml('leftSB1') in your sidebar, if you're not including children automatically.
(and then remove it from the particular page as in the previous answer)

Resources