[英]WooCommerce - Get product variation SKU in loop
我正在嘗試獲取產品變體 SKU,但它總是返回空白。 到目前為止,我已經嘗試了所有我能想到的東西,但在這里我找不到任何答案。
foreach ($available_variations as $variation) {
$variation_id = $variation->ID;
$variant = new WC_Product_Variation($variation_id);
// Get data from the product variation
$excerpt_var = wpautop($variation['variation_description']);
$sku = $variation['sku'];
$price = $variation['display_regular_price'];
$price = number_format($price, 0, ',', ' ');
if ($price != null) {
$price = $price . ",-";
}
$results[] = array(
"id" => $available_variations,
"cat" => $cat->name,
"sku" => $sku,
"title" => $title,
"price" => $price != null ? $price : "",
"excerpt" => $excerpt_var,
"thumbnail" => $thumbnail,
"type" => "variation"
);
}
獲取所有產品和變化的整個 function 在這里:
function get_all_prods_for_csv() {
$results = array(); // Rows to be returned
// Settings
$taxonomy = 'product_cat';
$title = '';
$empty = 0;
// Query arguments
$args_cat = array(
'taxonomy' => $taxonomy,
'orderby' => 'menu_order',
'show_count' => 0, // 1 for yes, 0 for no
'pad_counts' => 0, // 1 for yes, 0 for no
'hierarchical' => 0, // 1 for yes, 0 for no
'title_li' => $title,
'hide_empty' => $empty
);
// For all categories
foreach (get_categories( $args_cat ) as $cat) {
// If root category
if ($cat->category_parent === 0) {
$category_id = $cat->term_id;
$category_slug = $cat->slug;
$cat_thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$cat_image = wp_get_attachment_url( $cat_thumbnail_id );
$results[] = array(
"id" => $category_id,
"cat" => $cat->name,
"sku" => '',
"title" => '',
"price" => '',
"excerpt" => category_description( $category_id ),
"thumbnail" => $thumbnail,
"type" => "category"
);
// Get all products for current category
// Query arguments
$args_prod = array(
'post_type' => 'product',
'posts_per_page' => -1,
'order' => 'asc',
'orderby' => 'menu_order',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $category_id
)
)
);
$products = get_posts( $args_prod );
// For all products
foreach ( $products as $prod ) {
$product = wc_get_product($prod->ID);
// Get data from the product
$title = $product->get_title();
$thumbnail = get_the_post_thumbnail_url($prod->ID);
$excerpt = wpautop($product->get_short_description()); // wpautop(get_the_excerpt($prod->ID));
// If single variation
if( $product->is_type( 'simple' ) ) {
$price = $product->get_price();
$price = number_format($price, 0, ',', ' ');
$results[] = array(
"id" => $prod->ID,
"cat" => $cat->name,
"sku" => $product->get_sku(),
"title" => $title,
"price" => $price != null ? $price . "" : "",
"excerpt" => $excerpt,
"thumbnail" => $thumbnail,
"type" => "simple"
);
} else if ( $product->is_type( 'variable' ) ) {
$available_variations = $product->get_available_variations();
$counting_variations = 1;
foreach ($available_variations as $variation) {
$counting_variations++;
}
$results[] = array(
"id" => $prod->ID,
"cat" => $cat->name,
"sku" => "",
"title" => $title,
"price" => "",
"excerpt" => $excerpt,
"thumbnail" => $thumbnail,
"type" => "variation_root"
);
foreach ($available_variations as $variation) {
$variation_id = $variation->ID;
$variant = new WC_Product_Variation($variation_id);
// Get data from the product variation
$excerpt_var = wpautop($variation['variation_description']);
$sku = $variation['sku'];
$price = $variation['display_regular_price'];
$price = number_format($price, 0, ',', ' ');
if ($price != null) {
$price = $price . ",-";
}
$results[] = array(
"id" => $available_variations,
"cat" => $cat->name,
"sku" => $sku,
"title" => $title,
"price" => $price != null ? $price : "",
"excerpt" => $excerpt_var,
"thumbnail" => $thumbnail,
"type" => "variation"
);
}
}
}
}
}
return $results;
}
這條線
$sku = $variation['sku'];
應該
$sku = $variant['sku'];
因為您將 object 作為變量$variant
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.