我有两个自定义属性想要添加到产品页面的标签中。它们是“品牌”和“副标题”。</br><div class="h-2"></div>我的页面标题最终会变成这样:
$brand." ".$productname." ".$subtitle;</br><div class="h-2"></div>我该如何实现这一点?</br><div class="h-2"></div>非常感谢您的帮助。
从您的问题中,我认为您是在指更改产品的元标题。
您有三个选择:
您的决策实际上是在选项2和3之间(这两个选项都需要您创建自定义模块才能实现)。
当扩展Magento核心功能时,我总是尽可能不引人注目-因此我会选择选项3。 请参见下面的代码以获取完整示例:
app/etc/modules/Yourcompany_Yourmodule.xml
<?xml version="1.0"?>
<config>
<modules>
<Yourcompany_Yourmodule>
<active>true</active>
<codePool>local</codePool>
</Yourcompany_Yourmodule>
</modules>
</config>
应用程序/代码/本地/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;
}
}
<title>
格式,因此它只删除了“title”这个单词。 - Lucas Scholtenprotected 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();
}
<?php if ($_product = Mage::registry('current_product')) { ?>
<title><?php echo $_product->getData('xyz') . ' ' . $_product->getName(); ?></title>
<?php }else{ ?>
<title><?php echo $this->getTitle() ?></title>
<?php } ?>
请查看 获取Magento属性名称和值。
这是对Drew Hunter答案的补充。
Magento将类别名称包含在标题中,因此正确的解决方案应该是:
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()) {
if ($category) {
$title = $brand . ' ' . $product->getData('name') . ' - ' . $product->getData('category')->getData('name');
} else {
$title = $brand . ' ' . $product->getData('name');
}
$product->setMetaTitle($title);
}
return $this;
}
}
我相信Drew Hunter已经提供了大部分正确的答案,但是我不得不做一些更改才能使它在EE 1.14.2.x上运行:
在config.xml中:
将节点
<yourmodule>...</yourmodule>
更改为><yourcompany_yourmodule>...</yourcompany_yourmodule>
在Observer.php中:
将
public function catalog_controller_product_view(Varien_Event_Observer $observer)
更改为public function catalog_controller_product_view($observer)