1

我正在尝试在 Magento 中构建一个页面,该页面基本上列出了具有分层导航的特定类别中的所有产品(在我的情况下是左侧的过滤器),但该列表必须每天随机生成一次。

我曾尝试使用“product_list_random”,但它似乎忽略了 CategoryIDs 设置。

进一步调查,我找到了 Random.php,问题是 Random.php 中的受保护函数 _getProductCollection 只是从完整的产品集合开始,而在 List.php 中,它有一些逻辑来获取当前类别。

现在我正在努力将代码从 List.php 调整为 Random.php,但没有成功......

到目前为止,这是我的代码:

         $layer = $this->getLayer();
        /* @var $layer Mage_Catalog_Model_Layer */

        $origCategory = null;
        if ($this->getCategoryId()) {
            $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
            if ($category->getId()) {
                $origCategory = $layer->getCurrentCategory();
                $layer->setCurrentCategory($category);
            }
        }
        //$this->_productCollection = $layer->getProductCollection();
        $this->_productCollection = $layer->getSelect()->order('rand()');

        $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());

        if ($origCategory) {
            $layer->setCurrentCategory($origCategory);
        }        

现在看来你不能“rand” Magento 的层模型,但我在这里有点迷失了。关于如何完成这个的任何想法?我能感觉到我非常接近,但我错过了一些东西...... :(

提前致谢!

更新 2

好的。我想我知道发生了什么。我以“更优化/合理”的方式重写了代码,但仍然遇到分页问题。

我对渲染过程的一部分进行了调试,似乎正在发生的事情是 Toolbar.phtml/php 在 Random.php 之前被称为。原因有两个: (1) My Layout 实际上将工具栏声明为 Random 的子项,因此应首先调用它;(2) 必须首先调用它,因为工具栏需要知道它将分页多少元素。

因此,一旦我在代码中调用 setCollection,所有分页参数都已经存在,所以分页中断...... :( 这是我的理论。

我能想到的唯一解决方法是在我的集合加载后重置所有工具栏参数,这(1)似乎不合理(必须有更好的方法!)和(2)我不知道如何将它拉出来.. . :( :(

当前代码如下......像往常一样,任何指针将不胜感激!

protected function _getProductCollection()
{
    //xdebug_start_trace('/Library/WebServer/talcha/var/log/random-trace.txt');

    /* get ready to build collection */
    $collection=Mage::getResourceModel('catalog/product_collection');
    $genlayer=Mage::getModel('catalog/layer');
    $genlayer->prepareProductCollection($collection);
    $collection->addAttributeToFilter('status',1); //only enabled product
    $collection->addAttributeToFilter('visibility',4); //only 'catalog,search' products
    $collection->addStoreFilter();


    /* Find out where I am */
    $newrandom=false; // flag to check if random collection was generated
    $interna=false;
    $currentUrl = $this->helper('core/url')->getCurrentUrl();
    Mage::log("URL:".$currentUrl,null,"paulo.log");
    if (strpos($currentUrl,"chas")!==false) {  // estou em CHAS
       $interna=true;       
    }

    /* if I am where I need to be, then check whether I need to update the collection */
    if ($interna) {
        $now=time();

        /* fetch collection file name and generated time */
        if (strpos($currentUrl,"?")===false) {
            $name=substr($currentUrl,strpos($currentUrl,"index.php")+10);
        } else {
            $name=substr($currentUrl,strpos($currentUrl,"index.php")+10,strpos($currentUrl,"?")-strlen($currentUrl));
        }
        if ($name=="chas2") { $name="chas"; }
        Mage::log("Nome:".$name,null,"paulo.log");  
        $dir=Mage::getBaseDir('tmp');
        $dh=opendir($dir);
        while (($file=readdir($dh))!=false) {
            Mage::log("Arq.:".$file,null,"paulo.log");
            if (strpos($file,$name)!==false) {
                $epoch=substr($file,strpos($file,"-")+1);
                $fname=$file;
                Mage::log("Hora:".$epoch,null,"paulo.log");
                break;
            }
        }

        /* tests whether elapsed time between now and last update is longer that "x" seconds */
        /* if true, generates new random collection, serialize it and flag it */
        /* if false, just picks up last generated collection from file */
        if (($now-$epoch) > 3600) {  //set 1 hour
            $collection->addCategoryFilter(Mage::getModel('catalog/category')->load($this->get_cur_category()));
            $collection->getSelect()->order('rand()');
            file_put_contents($dir."/".$name."-".$now,serialize($collection));
            unlink($dir."/".$fname);
            $newrandom=true;
            Mage::log("Fiz shuffle!",null,"paulo.log");
            Mage::log("Count:".$collection->count(),null,"paulo.log");
        } else {
            $collection=unserialize(file_get_contents($dir."/".$fname));
        }
    } // close if($interna)     

    /* build the final collection.  If we are using the generated collection, we need
        to adjust pagination according to request parameters.
        If we are filtering, then we set proper filters and load the ordinary collection
        (no random stuff when filtering through) */
    if (is_null($this->_productCollection)) {

        $layer=$this->getLayer();   

        foreach($this->getRequest()->getParams() as $key=>$value) {
            Mage::log("Random:".$key."=>".$value,null,"paulo.log");

            switch ($key) {
              case "p":
                $collection->setPageSize($this->get_prod_count())->setCurPage($value);
                break;
              case "cat":
                $collection->addCategoryFilter(Mage::getModel('catalog/category')->load($value));
                break;
              case "price":
                list($range,$step)=explode(",",$value);
                $gt_value=($range-1)*$step;
                $lt_value=$range*$step;
                $collection->addAttributetoFilter($key,array('gt'=>$gt_value));
                $collection->addAttributetoFilter($key,array('lteq'=>$lt_value));
                break;
              case "limit":
                $collection->setPageSize($value)->setCurPage(1);
                break;
              case "page_id":
              case "dir":
              case "order":
              case "mode":
                break;
              default:
                $collection->addAttributeToFilter($key,$value); //filters
            }
        }   

        /* if we are filtering, then the collection wasn't loaded yet */
        if (!$interna) {
            $collection->load();    
        }

    }

    $this->setCollection($collection);
    $this->_productCollection=$collection;

    return $this->_productCollection;
}
4

1 回答 1

0
<?php // get Random prdocut list

$_productCollection = Mage::getResourceModel('catalog/product_collection');
Mage::getModel('catalog/layer')->prepareProductCollection($_productCollection);
$_productCollection->getSelect()->order('rand()');
$_productCollection->addStoreFilter();
$numProducts = $this->getNumProducts() ? $this->getNumProducts() : 20;
$_productCollection->setPage(1, $numProducts);

if (sizeof($_productCollection) < $numProducts) {
        $numProducts = sizeof($_productCollection);
}
$displayed_products = array();
foreach ($_productCollection as $_product) {
        $displayed_products[] = $_product;
}
$random_products = array();
if (sizeof($_productCollection) > 1) {
        $random_products = array_rand($displayed_products, $numProducts);
} else {
        $random_products = array('0');
}

?>
于 2012-11-22T05:18:40.860 回答