php - woocommerce 构建类别树

标签 php wordpress woocommerce

我有这个类别到我的 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/

相关文章:

php - 限制函数或命令 PHP 的执行时间

php - 存储在 session 数据中与存储在 Sql 数据库中用于临时数据

php - 如何将特定商品的 WooCommerce 购物车商品数量限制为一个

php - 在 woocommerce 中添加自定义产品类型

wordpress - WooCommerce API 的高级自定义字段

php - 通过许多 url 在源代码中查找字符串的代码

php - 如何在以下场景中操作数组?

php - 如何从 youtube 嵌入中删除更多视频?

javascript - JS 脚本无法通过 Hooks 加载到 WordPress 中

php - 将 pdf 附件添加到 WooCommerce 已完成订单电子邮件通知