【问题标题】:Magento - get products by category idsMagento - 按类别 id 获取产品
【发布时间】:2015-04-07 20:15:57
【问题描述】:

我想使用网址 http://mydomain.test/categoru-name.html?cat=9,10,40 。我需要从类别类别名称和具有类别 ID (9,10,40) 的子类别中获取所有产品。

我该怎么做?也许有这样的扩展。

【问题讨论】:

    标签: magento categories product


    【解决方案1】:

    使用以下代码将 php 页面 (categoru-name.php) 添加到 Magento 根目录。您需要进行一些修改才能使其正常工作。

    /** START Include Magento **/
    define('MAGENTO_ROOT', getcwd());
    
    $compilerConfig = MAGENTO_ROOT . '/includes/config.php';
    if (file_exists($compilerConfig)) {
        include $compilerConfig;
    }else{exit;}
    
    $mageFilename = MAGENTO_ROOT . '/app/Mage.php';
    
    if (file_exists($mageFilename)) {
        require_once $mageFilename;
    }else{exit;}
    
    Mage::app();
    /** END Include Magento **/
    
    // This is not a complete code. You need to fill in.
    
    // Add from query paramter
    $categoryIds = array("Insert IDs from URL");
    $categoryIdList = array();
    array_push($categoryIdList,$categoryIds); //Collect child category IDs
    
    foreach($categoryIds as $Catid){
        $cat = Mage::getModel('catalog/category')->load($Catid);
        $subCats = $cat->getChildren();
        $arrSubCats = explode(',',$subCats);
        array_push($categoryIdList,$arrSubCats); //Collect child category IDs
    
        // Go another level down
            foreach($arrSubCats as $Catid){
            $cat = Mage::getModel('catalog/category')->load($Catid);
            $subSubCats = $cat->getChildren();
            $arrSubSubCats = explode(',',$subSubCats);
            array_push($categoryIdList,$arrSubSubCats); //Collect child category IDs
    
            // Go another level down
            // Another foreach loop for  $arrSubSubCats
        }
    }
    
    // Now we have all categoryIds in the tree.
    // Get all products.
    $collection = Mage::getModel('catalog/product')
                                 ->getCollection()
                                 ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
                                 ->addAttributeToSelect('*')
                                 ->addAttributeToFilter('category_id', array('in' => $categoryIdList))
    

    【讨论】:

      猜你喜欢
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-27
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      相关资源
      最近更新 更多