动态获取magento元关键字? [英] Get magento meta keywords dynamically?

查看:82
本文介绍了动态获取magento元关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在magento的产品"页面上,我想在meta关键字标签中获得产品名称,其类别名称和子类别名称.

On the Product page of magento, i want to get the product name, its category name and sub category name in the meta keywords tag.

请帮助!

提前谢谢.

推荐答案

由于产品已经具有附加的MetaKeyword值,因此您可以使用观察者来轻松地扩展该值.此方法不涉及扩展核心类

Since products already have an attached MetaKeyword value, you can use an observer to unobtrusively extend that value. This method doesn't involve extending a core class

尝试一下:

/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>
            <YourCompany_YourModule>
                <class>YourCompany_YourModule_Model</class>
            </YourCompany_YourModule>
        </models>
    </global>
    <frontend>
        <events>
            <catalog_controller_product_view>
                <observers>
                    <YourCompany_YourModule>
                        <class>YourCompany_YourModule/Observer</class>
                        <method>productView</method>
                    </YourCompany_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
{

    public function productView(Varien_Event_Observer $observer)
    {
        $product = $observer->getEvent()->getProduct();
        /* @var $product Mage_Catalog_Model_Product */

        if ($product) {
            $keywords = $product->getMetaKeyword();

            // Add the product name
            $keywords = ' ' . $product->getName();

            // Add the category name
            $currentCategory = Mage::registry('current_category');
            if ($currentCategory && $currentCategory instanceof Mage_Catalog_Model_Category) {
                $keywords = ' ' . $currentCategory->getName();
            }

            $product->setMetaKeyword($keywords);
        }
    }

}

这篇关于动态获取magento元关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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