【问题标题】:get an array of all categories with details in Magento在 Magento 中获取包含详细信息的所有类别的数组
【发布时间】:2012-02-25 21:18:40
【问题描述】:

我希望能够通过 API 调用以获取所有类别的数组,其中包含 URL 键等详细信息。那个目标最终会是这样一个数组

$massage_cats=array(
    array("entity_id"=>78,
          "name"=>"Massage Oils and Tools",
          "url_key"=>"massage-oils-and-tools",
          "url_path"=>"essential-accessories/massage-oils-and-tools.html"),
    array("entity_id"=>79,
          "name"=>"Massage Oils",
          "url_key"=>"massage-oils",
          "url_path"=>"essential-accessories/massage-oils-and-tools/massage-oils.html")
);

所以我想喊出类似的东西

$massage_cats= array();
$allcats = Mage::getModel('catalog/cats?')->loadAll();
    foreach($allcats $k=>$item){
        array_push($massage_cats,$item->loadDetails());
    }

我知道这完全是虚构的,对 API 来说并不真实,但这基本上是目标。我确实需要我展示的输出。关于实现代码的想法需要?

【问题讨论】:

    标签: php magento e-commerce


    【解决方案1】:

    这将获得您的价值观。您可以从这里构建您喜欢的阵列。

    $categories = Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToSelect('id')
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('url_key')
    ->addAttributeToSelect('url')
    ->addAttributeToSelect('is_active');
    
    foreach ($categories as $category)
    {
        if ($category->getIsActive()) { // Only pull Active categories
            $entity_id = $category->getId();
            $name = $category->getName();
            $url_key = $category->getUrlKey();
            $url_path = $category->getUrl();
        }
    }
    

    EDIT

    我改编自 MagentoCommerce.com 上的帖子。你可以改用这个:

    $category = Mage::getModel('catalog/category');
    $tree = $category->getTreeModel();
    $tree->load();
    $ids = $tree->getCollection()->getAllIds();
    if ($ids){
        foreach ($ids as $id){
            $cat = Mage::getModel('catalog/category');
            $cat->load($id);
    
            $entity_id = $cat->getId();
            $name = $cat->getName();
            $url_key = $cat->getUrlKey();
            $url_path = $cat->getUrlPath();
        }
    }
    

    【讨论】:

    • 如此接近.. $url_path = $category->getUrl();不是网址,即:我应该在哪里有 essential-accessories/massage-oils-and-tools.html 我有 domain.com/importScript.php/essential-accessories/… 甚至 domain.com/importScript.php/catalog/category/view/s/…
    • 啊,试试$url_path = $category->getUrlPath();
    • 是我尝试的第一件事,但我得到了“/massage-oils-and-tools”,我应该得到“essential-accessories/massage-oils-and-tools”的猫“按摩油和工具”与父“基本配件”..
    • 使用普通集合或树集合,它总是获取重复的类别,例如。如果父类别为“咖啡”,则它会获取多次而不是一次。
    【解决方案2】:

    在这里,我以数组格式编写了最多三层返回的函数

    $array=hpCat(2,3); //categoryID,Sublevel最多三层

    print_r($array);

    <?php     
    
    function hpCat($id,$level=0){
        if(!empty($id)){
            $level=empty($level)?0:$level;
            $category = Mage::getModel('catalog/category')->load($id);
            $levelOneItems = $category->getChildrenCategories();
            if (count($levelOneItems) > 0){
                $array=hpCatDetails($category);
                if($level>=1):
                    $i=0;
                    foreach($levelOneItems as $levelOneItem){ 
                        $array['sub'][$i]=hpCatDetails($levelOneItem);    
                        $leveltwoItems=$levelOneItem->getChildrenCategories();
                        if (count($leveltwoItems) > 0){
                            if($level>=2):
                                $j=0;
                                foreach($leveltwoItems as $leveltwoItem){     
                                    $array['sub'][$i]['sub'][$j]=hpCatDetails($leveltwoItem);
                                    $levelthreeItems=$leveltwoItem->getChildrenCategories();
                                    if (count($levelthreeItems) > 0){
                                        if($level>=3):
                                        $k=0;
                                        foreach($levelthreeItems as $levelthreeItem){     
                                            $array['sub'][$i]['sub'][$j]['sub'][$k]=hpCatDetails($levelthreeItem);
                                            $k++;
                                        }  
                                        endif;
                                    }
                                    $j++;
                                }  
                            endif;
                        }
                        $i++;
                    }
                endif;
            }
            return $array;
        }
        return array();
    }
    function hpCatDetails($cat){
        return array('name'=>$cat->getName());
    }
    
    $array=hpCat(2,3);//categoryID,Sublevel upto three level
    echo '<pre>';print_r($array);die();
    
    
    ?>
    

    【讨论】:

      【解决方案3】:

      对于寻找 MySQL 查询以获取所有 Magento 类别的人。

      SELECT
          e.entity_id AS id,
          e.parent_id,
          e.path,
          e.`level`,
      
      IF (
          at_name.value_id > 0,
          at_name.
      VALUE
          ,
          at_name_default.
      VALUE
      
      ) AS `name`
      FROM
          `catalog_category_entity` AS `e`
      INNER JOIN `catalog_category_entity_varchar` AS `at_name_default` ON (
          `at_name_default`.`entity_id` = `e`.`entity_id`
      )
      AND (
          `at_name_default`.`attribute_id` = '41'
      )
      LEFT JOIN `catalog_category_entity_varchar` AS `at_name` ON (
          `at_name`.`entity_id` = `e`.`entity_id`
      )
      AND (
          `at_name`.`attribute_id` = '41'
      )
      

      【讨论】:

      • 太棒了!这对我来说可以更快地获得选定的属性值。
      【解决方案4】:

      一个递归函数来实现它:

       private function __categoriesTree($id = 2) {
          $category = Mage::getModel('catalog/category');
      
          $_category = $category->load($id);
          $details = new stdClass();
          list($details->id, $details->name, $details->urlKey, $details->level, $details->children) = [
              $_category->getId(),
              $_category->getName(),
              $_category->getUrlKey(),
              $_category->getLevel(),
              []
          ];
      
          foreach (array_filter(explode(',', $_category->getChildren())) as $childId) {
              $details->children[] = $this->__categoriesTree($childId);
          }
          if (count($details->children) === 0) {
              unset($details->children);
          }
          return $details;
      }
      

      还有

      $categoriesTree=  $this->categoriesTree()
      

      我更喜欢使用对象而不是数组来建模节点,但您可以轻松替换。

      【讨论】:

        猜你喜欢
        • 2018-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-03
        相关资源
        最近更新 更多