WooCommerce:获取所有产品变体的 SKU
WooCommerce: Get SKUs of all product variations
我有一个产品列表,应该在它自己的行中显示变化。
到目前为止,该代码工作正常。
但我不知道如何显示变体的 SKU。
这是我当前的代码:
$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;
我尝试显示产品属性,还尝试获取变体的 ID。
什么都不适合我?!
有没有简单的方法来获取变体SKU?
您最好使用 WC_Product_Variable
get_children()
方法,例如:
$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;
甚至一些其他可用的方法,如 get_available_variations()
(在查看其源代码时使用 get_children()
方法)。它应该更好地工作...
针对具有不同于“publish"
.
的其他 post 状态的可变产品的产品变体
在这种情况下,您应该将 get_children() 方法替换为将处理其他 post 状态的自定义 WP_Query 方法,如下所示:
$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;
这次您将获得可变产品的所有产品变体。
供参考:Product -> Get Children doesn't return all variations #13469
我有一个产品列表,应该在它自己的行中显示变化。 到目前为止,该代码工作正常。
但我不知道如何显示变体的 SKU。
这是我当前的代码:
$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;
我尝试显示产品属性,还尝试获取变体的 ID。 什么都不适合我?!
有没有简单的方法来获取变体SKU?
您最好使用 WC_Product_Variable
get_children()
方法,例如:
$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;
甚至一些其他可用的方法,如 get_available_variations()
(在查看其源代码时使用 get_children()
方法)。它应该更好地工作...
针对具有不同于“publish"
.
在这种情况下,您应该将 get_children() 方法替换为将处理其他 post 状态的自定义 WP_Query 方法,如下所示:
$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;
这次您将获得可变产品的所有产品变体。
供参考:Product -> Get Children doesn't return all variations #13469