php - woocommerce 构建类别树
问题描述
我的wordpress
( woocommerce
) 中有这个类别,我需要使用从woocommerce api
.
建立这样一个菜单的有效方法是什么?我试图在没有弄清楚的情况下检查 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\"> <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 的功能。
我希望这可以帮助某人,我花了几个小时才得到它。
推荐阅读
- python - TypeError('找不到合适的 SharedVariable 构造函数。') 使用千层面和 theano
- html - CSS背景颜色和颜色标签不起作用
- python - 从源代码构建张量流
- cython - 在 cython 中使用 MKL 失败,在来自 MKL 的共享对象中有未定义的符号,但直接执行没问题
- python - 使用 Python 从数据框中用逗号替换点
- python - 求解隐式解
- spring-boot - 引起:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“thymeleafViewResolver”的bean时出错
- php - 在 WooCommerce 我的帐户中显示属于特定类别的产品
- wordpress - 更改了 Wordpress.com 网站中的 URL 并破坏了网站
- c - C:标准输入和文件输入,如何输入字符串并在文件中显示新行