gpt4 book ai didi

php - woocommerce 构建类别树

转载 作者:行者123 更新时间:2023-12-04 13:01:15 27 4
gpt4 key购买 nike

我有这个类别到我的 wordpress ( woocommerce ),我需要使用从 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"
}
}
}
}
}

最佳答案

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

    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

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

关于php - woocommerce 构建类别树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56734136/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com