Is er een probleem dat snel moet worden opgelost?Spoedfix nodig?Vraag spoedhulp aan →
Blog

Praktische Magento 2-gidsen over upgrades, beveiliging, snelheid en migratie, geschreven door het MageBooster-team.

Magento 2.4.9 categories and subcategories: step by step, with a REST API script

Create a parent category and a subcategory with products in Magento 2.4.9: every admin field from MageBooster lesson 3, a REST API script to download, and troubleshooting.
Lesson 3MageBooster Lessons · Magento 2.4.9

Creating categories and subcategories in Magento

A parent category and a subcategory with products, step by step in the admin: every field from the video, a REST API script that builds the same tree, and the fixes for when a category doesn’t show up.

Bijgewerkt Oktober 2026Video about 3 minNiveau Store owners and developers

Downloads

The script uses the Luma sample data SKUs; change SKUS, the names and the URL keys to your own.

The category tree

Default Category root · ID 2 · not shown itself
├─ Women menu item
├─ Men menu item
└─ Travel Gear parent · menu item · anchor
   └─ Backpacks subcategory · drop-down · 4 products

Every store view has one root category (Stores › All Stores shows which). Its children are the main menu, and their children open as drop-downs. A new category always goes under the category that is selected in the tree.

1. Parent category: Travel Gear

Catalog › Categories, select Default Category, then Add Subcategory.

SectionFieldValueWhy
Enable CategoryJaNo hides it everywhere.
Include in MenuJaNo keeps the page, but takes it out of the main menu.
Category NameTravel GearMenu text, page title, breadcrumb.
Display SettingsAnchorJaThe page also lists the products of all subcategories, with layered navigation.
Search Engine OptimizationURL Keytravel-gearThe page is /travel-gear.html.
Search Engine OptimizationMeta TitleTravel Gear: Bags and BackpacksThe title in Google results.

Save. Travel Gear appears in the tree with its ID, for example Travel Gear (ID: 41) (0).

2. Subcategory: Backpacks

With Travel Gear selected, Add Subcategory again and name it Backpacks. Open Products in Category, type Backpack in the Name filter, press Enter and tick the products. Save.

FieldValue
Category NameBackpacks
URL Keybackpacks (filled in from the name), so the page is /travel-gear/backpacks.html
Products in CategoryDriven Backpack, Endeavor Daytrip Backpack, Fusion Backpack, Crown Summit Backpack
The number after the ID in the tree is the product count: Backpacks (ID: 42) (4).

3. On the storefront

Travel Gear is in the main menu, Backpacks opens under it, and the breadcrumb reads Home › Travel Gear › Backpacks. Travel Gear has no products of its own, but as an anchor it lists the four backpacks.

On a live store the cron job updates the indexes and caches within a minute or two. To see the result right away:

cd /var/www/magento
# are the indexers on "Update by Schedule", and is anything waiting?
sudo -u magento bin/magento indexer:status
# the cron job does the work on a live store; to see the result right now:
sudo -u magento bin/magento indexer:reindex catalog_category_product catalog_product_category catalogsearch_fulltext
sudo -u magento bin/magento cache:clean block_html full_page

The URLs Magento created:

# the URLs Magento made for the new categories
mysql magento -e "SELECT request_path, target_path FROM url_rewrite
  WHERE entity_type = 'category' AND request_path LIKE 'travel-gear%';"

The same tree by REST API

Useful when you set up many stores or move a category structure from a test store. The script logs in with the admin user and the code from your authenticator app, creates both categories and adds the products by SKU. Needs curl en jq.

magento-create-categories.shDownload

#!/usr/bin/env bash
# Build the lesson's category tree through the Magento REST API:
#   Default Category (root, ID 2) > Travel Gear (anchor) > Backpacks (+ products by SKU)
# usage: STORE=https://www.example.com ADMIN_USER=youradmin ./magento-create-categories.sh
# Needs curl and jq. Asks for the admin password and the 6-digit code from your authenticator app
# (Magento 2.4 admin accounts use two-factor authentication).
set -euo pipefail
STORE=${STORE:?set STORE, e.g. https://www.example.com}
ADMIN_USER=${ADMIN_USER:?set ADMIN_USER}
ROOT_ID=${ROOT_ID:-2}                                    # Default Category
SKUS=${SKUS:-"24-WB03 24-WB06 24-MB02 24-MB03"}          # Luma sample backpacks
CURL=(curl -sS --fail-with-body ${INSECURE:+-k})         # INSECURE=1 for a self-signed test certificate

read -rsp "Admin password: " PASS; echo
read -rp  "Authenticator code: " OTP
TOKEN=$("${CURL[@]}" -X POST "$STORE/rest/V1/tfa/provider/google/authenticate" -H 'Content-Type: application/json' \
  -d "$(jq -n --arg u "$ADMIN_USER" --arg p "$PASS" --arg o "$OTP" '{username:$u,password:$p,otp:$o}')" | jq -r .)
API() { "${CURL[@]}" -X "$1" "$STORE/rest/all/V1/$2" -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' ${3:+-d "$3"}; }

new_category() {   # name parent url_key [meta_title] -> prints the new ID
  API POST categories "$(jq -n --arg n "$1" --argjson p "$2" --arg k "$3" --arg m "${4:-}" '{category:{
    name:$n, parent_id:$p, is_active:true, include_in_menu:true,
    custom_attributes:([{attribute_code:"url_key",value:$k},{attribute_code:"is_anchor",value:"1"}]
      + (if $m == "" then [] else [{attribute_code:"meta_title",value:$m}] end))}}')" | jq -r .id
}

PARENT=$(new_category "Travel Gear" "$ROOT_ID" travel-gear "Travel Gear: Bags and Backpacks")
echo "Travel Gear: ID $PARENT"
CHILD=$(new_category "Backpacks" "$PARENT" backpacks)
echo "Backpacks:   ID $CHILD"
for sku in $SKUS; do
  API POST "categories/$CHILD/products" "$(jq -n --arg s "$sku" --argjson c "$CHILD" '{productLink:{sku:$s,category_id:$c,position:0}}')" >/dev/null
  echo "  + $sku"
done
echo "Done. Open $STORE/travel-gear/backpacks.html (the menu shows it after the next cron run or a cache flush)."

Run: STORE=https://www.example.com ADMIN_USER=youradmin bash magento-create-categories.sh. Add INSECURE=1 for a self-signed test certificate.

Good to know

  • Move a category by drag and drop in the tree. Magento rewrites the URLs and, with “Create Permanent Redirect for URLs if URL Key Changed” on, adds 301 redirects.
  • Keep the tree shallow. Two or three levels are enough for most stores; deep trees make menus and breadcrumbs long.
  • Hide instead of delete. Enable Category = No, or Include in Menu = No. Deleting a category removes its URL rewrites and position data for good.
  • Anchor for parent categories, off only when a parent should show its own hand-picked products.

Troubleshooting

SymptoomOorzaak en oplossing
New category missing from the menuCached menu block or full page cache. Wait for cron, or run cache:clean block_html full_page. Check Include in Menu.
Category page is emptyIndexers on schedule and cron not running: indexer:status, then fix cron. Products must be enabled, in stock and visible in Catalog.
Parent page shows no productsAnchor is No and the parent has no products of its own: set Anchor = Yes.
404 on the category URLURL key changed or rewrite missing: check the url_rewrite query above; save the category again to regenerate.
Category not in the tree you expectIt was created under the selected category. Drag it to the right parent.
Planning a catalogue structure?We design category trees, filters and URLs for Magento and Mage-OS stores, and migrate them from other platforms.

▶ Watch every MageBooster lesson on YouTube: all lessons in order · subscribe to the channel

Deze pagina is automatisch vertaald. Zie je een fout? Laat het ons weten, dan passen we het meteen aan.

Gratis webshop-audit

Stuur ons de gegevens van je webshop. We controleren snelheid, beveiliging en extensies en sturen je binnen 3 werkdagen drie quick wins.

Laten we praten

Vraag een offerte aan

Vertel ons over je webshop, dan reageren we binnen één werkdag. Liever chatten? Stuur ons een WhatsApp

MageBooster Boek een UnoPim-demo Bekijk UnoPim live met producten zoals de jouwe. Een nieuwe PIM of een overstap van Akeneo: we sturen je een plan, een planning en een offerte.
✓ Officiële UnoPim-partner✓ AI-agent ingericht voor je team✓ Gekoppeld aan Magento
MageBooster Start je Hyvä-project Vanaf nul of vanuit je huidige webshop. We reageren met een projectplan, een tijdlijn en een offerte.
✓ Design en structuur door ons✓ Voor Hyvä geoptimaliseerde server✓ Bonusmodules inbegrepen

MageBooster

Wij nemen contact met je op

Laat je gegevens en het adres van je webshop achter. We bekijken eerst je webshop en nemen binnen één werkdag contact op, via telefoon, WhatsApp of e-mail, wat jij het liefst hebt.

Word partner