WooCommerce-Build-Kategoriebaum

Lesezeit: 4 Minuten

Ich habe diese Kategorien in meiner wordpress (woocommerce), und ich muss ein Menü wie dieses mit abgerufenen Daten erstellen woocommerce api.

Geben Sie hier die Bildbeschreibung ein

Was ist ein effizienter Weg, um ein solches Menü zu erstellen? Ich habe versucht, den WordPress-Code zu inspizieren, ohne es herauszufinden.

Mit dieser API kann ich alle Produktkategorien abrufen.

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

Dies ist das Objekt, das von der Anfrage erhalten wurde:

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"
        }
      }
    }
  }
}

Schließlich habe ich mit einer rekursiven Funktion herausgefunden:

    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;
    }

nutze die Funktion getCategoriaTree() zum Abrufen von HTML.Geben Sie hier die Bildbeschreibung ein

Ich hoffe, das könnte jemandem helfen, ich habe einige Stunden damit verbracht, es zu bekommen.

1229590cookie-checkWooCommerce-Build-Kategoriebaum

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy