Magento更改产品页面标题以包含属性 [英] Magento Change Product Page Titles to Include Attributes

查看:102
本文介绍了Magento更改产品页面标题以包含属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个自定义属性,我想添加到产品页面的<title>标签中.它们是品牌"和字幕".

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;

我该如何实现?

非常感谢您的帮助.

推荐答案

从您的问题开始,我假设您是指更改产品的元标题.

From your question, I assume you are referring to changing the meta title for products.

有3个选项可供您选择

  1. 浏览每种产品并手动更新(或使用电子表格 (并导入)每个产品的元标题.这些值是 编辑产品时在管理区域中可用.
  2. 重写Mage_Catalog_Block_Product_View并覆盖 _prepareLayout()方法,即生成此标记的位置.
  3. 使用观察者并挂接到catalog_controller_product_view事件.
  1. Go through each product and manually update (or use a spreadsheet and import) each product meta title individually. These values are available in the admin area when editing a product.
  2. Rewrite Mage_Catalog_Block_Product_View and override the _prepareLayout() method which is where this tag is being generated.
  3. Use an observer and hook into catalog_controller_product_view event.

您的决定实际上是在选项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更改产品页面标题以包含属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆