I have 2 custom attributes I'd like to add to the <title>
tags on product pages. They are 'brand' and 'subtitle'.
My page title would end up something like this:
$brand." ".$productname." ".$subtitle;
How can I achieve this?
Help is much appreciated.
From your question, I assume you are referring to changing the meta title for products.
There are 3 options open to you:
Your decision is really between options 2 & 3 (both of which will require you to create a custom module to achieve).
I always try to be as unobtrusive as possible when extending Magento core functionality - so I would opt for option 3 here. Please see below code for a complete example:
app/etc/modules/Yourcompany_Yourmodule.xml
<?xml version="1.0"?>
<config>
<modules>
<Yourcompany_Yourmodule>
<active>true</active>
<codePool>local</codePool>
</Yourcompany_Yourmodule>
</modules>
</config>
app/code/local/Yourcompany/Yourmodule/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Yourcompany_Yourmodule>
<version>1.0.0</version>
</Yourcompany_Yourmodule>
</modules>
<global>
<models>
<yourmodule>
<class>Yourcompany_Yourmodule_Model</class>
</yourmodule>
</models>
</global>
<frontend>
<events>
<catalog_controller_product_view>
<observers>
<yourmodule>
<class>Yourcompany_Yourmodule_Model_Observer</class>
<method>catalog_controller_product_view</method>
</yourmodule>
</observers>
</catalog_controller_product_view>
</events>
</frontend>
</config>
app/code/local/Yourcompany/Yourmodule/Model/Observer.php
<?php
class Yourcompany_Yourmodule_Model_Observer
{
/**
* Change product meta title on product view
*
* @pram Varien_Event_Observer $observer
* @return Yourcompany_Yourmodule_Model_Observer
*/
public function catalog_controller_product_view(Varien_Event_Observer $observer)
{
if ($product = $observer->getEvent()->getProduct()) {
$title = $product->getData('brand') . ' ' . $product->getData('name') . ' ' . $product->getData('sub_title');
$product->setMetaTitle($title);
}
return $this;
}
}
Here is the simplest and best way to override META logics for product page in magento
copy /app/code/core/Mage/Catalog/Block/Product/View.php to /app/code/local/Mage/Catalog/Block/Product
and override function _prepareLayout(), my example goes bellow
protected function _prepareLayout()
{
$this->getLayout()->createBlock('catalog/breadcrumbs');
$headBlock = $this->getLayout()->getBlock('head');
if ($headBlock) {
$product = $this->getProduct();
$title = $product->getMetaTitle();
// SEO values start
$categoryCollection = $product->getCategoryCollection();
foreach ($categoryCollection as $k) {
$topCategory = Mage::getModel('catalog/category')->load($k->getId());
break;
}
$brand = $product->getResource()->getAttribute('brand')->getFrontend()->getValue($product);
$shortDescription = $product->getShortDescription();
$suffix = Mage::getStoreConfig('design/head/title_suffix');
// SEO values end
if (!$title) {
$title = $product->getName() .' '.$brand. ' - '. $topCategory->getName() . $suffix;
}
$headBlock->setTitle($title);
$keyword = $product->getMetaKeyword();
$currentCategory = Mage::registry('current_category');
if ($keyword) {
$headBlock->setKeywords($keyword);
} elseif($currentCategory) {
$headBlock->setKeywords($product->getName());
}
$description = $product->getMetaDescription();
if (!$description) {
$description = $brand. ' '. $topCategory->getName() .'. '. $shortDescription;
}
$headBlock->setDescription( ($description) );
// var_dump($title);
// var_dump($description);
if ($this->helper('catalog/product')->canUseCanonicalTag()) {
$params = array('_ignore_category'=>true);
$headBlock->addLinkRel('canonical', $product->getUrlModel()->getUrl($product, $params));
}
}
return parent::_prepareLayout();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With