【问题标题】:Magento : How to get category page URL keys for different store views?Magento:如何获取不同商店视图的类别页面 URL 键?
【发布时间】:2015-06-03 09:59:17
【问题描述】:

我正在尝试获取不同商店视图的类别页面的页面 URL 密钥。基本上我在 Magento 安装中设置了 3 家商店。现在我想在我的类别页面中实现 hrefhang 标签。 但是当我在默认商店时,我无法访问其他商店视图的类别 URL 键,反之亦然。

我有从中获得的类别对象,

$category = Mage::registry('current_category');

有什么想法吗?

【问题讨论】:

    标签: php magento url


    【解决方案1】:

    似乎在与当前商店不同的商店下获取类别 URL 的最佳方法是 make use of Magento’s Mage_Core_Model_App_Emulation。以下是您如何做到这一点的示例:

    /**
     * @var $categoryId - The numeric category ID you want to get the URL of.
     * @var $altStoreId - The numeric ID of the other store view to get the URL from.
     */
    $env = Mage::getSingleton('core/app_emulation')->startEnvironmentEmulation($altStoreId);
    $category = Mage::getModel('catalog/category')->load($categoryId);
    $altUrl = $category->getUrl();
    Mage::getSingleton('core/app_emulation')->stopEnvironmentEmulation($env);
    

    【讨论】:

    • 谢谢伙计!,这是炉排!!,不幸的是,由于某种原因,stopEnvironmentEmulation 没有按照预期的方式工作,基本上它为我提供了每个商店的正确类别 URL,但它阻止了页面(它停止加载任何类别)。所以我不得不探索其他方法来做到这一点,并设法提出以下答案。
    • 它有效,但执行时间比 Thanu 的解决方案多 20 倍
    【解决方案2】:

    我的解决方案,效果很好

    /**
     * @var $store_id  - The numeric ID of the store view to get the URL from.
     * @var $store_url - Base URL of the store
     */
    
     $store_url   = Mage::app()->getStore($store_id)
                    ->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
    
     $objcategory = Mage::registry('current_category');
     $categoryId  = $objcategory->getId();
     $caturlkey   = Mage::getModel('catalog/category')
                        ->setStoreId($store_id)->load($categoryId)->getUrlKey();
    
     $altUrl      = $store_url.$caturlkey;
    

    【讨论】:

      【解决方案3】:

      您可以像这样执行(在环境仿真时成本应该更低):

      $currentStore = Mage::app()->getStore();
      Mage::app()->setCurrentStore(Mage::app()->getStore($yourAltStoreId));
      $categoryLink = Mage::getModel('catalog/category')->load($yourCategoryId)->getUrl();
      Mage::app()->setCurrentStore($currentStore);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-01
        • 2013-07-18
        • 1970-01-01
        • 1970-01-01
        • 2015-12-19
        • 1970-01-01
        • 2019-09-03
        • 2013-07-13
        相关资源
        最近更新 更多