Ich habe eine Liste von Produkten, die Variationen in einer eigenen Zeile zeigen sollten. Der Code dafür funktioniert bisher einwandfrei.
Aber ich konnte nicht herausfinden, wie ich die SKU einer Variation anzeigen könnte.
Hier mein aktueller Code:
$args = [
'status' => array('publish', 'draft'),
'orderby' => 'name',
'order' => 'ASC',
'limit' => -1,
];
$vendor_products = wc_get_products($args);
$list_array = array();
foreach ($vendor_products as $key => $product) {
if ($product->get_type() == "variable") {
foreach ($product->get_variation_attributes() as $variations) {
foreach ($variations as $variation) {
$list_array[] = array(
'SKU' => $product->get_sku(),
'Name' => $product->get_title() . " - " . $variation,
);
}
}
} else {
$list_array[] = array(
'SKU' => $product->get_sku(),
'Name' => $product->get_title(),
);
}
}
return $list_array;
Ich habe versucht, die Produktattribute anzuzeigen, und ich habe auch versucht, die ID der Variation zu erhalten. Bei mir geht nichts?!
Gibt es keine einfache Möglichkeit, die Variations-SKU zu erhalten?
Solltest du besser nutzen WC_Product_Variable
get_children()
Methode wie:
$args = [
'status' => array('publish', 'draft'),
'orderby' => 'name',
'order' => 'ASC',
'limit' => -1,
];
$vendor_products = wc_get_products($args);
$list_array = array();
foreach ($vendor_products as $key => $product) {
if ( $product->is_type( "variable" ) ) {
foreach ( $product->get_children( false ) as $child_id ) {
// get an instance of the WC_Variation_product Object
$variation = wc_get_product( $child_id );
if ( ! $variation || ! $variation->exists() ) {
continue;
}
$list_array[] = array(
'SKU' => $variation->get_sku(),
'Name' => $product->get_name() . " - " . $child_id,
);
}
} else {
$list_array[] = array(
'SKU' => $product->get_sku(),
'Name' => $product->get_name(),
);
}
}
return $list_array;
Oder sogar einige andere verfügbare Methoden wie get_available_variations()
(welche verwenden get_children()
Methode beim Betrachten des Quellcodes). Das sollte besser funktionieren…
Targeting von Produktvariationen eines variablen Produkts mit anderen Post-Status als “publish"
.
In diesem Fall sollten Sie die Methode get_children() durch eine benutzerdefinierte WP_Query ersetzen, die andere Post-Status wie unten verarbeitet:
$statuses = array('publish', 'draft');
// Args on the main query for WC_Product_Query
$args = [
'status' => $statuses,
'orderby' => 'name',
'order' => 'ASC',
'limit' => -1,
];
$vendor_products = wc_get_products($args);
$list_array = array();
foreach ($vendor_products as $key => $product) {
if ($product->get_type() == "variable") {
// Args on product variations query for a variable product using a WP_Query
$args2 = array(
'post_parent' => $product->get_id(),
'post_type' => 'product_variation',
'orderby' => array( 'menu_order' => 'ASC', 'ID' => 'ASC' ),
'fields' => 'ids',
'post_status' => $statuses,
'numberposts' => -1,
);
foreach ( get_posts( $args2 ) as $child_id ) {
// get an instance of the WC_Variation_product Object
$variation = wc_get_product( $child_id );
if ( ! $variation || ! $variation->exists() ) {
continue;
}
$list_array[] = array(
'SKU' => $variation->get_sku(),
'Name' => $product->get_name() . " - " . $child_id,
);
}
} else {
$list_array[] = array(
'SKU' => $product->get_sku(),
'Name' => $product->get_name(),
);
}
}
return $list_array;
Diesmal erhalten Sie alle Produktvariationen Ihrer variablen Produkte.
Als Referenz: Produkt -> Get Children gibt nicht alle Variationen #13469 zurück
.
wc_get_products() ist nur für den Typ “product”, aber nicht für den Typ product_variation
– LoicTheAztec
8. April 20 um 9:21 Uhr
Ah ok, gibt es eine andere Möglichkeit, innerhalb meiner foreach-Schleife an die SKU zu gelangen?
– Kräh
8. April 20 um 9:28 Uhr
Sie möchten also alle Variations-Skus aus den variablen Produkten erhalten, da Sie alle “Produkt” -Posttypen abfragen?
– LoicTheAztec
8. April 20 um 9:32 Uhr
Ja, irgendwie. Ich benötige die SKU der Variation anstelle der des übergeordneten Produkts. Ich habe bereits die Variation im Titel hinzugefügt, konnte aber keine anderen Dinge wie ID oder SKU abrufen
– Kräh
8. April 20 um 9:35 Uhr