woocommerce建立类别树

时间:2019-06-24 09:57:58

标签: php wordpress woocommerce

我将此类别归入wordpresswoocommerce)中,我需要使用从woocommerce api检索到的数据来构建这样的菜单。

enter image description here

构建这样的菜单的有效方法是什么?我试图检查wordpress代码,但没有弄清楚。

该API使我可以检索所有产品类别。

  

https://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-product-categories

这是从请求获得的对象:

array(5) {
  [0] => object(stdClass)#77 (10) {
    ["id"] => int(15)
    ["name"] => string(13) "Uncategorized"
    ["slug"] => string(13) "uncategorized"
    ["parent"] => int(0)
    ["description"] => string(0) ""
    ["display"] => string(7) "default"
    ["image"] => NULL
    ["menu_order"] => int(0)
    ["count"] => int(0)
    ["_links"] => object(stdClass)#89 (2) {
      ["self"] => array(1) {
        [0] => object(stdClass)#88 (1) {
          ["href"] => string(68) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories/15"
        }
      }
      ["collection"] => array(1) {
        [0] => object(stdClass)#90 (1) {
          ["href"] => string(65) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories"
        }
      }
    }
  }
  [1] => object(stdClass)#91 (10) {
    ["id"] => int(19)
    ["name"] => string(16) "SUB SUB SUB ZERO"
    ["slug"] => string(16) "sub-sub-sub-zero"
    ["parent"] => int(18)
    ["description"] => string(0) ""
    ["display"] => string(7) "default"
    ["image"] => NULL
    ["menu_order"] => int(0)
    ["count"] => int(1)
    ["_links"] => object(stdClass)#93 (3) {
      ["self"] => array(1) {
        [0] => object(stdClass)#92 (1) {
          ["href"] => string(68) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories/19"
        }
      }
      ["collection"] => array(1) {
        [0] => object(stdClass)#94 (1) {
          ["href"] => string(65) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories"
        }
      }
      ["up"] => array(1) {
        [0] => object(stdClass)#95 (1) {
          ["href"] => string(68) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories/18"
        }
      }
    }
  }
  [2] => object(stdClass)#96 (10) {
    ["id"] => int(18)
    ["name"] => string(12) "SUB-SUB ZERO"
    ["slug"] => string(12) "sub-sub-zero"
    ["parent"] => int(17)
    ["description"] => string(4) "ssss"
    ["display"] => string(7) "default"
    ["image"] => NULL
    ["menu_order"] => int(0)
    ["count"] => int(1)
    ["_links"] => object(stdClass)#98 (3) {
      ["self"] => array(1) {
        [0] => object(stdClass)#97 (1) {
          ["href"] => string(68) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories/18"
        }
      }
      ["collection"] => array(1) {
        [0] => object(stdClass)#99 (1) {
          ["href"] => string(65) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories"
        }
      }
      ["up"] => array(1) {
        [0] => object(stdClass)#100 (1) {
          ["href"] => string(68) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories/17"
        }
      }
    }
  }
  [3] => object(stdClass)#101 (10) {
    ["id"] => int(17)
    ["name"] => string(8) "SUB-ZERO"
    ["slug"] => string(8) "sub-zero"
    ["parent"] => int(16)
    ["description"] => string(3) "sub"
    ["display"] => string(7) "default"
    ["image"] => NULL
    ["menu_order"] => int(0)
    ["count"] => int(1)
    ["_links"] => object(stdClass)#103 (3) {
      ["self"] => array(1) {
        [0] => object(stdClass)#102 (1) {
          ["href"] => string(68) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories/17"
        }
      }
      ["collection"] => array(1) {
        [0] => object(stdClass)#104 (1) {
          ["href"] => string(65) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories"
        }
      }
      ["up"] => array(1) {
        [0] => object(stdClass)#105 (1) {
          ["href"] => string(68) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories/16"
        }
      }
    }
  }
  [4] => object(stdClass)#106 (10) {
    ["id"] => int(16)
    ["name"] => string(4) "ZERO"
    ["slug"] => string(8) "zeroslug"
    ["parent"] => int(0)
    ["description"] => string(11) "prova categ"
    ["display"] => string(7) "default"
    ["image"] => NULL
    ["menu_order"] => int(0)
    ["count"] => int(1)
    ["_links"] => object(stdClass)#108 (2) {
      ["self"] => array(1) {
        [0] => object(stdClass)#107 (1) {
          ["href"] => string(68) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories/16"
        }
      }
      ["collection"] => array(1) {
        [0] => object(stdClass)#109 (1) {
          ["href"] => string(65) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories"
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

最后我想出了,使用递归函数:

    private function _categoriaOrder($level = 0) {
        echo "<ul>";
        for($i = 0; $i < count($this->_mycats[$level]); $i++) {
            echo "<li style='list-style-type: none;'><label style=\"margin-bottom:0;\" for=\"woo_id_categoria_{$this->_mycats[$level][$i]['id']}\"><input value=\"{$this->_mycats[$level][$i]['id']}\" id=\"woo_id_categoria_{$this->_mycats[$level][$i]['id']}\" name=\"woo_id_categoria[]\" type=\"checkbox\">&nbsp;<span>" . $this->_mycats[$level][$i]['id'] . " " . $this->_mycats[$level][$i]['name'] . "</span></label>";
            if(array_key_exists($this->_mycats[$level][$i]['id'], $this->_mycats)) {
                $this->_categoriaOrder($this->_mycats[$level][$i]['id']);
            }
            echo "</li>";
        }
        echo "</ul>";
    }

    public function getCategoriaTree(){
        $categorie = $this->woo->getCategoria(); //here is the object with categories from woocommerce

        foreach($categorie as $k => $category){
            $this->_mycats[(int)$category->parent][] = array(
                'id' => $category->id,
                'name' => $category->name,
            );

            $categories_ordered[(int)$category->parent] = $category;
        }

        ob_start();
        $this->_categoriaOrder(); 
        $output = ob_get_contents();
        ob_end_clean();

        return $output;
    }

使用功能getCategoriaTree()来获取html。enter image description here

我希望这可以对某人有所帮助,我花了几个小时才能得到它。

相关问题