我有2个自定义属性,我想添加到<title>产品页面上的标签.它们是"品牌"和"副标题".
我的页面标题最终会是这样的:
$ brand."".$ productname."".$ subtitle;
我怎样才能做到这一点?
非常感谢帮助.
Dre*_*ter 22
从您的问题来看,我假设您指的是更改产品的元标题.
有3个选项可供选择:
您的决定实际上是在选项2和3之间(这两个选项都需要您创建自定义模块才能实现).
在扩展Magento核心功能时,我总是尽量不引人注目 - 所以我会在这里选择选项3.请参阅以下代码以获取完整示例:
应用程序的/ etc /模块/ Yourcompany_Yourmodule.xml
<?xml version="1.0"?>
<config>
<modules>
<Yourcompany_Yourmodule>
<active>true</active>
<codePool>local</codePool>
</Yourcompany_Yourmodule>
</modules>
</config>
Run Code Online (Sandbox Code Playgroud)
应用程序/代码/本地/ 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>
Run Code Online (Sandbox Code Playgroud)
应用程序/代码/本地/ Yourcompany/Yourmodule /型号/ 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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是在 magento 中覆盖产品页面 META 逻辑的最简单和最好的方法
将/app/code/core/Mage/Catalog/Block/Product/View.php复制 到/app/code/local/Mage/Catalog/Block/Product
并覆盖函数 _prepareLayout(),我的例子如下
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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16367 次 |
| 最近记录: |