在类别列表-Magento 1.9

时间:2018-12-12 16:48:50

标签: php 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;
}

1 个答案:

答案 0 :(得分:1)

在您的代码中执行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中的适当位置。