Magento将产品页面标题更改为包含属性

时间:2022-10-25 11:06:37

I have 2 custom attributes I'd like to add to the <title> tags on product pages. They are 'brand' and 'subtitle'.

我有两个自定义属性,我想要添加到产品页面上的

标签中。它们是“品牌”和“字幕”。</p>

My page title would end up something like this:

我的页面标题会是这样的:

$brand." ".$productname." ".$subtitle;

美元的品牌。””。productname美元。””。副标题美元;

How can I achieve this?

我如何做到这一点?

Help is much appreciated.

感谢帮助。

5 个解决方案

#1


21  

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

在您的问题中,我假定您是指更改产品的meta标题。

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. 检查每个产品并手动更新(或使用电子表格和导入)每个产品元标题。这些值在编辑产品时可以在管理区域中使用。
  3. Rewrite Mage_Catalog_Block_Product_View and override the _prepareLayout() method which is where this tag is being generated.
  4. 重写Mage_Catalog_Block_Product_View并重写_prepareLayout()方法,该方法就是生成标记的地方。
  5. Use an observer and hook into catalog_controller_product_view event.
  6. 使用一个观察者并将其挂接到catalog_controller_product_view事件。

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

您的决定实际上是在选项2和选项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:

在扩展Magento核心功能时,我总是尽量不引人注目,所以我选择这里的选项3。请参阅下面的代码,以获得完整的示例:

app/etc/modules/Yourcompany_Yourmodule.xml

应用程序/ etc /模块/ 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

应用程序/代码/地方/你/ 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

应用程序/代码/地方/你/ Yourmodule /模型/ 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;
    }
}

#2


4  

Here is the simplest and best way to override META logics for product page in magento

这是在magento中重写产品页面元逻辑的最简单、最好的方法

copy /app/code/core/Mage/Catalog/Block/Product/View.php to /app/code/local/Mage/Catalog/Block/Product

复制/ app /代码/核心/法师/目录/块/产品/视图。php / app /代码/地方/法师/目录/块/产品

and override function _prepareLayout(), my example goes bellow

然后重写函数_prepareLayout(),我的示例如下所示

protected 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();
}

#3


1  

this is addition to Drew Hunter's answer

这是德鲁·亨特的回答。

magento includes category name into title so the proper solution would be:

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

#4


1  

Assuming that you only want to change the product title on the catalog view page

假设您只想更改catalog视图页面上的产品标题

In app/design/frontend/default/{your theme Folder}/template/page/html/head.phtml you could

在app /设计/前端/违约/ {你的主题文件夹} /模板/页面/ html /头。phtml你可以

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

See Get Attribute Name And Value Magento

参见获取属性名和值Magento

#5


0  

I believe Drew Hunter has provided most of the correct answer, but I had to make a few changes to get it to work (EE, 1.14.2.x):

我相信Drew Hunter已经提供了大部分正确答案,但是我必须做一些修改才能让它生效(EE, 1.14.2.x):

In config.xml:

在config . xml:

Change the nodes <yourmodule>...</yourmodule> to ><yourcompany_yourmodule>...</yourcompany_yourmodule>

改变节点< yourmodule >…< / yourmodule > > < yourcompany_yourmodule >…< / yourcompany_yourmodule >

In Observer.php:

在Observer.php:

Change public function catalog_controller_product_view(Varien_Event_Observer $observer) to public function catalog_controller_product_view($observer)

将public function catalog_controller_product_view(Varien_Event_Observer $observer)更改为public function catalog_controller_product_view($observer)

#1


21  

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

在您的问题中,我假定您是指更改产品的meta标题。

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. 检查每个产品并手动更新(或使用电子表格和导入)每个产品元标题。这些值在编辑产品时可以在管理区域中使用。
  3. Rewrite Mage_Catalog_Block_Product_View and override the _prepareLayout() method which is where this tag is being generated.
  4. 重写Mage_Catalog_Block_Product_View并重写_prepareLayout()方法,该方法就是生成标记的地方。
  5. Use an observer and hook into catalog_controller_product_view event.
  6. 使用一个观察者并将其挂接到catalog_controller_product_view事件。

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

您的决定实际上是在选项2和选项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:

在扩展Magento核心功能时,我总是尽量不引人注目,所以我选择这里的选项3。请参阅下面的代码,以获得完整的示例:

app/etc/modules/Yourcompany_Yourmodule.xml

应用程序/ etc /模块/ 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

应用程序/代码/地方/你/ 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

应用程序/代码/地方/你/ Yourmodule /模型/ 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;
    }
}

#2


4  

Here is the simplest and best way to override META logics for product page in magento

这是在magento中重写产品页面元逻辑的最简单、最好的方法

copy /app/code/core/Mage/Catalog/Block/Product/View.php to /app/code/local/Mage/Catalog/Block/Product

复制/ app /代码/核心/法师/目录/块/产品/视图。php / app /代码/地方/法师/目录/块/产品

and override function _prepareLayout(), my example goes bellow

然后重写函数_prepareLayout(),我的示例如下所示

protected 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();
}

#3


1  

this is addition to Drew Hunter's answer

这是德鲁·亨特的回答。

magento includes category name into title so the proper solution would be:

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

#4


1  

Assuming that you only want to change the product title on the catalog view page

假设您只想更改catalog视图页面上的产品标题

In app/design/frontend/default/{your theme Folder}/template/page/html/head.phtml you could

在app /设计/前端/违约/ {你的主题文件夹} /模板/页面/ html /头。phtml你可以

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

See Get Attribute Name And Value Magento

参见获取属性名和值Magento

#5


0  

I believe Drew Hunter has provided most of the correct answer, but I had to make a few changes to get it to work (EE, 1.14.2.x):

我相信Drew Hunter已经提供了大部分正确答案,但是我必须做一些修改才能让它生效(EE, 1.14.2.x):

In config.xml:

在config . xml:

Change the nodes <yourmodule>...</yourmodule> to ><yourcompany_yourmodule>...</yourcompany_yourmodule>

改变节点< yourmodule >…< / yourmodule > > < yourcompany_yourmodule >…< / yourcompany_yourmodule >

In Observer.php:

在Observer.php:

Change public function catalog_controller_product_view(Varien_Event_Observer $observer) to public function catalog_controller_product_view($observer)

将public function catalog_controller_product_view(Varien_Event_Observer $observer)更改为public function catalog_controller_product_view($observer)