首页 > 解决方案 > 在类别列表中显示产品页面的价值 - Magento 1.9

问题描述

我们目前加载了一个 php 值,它只会显示在产品页面上。

因此我们使用:<?php Mage::helper('seo')->getCurrentSeo()->getTitle();?>

并显示类似:DELL Latitude 5590 - 15.6" - i5-8350U - MJR1P

每个产品的价值都不同。

有没有办法重写该行并包含一个productid?这样我们就可以在类别列表页面上使用此行并显示与产品页面上相同的值?

我们怎样才能做到这一点?

我已经尝试过了,但这不起作用:

$product = Mage::getModel('catalog/product')->load(409728);
Mage::register('product', $product);
echo Mage::helper('seo')->getCurrentSeo()->getTitle();

getCurrentSeo 的代码:

public function getCurrentSeo()
{
    if (Mage::app()->getStore()->getCode() == 'admin') {
        return new Varien_Object();
    }

    $isCategory = Mage::registry('current_category') || Mage::registry('category');
    $isProduct  = Mage::registry('current_product') || Mage::registry('product');
    $isFilter   = false;

    if ($isCategory) {
        $filters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
        $isFilter = count($filters) > 0;
    }

    if ($isProduct) {
        $seo = Mage::getSingleton('seo/object_product');
    } elseif ($isCategory && $isFilter) {
        $seo =  Mage::getSingleton('seo/object_filter');
    } elseif ($isCategory) {
        $seo =  Mage::getSingleton('seo/object_category');
    } else {
        $seo = new Varien_Object();
    }

    if ($seoTempalate = $this->checkTempalateRule($isProduct, $isCategory, $isFilter)) {
        foreach ($seoTempalate->getData() as $k=>$v) {
            if ($v) {
               $seo->setData($k, $v);
            }
        }
    }

    if ($seoRewrite = $this->checkRewrite()) {
        foreach ($seoRewrite->getData() as $k=>$v) {
            if ($v) {
               $seo->setData($k, $v);
            }
        }
    }

    $storeId = Mage::app()->getStore()->getStoreId();
    $page    = Mage::app()->getFrontController()->getRequest()->getParam('p');
    if (!$page) {
        $page = 1;
    }

    if ($isCategory && !$isProduct) {
        if ($this->_titlePage) {
            switch ($this->_config->getMetaTitlePageNumber($storeId)) {
                case Mirasvit_Seo_Model_Config::META_TITLE_PAGE_NUMBER_BEGIN:
                    if ($page > 1) {
                        $seo->setMetaTitle(Mage::helper('seo')->__("Page %s | %s", $page, $seo->getMetaTitle()));
                        $this->_titlePage = false;
                    }
                    break;
                case Mirasvit_Seo_Model_Config::META_TITLE_PAGE_NUMBER_END:
                    if ($page > 1) {
                        $seo->setMetaTitle(Mage::helper('seo')->__("%s | Page %s", $seo->getMetaTitle(), $page));
                        $this->_titlePage = false;
                    }
                    break;
                case Mirasvit_Seo_Model_Config::META_TITLE_PAGE_NUMBER_BEGIN_FIRST_PAGE:
                    $seo->setMetaTitle(Mage::helper('seo')->__("Page %s | %s", $page, $seo->getMetaTitle()));
                    $this->_titlePage = false;
                    break;
                case Mirasvit_Seo_Model_Config::META_TITLE_PAGE_NUMBER_END_FIRST_PAGE:
                    $seo->setMetaTitle(Mage::helper('seo')->__("%s | Page %s", $seo->getMetaTitle(), $page));
                    $this->_titlePage = false;
                    break;
            }
        }

        if ($this->_descriptionPage) {
            switch ($this->_config->getMetaDescriptionPageNumber($storeId)) {
                case Mirasvit_Seo_Model_Config::META_DESCRIPTION_PAGE_NUMBER_BEGIN:
                    if ($page > 1) {
                        $seo->setMetaDescription(Mage::helper('seo')->__("Page %s | %s", $page, $seo->getMetaDescription()));
                        $this->_descriptionPage = false;
                    }
                    break;
                case Mirasvit_Seo_Model_Config::META_DESCRIPTION_PAGE_NUMBER_END:
                    if ($page > 1) {
                        $seo->setMetaDescription(Mage::helper('seo')->__("%s | Page %s", $seo->getMetaDescription(), $page));
                        $this->_descriptionPage = false;
                    }
                    break;
                case Mirasvit_Seo_Model_Config::META_DESCRIPTION_PAGE_NUMBER_BEGIN_FIRST_PAGE:
                    $seo->setMetaDescription(Mage::helper('seo')->__("Page %s | %s", $page, $seo->getMetaDescription()));
                    $this->_descriptionPage = false;
                    break;
                case Mirasvit_Seo_Model_Config::META_DESCRIPTION_PAGE_NUMBER_END_FIRST_PAGE:
                    $seo->setMetaDescription(Mage::helper('seo')->__("%s | Page %s", $seo->getMetaDescription(), $page));
                    $this->_descriptionPage = false;
                    break;
            }
        }

        if ($page > 1) {
            $seo->setDescription(''); //set an empty description for page with number > 1 (to not have a duplicate content)
        }
    }

    if ($metaTitleMaxLength = $this->_config->getMetaTitleMaxLength($storeId)) {
        $metaTitleMaxLength = (int)$metaTitleMaxLength;
        if ($metaTitleMaxLength < Mirasvit_Seo_Model_Config::META_TITLE_INCORRECT_LENGTH) {
            $metaTitleMaxLength = Mirasvit_Seo_Model_Config::META_TITLE_MAX_LENGTH; //recommended length
        }
        $seo->setMetaTitle($this->_getTruncatedString($seo->getMetaTitle(), $metaTitleMaxLength, $page));
    }

    if ($metaDescriptionMaxLength = $this->_config->getMetaDescriptionMaxLength($storeId)) {
        $metaDescriptionMaxLength = (int)$metaDescriptionMaxLength;
        if ($metaDescriptionMaxLength < Mirasvit_Seo_Model_Config::META_DESCRIPTION_INCORRECT_LENGTH) {
            $metaDescriptionMaxLength = Mirasvit_Seo_Model_Config::META_DESCRIPTION_MAX_LENGTH; //recommended length
        }
        $seo->setMetaDescription($this->_getTruncatedString($seo->getMetaDescription(), $metaDescriptionMaxLength, $page));
    }

    return $seo;
}

标签: phpmagento-1.9

解决方案


在你的代码中你Mage::register('product', $product);应该做的是Mage::register('current_product', $product);. 但是,既然您说您在产品页面上,这应该由 Magento 完成吗?

我不确定您希望产品 ID 如何显示,但您始终可以执行以下操作:

// retrieve current product
$product = Mage::registry('current_product');
// put product id at the beginning e.g. 1020: some title
echo $product->getId() . ': ' . Mage::helper('seo')->getCurrentSeo()->getTitle();

请注意,回声当然应该在您的 PHTML 中的适当位置。


推荐阅读