Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

php - Magento Change Product Page Titles to Include Attributes

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;

How can I achieve this?

Help is much appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

There are 3 options open to you:

  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.

Your decision is really between options 2 & 3 (both of which will require you to create a custom module to achieve).

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

<?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

<?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;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...