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

6
我有两个自定义属性想要添加到产品页面的标签中。它们是“品牌”和“副标题”。</br><div class="h-2"></div>我的页面标题最终会变成这样: $brand." ".$productname." ".$subtitle;</br><div class="h-2"></div>我该如何实现这一点?</br><div class="h-2"></div>非常感谢您的帮助。

您是指实际页面标题,即元标题,还是仅指产品名称在产品页面上的显示方式? - Drew Hunter
5个回答

22

从您的问题中,我认为您是在指更改产品的元标题。

您有三个选择:

  1. 逐个产品手动更新(或使用电子表格并导入)每个产品的元标题。这些值在编辑产品时在管理区域中可用。
  2. 重写Mage_Catalog_Block_Product_View并覆盖_prepareLayout()方法,这是生成此标记的位置。
  3. 使用观察者并挂钩到catalog_controller_product_view事件。

您的决策实际上是在选项2和3之间(这两个选项都需要您创建自定义模块才能实现)。

当扩展Magento核心功能时,我总是尽可能不引人注目-因此我会选择选项3。 请参见下面的代码以获取完整示例:

app/etc/modules/Yourcompany_Yourmodule.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Yourcompany_Yourmodule>
            <active>true</active>
            <codePool>local</codePool>
        </Yourcompany_Yourmodule>
    </modules>
</config>

应用程序/代码/本地/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;
    }
}

嗨,Drew,谢谢你的回答。很抱歉,但我指的是页面标题标签。Stackoverflow在我添加反引号之前不接受我的<title>格式,因此它只删除了“title”这个单词。 - Lucas Scholten
嗨,Lucas - 这段代码将根据您的要求更改页面标题标签。 - Drew Hunter
我相信我能够成功地在产品视图.phtml中调用这个值。 - Lucas Scholten
@LucasScholten - 我已经测试了下拉列表和文本属性的代码。都可以正常工作。如果你使用getAttributeText方法并var_dump结果,你会得到一个空字符串还是false? - Drew Hunter
嗨,Drew,再次感谢您的帮助。我对此非常陌生,如何var_dump getAttributeText的结果?到目前为止,我已经进入view.phtml并添加了Zend_Debug :: dump($ _product);这显示metatitle未更改。它显示示例数据产品上的品牌,但这仅是因为品牌已经输入到名称字段中。我正在使用1.7.0社区版。 - Lucas Scholten
显示剩余3条评论

5
以下是关于如何在Magento中覆盖产品页面META逻辑的最简单和最佳方法:
/app/code/core/Mage/Catalog/Block/Product/View.php复制到/app/code/local/Mage/Catalog/Block/Product中,并覆盖function _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();
}

1
假设您只想更改目录视图页面上的产品标题。
在app/design/frontend/default/{您的主题文件夹}/template/page/html/head.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 } ?>

请查看 获取Magento属性名称和值


1

这是对Drew Hunter答案的补充。

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


0

我相信Drew Hunter已经提供了大部分正确的答案,但是我不得不做一些更改才能使它在EE 1.14.2.x上运行:

在config.xml中:

将节点<yourmodule>...</yourmodule>更改为><yourcompany_yourmodule>...</yourcompany_yourmodule>

在Observer.php中:

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接