从woocommerce api产品/类别

时间:2017-01-26 20:33:41

标签: php wordpress woocommerce

我正在尝试创建一个我的应用程序可以使用的数据结构,这样我就可以在系统中映射我的类别以吸引商业结构。我只需要一个类别层次结构(如下所示),它具有类别的完整路径,每个父/子之间有一个管道,其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回来的数组的顺序并不是以任何层次结构的方式。 (我最近加入时的顺序)

2 个答案:

答案 0 :(得分: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] => 'path'结构,因此必须翻转以匹配您所需的结果。使用id访问路径比反过来更方便,但如果你真的需要这个结构,那么最后一行将是:

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

答案 1 :(得分:0)

我会一个3级循环来检索

  1. 获取父类别
  2. 获取子类别
  3. 获取子女类别
  4. 在每个循环中我连接“|”,然后添加key =&gt;每个人的价值 水平。

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

    var a = { b: 'c', d: 'e' };
    
    a.f = a.b;
    delete a.b;
    
    console.log(a);  // { d: 'e', f: 'c' }