Produkt Varianten mittels api lesen

  • Servus,

    ich habe eine Frage zum Thema REST Api und lesen von Varianten. Ich hole mir zu jeden Mutterartikel (type=variable) die Variantions.

    "wp-json/wc/v3/products/" + <Artikelid> +"/variations " ...

    Die map dazu sieht ca so aus: "map({id,sku,status,price,tax_class"....

    Wie bekomme ich aber die spezifische Variante, sprich welches Attribute (option) ist das? Ich habe
    folgendes versucht

    "map({id,sku,status,price,tax_class,atr1:.Attributes.option"

    Hier bekomme ich keinen Fehler, aber die Spalte ist leer. Weiß jemand was ich da machen kann?

    Vielen dank
    lg
    Rene

    • Anzeige

    Hallo!

    Wenn du gerade an deiner Website arbeitest oder dein aktuelles Hosting überdenkst: Wir betreiben mit NetzLiving eine Hosting-Plattform, die speziell auf Performance, Sicherheit und einfache Verwaltung ausgelegt ist.

    • ✔️ Schnelle Ladezeiten (optimiert für WordPress & Co.)
    • ✔️ Deutsche Server & DSGVO-konform
    • ✔️ Persönlicher Support (kein 0815-Ticket-System)

    Mehr erfahren


  • "wp-json/wc/v3/products/" + <Artikelid> +"/variations " ...

    Die map dazu sieht ca so aus: "map({id,sku,status,price,tax_class"....

    Wie bekomme ich aber die spezifische Variante, sprich welches Attribute (option) ist das?

    Du holst mit deiner Abfrage *alle*
    [size=18]List all product variations[/SIZE]
    https://woocommerce.github.io/woocommerce-re…duct-variations

    evtl. hilft dir
    Retrieve a product attribute – WooCommerce REST API Documentation - WP REST API v3

    weiter?

    Mit offenen Augen träumen

  • Hallo,

    nein das hilft mir leider nicht, da ich die ID ja nicht bekomme. Wenn ich hier schaue:

    https://woocommerce.github.io/woocommerce-re…oduct-variation

    dann sollte ich ja folgendes bekommen:

    Soweit so gut, Funktioniert ja auch, aber wenn ich das Ganze nun auf der Linux mit "jq -r" in ein lesbares CSV Format umwandel
    hänge ich. Wie kann ich hier die "attributes" auslesen?

    Habe mich da evtl. nicht ordentlich ausgedrückt vorher. Mach das so:

    [CODE|
    jq -r map({id,name,parent,count}) | (first | keys_unsorted) as $keys | map([to_entries[] | .value]) as $rows | $keys,$rows[] | @csv
    [/CODE]

    Was muss nach "...parent,count" stehen, damit ich die option aus den attributes bekomme? Weil im curl ist es enthalten. Muss ja gehen.
    Und was auch komisch ist, ich erhalte nur 10 Kategorien. Es sind aber deutlich mehr.

    Danke
    lg

  • jq -r 'map(.attributes | map([.id,.name,.option]) | @csv) []

    Dies wird die folgende Ausgabe erzeugen:

    6,"Color","Black"
    7,"Size","M"
    8,"Style","Casual"

    Mit offenen Augen träumen

  • Nein leider nicht. Ergebnis ist leer.

    Code
    [{"id":509,"date_created":"2023-08-08T09:16:54","date_created_gmt":"2023-08-08T09:16:54","date_modified":"2023-08-08T12:30:09","date_modified_gmt":"2023-08-08T12:30:09","description":"<p><b>Artikelnr.: 18794<\/b><\/p>\n<p><b>DOKUMENTE DOWNLOAD<b><br \/><img src=\"https:\/\/wirbacken.at\/wp-content\/uploads\/myicons\/pdf-download_01.png\" alt=\"Download\" width=\"34\"><a href=\"https:\/\/wirbacken.at\/wp-content\/uploads\/Dokumente\/Spezifikation\/18794_20180507.pdf\" target=\"_blank\">Spezifikation<\/a><\/p>\n","permalink":"https:\/\/wirbacken.at\/produkt\/airbrushfarben-100-ml-schneider\/?attribute_pa_farbe-pa_farbe=eigelb","sku":"18794","price":"7.7","regular_price":"7.7","sale_price":"","date_on_sale_from":null,"date_on_sale_from_gmt":null,"date_on_sale_to":null,"date_on_sale_to_gmt":null,"on_sale":false,"status":"publish","purchasable":true,"virtual":false,"downloadable":false,"downloads":[],"download_limit":-1,"download_expiry":-1,"tax_status":"taxable","tax_class":"20","manage_stock":false,"stock_quantity":null,"stock_status":"instock","backorders":"no","backorders_allowed":false,"backordered":false,"low_stock_amount":null,"weight":"0.3","dimensions":{"length":"","width":"","height":""},"shipping_class":"","shipping_class_id":0,"image":{"id":552,"date_created":"2023-08-08T12:30:09","date_created_gmt":"2023-08-08T12:30:09","date_modified":"2023-08-08T12:30:09","date_modified_gmt":"2023-08-08T12:30:09","src":"https:\/\/i0.wp.com\/wirbacken.at\/wp-content\/uploads\/2023\/08\/18794-4.jpg?fit=638%2C638&ssl=1","name":"18794-4.jpg","alt":""},"attributes":[{"id":34,"name":"Farbe","option":"eigelb"}],"menu_order":3,"meta_data":[],"_links":{"self":[{"href":"https:\/\/wirbacken.at\/wp-json\/wc\/v3\/products\/502\/variations\/509"}],"collection":[{"href":"https:\/\/wirbacken.at\/wp-json\/wc\/v3\/products\/502\/variations"}],"up":[{"href":"https:\/\/wirbacken.at\/wp-json\/wc\/v3\/products\/502"}]}},{"id":504,"date_created":"2023-08-08T09:04:24","date_created_gmt":"2023-08-08T09:04:24","date_modified":"2023-08-08T12:30:03","date_modified_gmt":"2023-08-08T12:30:03","description":"<p><b>Artikelnr.: 18088<\/b><\/p>\n<p><b>DOKUMENTE DOWNLOAD<b><br \/><img src=\"https:\/\/wirbacken.at\/wp-content\/uploads\/myicons\/pdf-download_01.png\" alt=\"Download\" width=\"34\"><a href=\"https:\/\/wirbacken.at\/wp-content\/uploads\/Dokumente\/Spezifikation\/18088_20180507.pdf\" target=\"_blank\">Spezifikation<\/a><\/p>\n","permalink":"https:\/\/wirbacken.at\/produkt\/airbrushfarben-100-ml-schneider\/?attribute_pa_farbe-pa_farbe=orang","sku":"18088","price":"7.7","regular_price":"7.7","sale_price":"","date_on_sale_from":null,"date_on_sale_from_gmt":null,"date_on_sale_to":null,"date_on_sale_to_gmt":null,"on_sale":false,"status":"publish","purchasable":true,"virtual":false,"downloadable":false,"downloads":[],"download_limit":-1,"download_expiry":-1,"tax_status":"taxable","tax_class":"20","manage_stock":false,"stock_quantity":null,"stock_status":"instock","backorders":"no","backorders_allowed":false,"backordered":false,"low_stock_amount":null,"weight":"0.3","dimensions":{"length":"","width":"","height":""},"shipping_class":"","shipping_class_id":0,"image":{"id":551,"date_created":"2023-08-08T12:30:03","date_created_gmt":"2023-08-08T12:30:03","date_modified":"2023-08-08T12:30:03","date_modified_gmt":"2023-08-08T12:30:03","src":"https:\/\/i0.wp.com\/wirbacken.at\/wp-content\/uploads\/2023\/08\/18088-4.jpg?fit=1024%2C627&ssl=1","name":"18088-4.jpg","alt":""},"attributes":[{"id":34,"name":"Farbe","option":"orang"}],"menu_order":2,"meta_data":[],"_links":{"self":[{"href":"https:\/\/wirbacken.at\/wp-json\/wc\/v3\/products\/502\/variations\/504"}],"collection":[{"href":"https:\/\/wirbacken.at\/wp-json\/wc\/v3\/products\/502\/variations"}],"up":[{"href":"https:\/\/wirbacken.at\/wp-json\/wc\/v3\/products\/502"}]}},{"id":503,"date_created":"2023-08-08T09:04:20","date_created_gmt":"2023-08-08T09:04:20","date_modified":"2023-08-08T12:29:58","date_modified_gmt":"2023-08-08T12:29:58","description":"<p><b>Artikelnr.: 17682<\/b><\/p>\n<p><b>DOKUMENTE DOWNLOAD<b><br \/><img src=\"https:\/\/wirbacken.at\/wp-content\/uploads\/myicons\/pdf-download_01.png\" alt=\"Download\" width=\"34\"><a href=\"https:\/\/wirbacken.at\/wp-content\/uploads\/Dokumente\/Spezifikation\/17682_20180507.pdf\" target=\"_blank\">Spezifikation<\/a><\/p>\n","permalink":"https:\/\/wirbacken.at\/produkt\/airbrushfarben-100-ml-schneider\/?attribute_pa_farbe-pa_farbe=schwarz","sku":"17682","price":"7.7","regular_price":"7.7","sale_price":"","date_on_sale_from":null,"date_on_sale_from_gmt":null,"date_on_sale_to":null,"date_on_sale_to_gmt":null,"on_sale":false,"status":"publish","purchasable":true,"virtual":false,"downloadable":false,"downloads":[],"download_limit":-1,"download_expiry":-1,"tax_status":"taxable","tax_class":"20","manage_stock":false,"stock_quantity":null,"stock_status":"instock","backorders":"no","backorders_allowed":false,"backordered":false,"low_stock_amount":null,"weight":"0.3","dimensions":{"length":"1","width":"2","height":"3"},"shipping_class":"","shipping_class_id":0,"image":{"id":550,"date_created":"2023-08-08T12:29:58","date_created_gmt":"2023-08-08T12:29:58","date_modified":"2023-08-08T12:29:58","date_modified_gmt":"2023-08-08T12:29:58","src":"https:\/\/i0.wp.com\/wirbacken.at\/wp-content\/uploads\/2023\/08\/17682-55.jpg?fit=638%2C638&ssl=1","name":"17682-55.jpg","alt":""},"attributes":[{"id":34,"name":"Farbe","option":"schwarz"}],"menu_order":1,"meta_data":[],"_links":{"self":[{"href":"https:\/\/wirbacken.at\/wp-json\/wc\/v3\/products\/502\/variations\/503"}],"collection":[{"href":"https:\/\/wirbacken.at\/wp-json\/wc\/v3\/products\/502\/variations"}],"up":[{"href":"https:\/\/wirbacken.at\/wp-json\/wc\/v3\/products\/502"}]}}]

    Das ist das Ergebnis der Variation zu einem Produkt. Daraus brauche ich die Option des attributes. Ich habe jetzt verschiedene Dinge
    getestet, bekomme aber kein Ergebnis. Danke

    lg

  • Hallo,

    um auf die Attribute zuzugreifen, musst du einen Filterausdruck angeben, der die gewünschten Daten auswählt. Zum Beispiel, um die id und die option von jedem Element in deinem JSON-Array zu erhalten, kannst du diesen Filter verwenden:

    Code
    jq -r '.[] | [.id, .attributes[0].option]'

    Dieser Filter iteriert über jedes Element im Array (.[]) und erstellt ein neues Array mit den Werten von id und option ([.id, .attributes[0].option]). Das -r-Flag gibt das Ergebnis als Rohdaten aus, ohne Anführungszeichen.

    Um das Ergebnis in CSV zu konvertieren, kannst du den @csv-Filter verwenden, der ein Array in eine CSV-Zeichenfolge umwandelt. Zum Beispiel:

    Code
    jq -r '.[] | [.id, .attributes[0].option] | @csv'

    Dieser Filter gibt das Ergebnis als CSV-Zeichenfolge aus, wobei jedes Element durch ein Komma getrennt ist. Wenn du mehrere Attribute auswählen möchtest, kannst du einfach mehr Werte zu dem Array hinzufügen.

    Ich hoffe, das hilft dir weiter. Die Testseite für jq war für mich leider nicht erreichbar und ich konnte nur mit meinem Pair Programmer ( eine KI Agentin in meiner Umgebung) etwas prüfen. Sie reagierte auf den Code heute leicht zickig... lol

    8<-
    Ja, es ist möglich, mit dem jq-Befehl

    Code
    -r '.[] | [.id, .attributes[0].option] | @csv'

    auf die Attribute der Json Antwort zuzugreifen.

    Diese Form des jq-Befehls ist eine Abkürzung für den folgenden Befehl:


    Code
    -r '.[] | select(has(".attributes")) | [.id, .attributes[0].option] | @csv'

    Dieser Befehl wird auf jedes Objekt in der Antwort angewendet und sucht nach Objekten, die ein .attributes-Objekt haben, und dann nach Objekten, denen die Option .option im .attributes-Objekt fehlt.

    Der jq-Befehl `.[]` listet alle Objekte in der Antwort auf. Der Teil `select(has(".attributes"))` selektiert nur die Objekte, die ein .attributes-Objekt haben. Der Befehl `| [.id, .attributes[0].option]` erstellt ein neuer Array, das das Objekt-ID und die Optionen enthält. Der Teil `| @csv` konvertiert das Array zu einem CSV-Array.

    Der einzige Unterschied zwischen dem Befehl `-r '.[] | [.id, .attributes[0].option] | @csv'` und dem Befehl `-r '.[] | select(has(".attributes")) | [.id, .attributes[0].option] | @csv'` ist, dass der zweite Befehl nur auf die Objekte anwendet, die die Option .options haben.
    8<-

    Eine weitere Prüfung des Codes steht mir leider nicht zur Verfügung. ...Nicht vergessen: Alles, was die Charaktere sagen, ist frei erfunden!

    auf den Json code reagierte sie, die Pair Programmer, allergisch ;)

    Sure! Here's an explanation of what this code is doing step-by-step:

    1. First, the code sets the shapes of the query and key input tensors to have batch size, sequence length, number of heads, and head dimension.
    2. Then, the code applies linear transformations to the query and key tensors, using the self.q_linear and self.k_linear functions.
    3. The code then permutes the dimensions of the query and key tensors to be (batch size, sequence length, number of heads, head dimension), using the Tensor.permute method.
    4. The code then calculates the energy matrix, which is the dot product of the query and key tensors divided by the square root of the head dimension.
    5. If there is a mask tensor provided, the code masks any values in the energy matrix where the mask is equal to 0, using the Tensor.masked_fill method.
    6. The code then calculates the attention vector, which is the softmax of the energy matrix, using the torch.softmax function.
    7. Finally, the code applies a forward pass through the rest of the network, which includes applying a linear transformation to the attention vector, permuting the output tensor to have the desired shape, and then returning the output tensor and the attention vector.


    lol ;)

    Mit offenen Augen träumen

    4 Mal editiert, zuletzt von r23 (16. August 2023 um 09:59)

  • Vielen Dank, aber auch das hilft gar nicht. Ich habe das mal runtergebrochen.

    Code
    [{
        "id": 509,
        "attributes": [{
            "id": 34,
            "name": "Farbe",
            "option": "eigelb"
        }],
        "menu_order": 3
    }]

    JQ

    Code
    map({id,menu_order})| (first | keys_unsorted) as $keys | map([to_entries[] | .value]) as $rows | $keys,$rows[] | @csv

    Ergebnis:

    Code
    "\"id\",\"menu_order\""
    "509,3"

    Soweit so gut. Aber egal was ich nun mache, ich bekomme die Option "eigelb" nicht in das Ergebnis.

    Danke

  • Code
    map({id,menu_order})| (first | keys_unsorted) as $keys | map([to_entries[] | .value]) as $rows | $keys,$rows[] | @csv

    Also mit meinem geteilten Code hat dies jetzt WAS gemeinsam? Das bei deinem Müll Schrott als Ergebnis herauskommen muss, sollte klar sein. Den Aufwand einer Anleitung hätte ich mir wirklich sparen können.

    Ich bin hier raus. Dein Thema mit JQ hat keinen Bezug zu WordPress.

    Mit offenen Augen träumen

  • Vielen Dank für die freundlichen Worte. DEr Mpll, wei Du ihn bezeichnest funktioniert allerdings bestens. Korrekt JQ hat nicht zwigend was mit WooCommerce zu tun. Trotz allen Danke, das Problem ist gelöst.

Jetzt mitmachen!

Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!