【问题标题】:Create hierarchy of categories from woocommerce api products/categories从 woocommerce api 产品/类别创建类别层次结构
【发布时间】:2017-01-26 20:33:40
【问题描述】:

我正在尝试创建我的应用程序可以使用的数据结构,以便我可以将系统中的类别映射到 woo 商务结构。我只需要一个类别层次结构(如下所示),其中包含类别的完整路径,每个父/子之间都有一个管道,该管道具有类别子节点的 id。

我可以通过以下方式轻松获取 woocommerce 中的所有产品类别:

    $woocommerce    =   new Client($this->ecommerce_url,$this->ecommerce_api_key,$this->ecommerce_api_cred,array('wp_api' => true,'version' => 'wc/v1'));

    $send_call = true;
    $per_page = 30;
    $categories = array();
    $page = 1;

    $categories = array();

    while($send_call == true) 
    {

        $result_categories = $woocommerce->get('products/categories', array('per_page'=>$per_page, 'page'=>$page,'context' => 'view'));
        $page++;
        $categories = array_merge( $categories, $result_categories );

        if( count($result_categories) < $per_page )
        {
            $send_call=false;
        }

    }

    $return_woo_categories = array();

    foreach($categories as $index => $category)
    {
        $return_woo_categories[] = array('name' => $category['name'], 'id' => $category['id'], 'parent' => $category['parent']);
    }

$return_woo_categories 的 var_dump

array(13) {
  [0]=>
  array(3) {
    ["name"]=>
    string(1) "/"
    ["id"]=>
    int(23)
    ["parent"]=>
    int(20)
  }
  [1]=>
  array(3) {
    ["name"]=>
    string(1) "+"
    ["id"]=>
    int(21)
    ["parent"]=>
    int(20)
  }
  [2]=>
  array(3) {
    ["name"]=>
    string(6) "Albums"
    ["id"]=>
    int(20)
    ["parent"]=>
    int(16)
  }
  [3]=>
  array(3) {
    ["name"]=>
    string(18) "Castle On The Hill"
    ["id"]=>
    int(24)
    ["parent"]=>
    int(23)
  }
  [4]=>
  array(3) {
    ["name"]=>
    string(8) "Clothing"
    ["id"]=>
    int(14)
    ["parent"]=>
    int(0)
  }
  [5]=>
  array(3) {
    ["name"]=>
    string(7) "Hoodies"
    ["id"]=>
    int(15)
    ["parent"]=>
    int(14)
  }
  [6]=>
  array(3) {
    ["name"]=>
    string(5) "Music"
    ["id"]=>
    int(16)
    ["parent"]=>
    int(0)
  }
  [7]=>
  array(3) {
    ["name"]=>
    string(10) "My New Cat"
    ["id"]=>
    int(6)
    ["parent"]=>
    int(0)
  }
  [8]=>
  array(3) {
    ["name"]=>
    string(7) "Posters"
    ["id"]=>
    int(17)
    ["parent"]=>
    int(0)
  }
  [9]=>
  array(3) {
    ["name"]=>
    string(12) "Shape of You"
    ["id"]=>
    int(25)
    ["parent"]=>
    int(23)
  }
  [10]=>
  array(3) {
    ["name"]=>
    string(7) "Singles"
    ["id"]=>
    int(18)
    ["parent"]=>
    int(16)
  }
  [11]=>
  array(3) {
    ["name"]=>
    string(8) "T-shirts"
    ["id"]=>
    int(19)
    ["parent"]=>
    int(14)
  }
  [12]=>
  array(3) {
    ["name"]=>
    string(1) "x"
    ["id"]=>
    int(22)
    ["parent"]=>
    int(20)
  }

我需要一个数据结构作为输出如下。 (键的顺序无关紧要)。

$cat_map = array(
    'Clothing' => 14,
    'Clothing|Hoodies' => 15,
    'Clothing|T-shirts' => 19,
    'My New Cat' => 6,
    'Posters' => 17,
    'Music' => 16,
    'Music|Singles' => 18,
    'Music|Albums' => 20,
    'Music|Albums|x' => 22,
    'Music|Albums|/' => 23,
    'Music|Albums|+' => 21,
    'Music|Albums|/|Castle On The Hill' => 24,
    'Music|Albums|/|Shape Of You' => 25,
);

我不知道该怎么做;我会假设我需要递归;但让我陷入困境的是我从 woo commerce 返回的数组的顺序不是任何等级的方式。 (我添加的时间顺序是最近的)

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    最简单的方法是先构建树(嵌套)结构,然后使用递归函数连接字符串:

    $tree = array();
    foreach ($return_woo_categories as $cat) {
        if (!isset($tree[$cat['id']])) { $tree[$cat['id']] = array(); }
        $tree[$cat['id']]['name'] = $cat['name'];
        if (!isset($tree[$cat['parent']])) { $tree[$cat['parent']] = array(); }
        $tree[$cat['parent']]['children'][$cat['id']] =& $tree[$cat['id']];
    }
    
    function buildPaths($tree, $path = '') {
        $result = array();
        foreach ($tree as $id => $cat) {
            $result[$id] = $path . $cat['name'];
            if (isset($cat['children'])) {
                $result += buildPaths($cat['children'], $result[$id] . '|');
            }
        }
        return $result;
    }
    
    $cat_map = buildPaths($tree[0]['children']);
    

    $cat_map 具有[id] =&gt; 'path' 结构,因此必须翻转它以匹配您所需的结果。使用 id 访问路径比相反更方便,但如果你真的需要这种结构,那么最后一行是:

    $cat_map = array_flip(buildPaths($tree[0]['children']));
    

    【讨论】:

      【解决方案2】:

      我会做一个3级循环来检索

      1. 获取父类别
      2. 获取子类别
      3. 获取子子类别

      在每个循环中,我将 "|" 连接起来,然后将 key => 值添加到每个循环中 级别。

      以下代码提供了预期的数组

      ////////////////////////////////////////////////////
       //creating new array to store new structure
       $cat_map = [] ;
      //3 level loop to get desired info
      foreach($return_woo_categories as $inf){
       //1 loop to get parent categories
          if($inf['parent'] == 0){
                   $parentcategoryname = $inf['name'];           
                  $cat_map[$parentcategoryname] = $inf['id'];          
                  //2 loop to get 1 child category
                      foreach($return_woo_categories as $inf2){
                          if($inf['id'] == $inf2['parent'] ){ 
                          //concat first level and second level name               
                          $firstchildname = $inf['name']."|".$inf2['name'];
                              $cat_map[$firstchildname] = $inf2['id'];                 
      
                          //3 loop to get 2 child category
                           foreach($return_woo_categories as $inf3){
      
                              if($inf2['id'] == $inf3['parent'] ){
      
                               $secondchildname = $inf['name']."|".$inf2['name']."|".$inf3['name'];   
                                $cat_map[$secondchildname] = $inf3['id'];    
      
                              }
                           }
                          }               
                      }        
              }
      }
      //Result
      var_dump( $cat_map);
      array (size=11)
        'Clothing' => int 14
        'Clothing|Hoodies' => int 15
        'Clothing|T-shirts' => int 19
        'Music' => int 16
        'Music|Albums' => int 20
        'Music|Albums|/' => int 23
        'Music|Albums|+' => int 21
        'Music|Albums|x' => int 22
        'Music|Singles' => int 18
        'My New Cat' => int 6
        'Posters' => int 17
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-20
        • 1970-01-01
        • 1970-01-01
        • 2010-09-16
        • 2021-09-05
        • 2018-10-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多