问题描述
我想将 2 个自定义属性添加到产品页面上的
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;
$brand." ".$productname." ".$subtitle;
我怎样才能做到这一点?
How can I achieve this?
非常感谢您的帮助.
推荐答案
根据您的问题,我假设您指的是更改产品的元标题.
From your question, I assume you are referring to changing the meta title for products.
有 3 个选项可供您选择:
There are 3 options open to you:
- 浏览每个产品并手动更新(或使用电子表格并单独导入)每个产品元标题.这些值是编辑产品时可在管理区域中使用.
- 重写 Mage_Catalog_Block_Product_View 并覆盖_prepareLayout() 方法,即生成此标签的位置.
- 使用观察者并挂钩到 catalog_controller_product_view 事件.
您的决定实际上是在选项 2 和选项 2 之间3(这两者都需要你创建一个自定义模块来实现).
Your decision is really between options 2 & 3 (both of which will require you to create a custom module to achieve).
在扩展 Magento 核心功能时,我总是尽量不引人注目 - 所以我会在这里选择选项 3.请参阅以下代码以获取完整示例:
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
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
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
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; } }