$ block-> getLoadedProductCollection()打破了分页--Magneto EE 1.14.2.2

时间:2016-02-11 17:37:17

标签: javascript php magento zend-framework

因此,我们为Magento定制的数据层:https://github.com/QubitProducts/UniversalVariable-Magento-Extension效果非常好,但我在一些客户端网站上遇到了一些问题(分页显示错误的信息并将其精确定位到我们的方式正在加载产品系列的类别页面。

简而言之,此模块将所有相关页面信息放入位于window.universal_variable的漂亮JavaScript对象。

在第二个(或更晚的)类别页面上,您会看到如下内容:(图1)

figure 1

这就是“非魔法”的地方。发生:

<?php
class QuBit_UniversalVariable_Model_Observer
{
/**
 * Observes controller_action_layout_render_before
 * @param Varien_Event_Observer $observer
 */
public function processListingData(Varien_Event_Observer $observer)
{
    /** @var QuBit_UniversalVariable_Helper_Data $helper */
    $helper = Mage::helper('qubituv');
    $layout = Mage::app()->getLayout();
    /** @var Mage_Catalog_Block_Product_List $block */
    $block = $layout->getBlock($helper->getCategoryProductListBlockName());
    if (!$block instanceof Mage_Catalog_Block_Product_List) {
        $block = $layout->getBlock($helper->getSearchProductListBlockName());
        if (!$block instanceof Mage_Catalog_Block_Product_List) {
            return;
        }
    }
    $uv = $helper->getUv();
    $listing = $uv->getListing();
    /** @var Mage_Catalog_Model_Resource_Product_Collection $collection */
    $collection = $block->getLoadedProductCollection();
    $listing['result_count'] = $collection->getSize();
    $listing['items'] = array();
    foreach($collection as $product) {
        $listing['items'][] = $uv->getProductData($product);
    }
    $toolbar = $block->getToolbarBlock();
    $listing['sort_by'] = $toolbar->getCurrentOrder() . '_' . $toolbar->getCurrentDirection();
    $listing['layout'] = $toolbar->getCurrentMode();
    $uv->setListing($listing);
 }

}

来源:https://github.com/QubitProducts/UniversalVariable-Magento-Extension/blob/master/app/code/community/QuBit/UniversalVariable/Model/Observer.php

和$ uv-&gt; getProductData($ product);看起来像这样:

/**
 * @param Mage_Catalog_Model_Product $product
 * @return array
 */
public function getProductData(Mage_Catalog_Model_Product $product)
{
    $id = $product->getId();
    if (!$this->helper()->shouldUseRealProductId()) {
        $id = $product->getSku() ? $product->getSku() : md5($id);
    }
    $data = array(
        'id' => $id,
        'url' => $product->getProductUrl(),
        'name' => $product->getName(),
        'unit_price' => (float)$product->getPrice(),
        'unit_sale_price' => (float)$product->getFinalPrice(),
        'currency' => $this->_getCurrency(),
        'description' => strip_tags($product->getShortDescription()),
        'sku_code' => $product->getSku()
    );
    if ($this->helper()->shouldShowProductStockInfo()) {
        $data['stock'] = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();
    }
    $catIndex = $catNames = array();
    $limit = 2; $k = 0;
    foreach ($product->getCategoryIds() as $catId) {
        if (++$k > $limit) {
            break;
        }
        if (!isset($catIndex[$catId])) {
            $catIndex[$catId] = Mage::getModel('catalog/category')->load($catId);
        }
        $catNames[] = $catIndex[$catId]->getName();
    }
    if (isset($catNames[0])) {
        $data['category'] = $catNames[0];
    }
    if (isset($catNames[1])) {
        $data['subcategory'] = $catNames[1];
    }
    return $data;
}

来源:https://github.com/QubitProducts/UniversalVariable-Magento-Extension/blob/master/app/code/community/QuBit/UniversalVariable/Model/Uv.php

有没有人对为什么使用$ block-&gt; getLoadedProductCollection()可能导致此问题有任何想法?以及我们是否可以添加任何过滤器?或者在类别页面上加载产品以填充我们的对象的另一种方法。

谢谢!

0 个答案:

没有答案