Magento Change Product Page Titles to Include Attributes(Magento 更改产品页面标题以包含属性)
问题描述
我想将 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;
}
}
这篇关于Magento 更改产品页面标题以包含属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Magento 更改产品页面标题以包含属性
基础教程推荐
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
