2016-04-03 140 views
1

我想要獲得WooCommerce中的所有產品數據(產品sku,名稱,價格,庫存量,可用性等)我可以使用wp-query來完成這項工作嗎?如何以編程方式獲取WooCommerce中的所有產品?

+0

在woocommerce插件的所有模板已經存在找到它並將代碼複製到您的自定義模板e ..他們是用meta添加的自定義帖子類型u也可以使用自定義帖子類型查詢並獲得帖子meta方法 –

+2

只是一個友好的提示,您可能想要閱讀本頁:[如何提問](https: //stackoverflow.com/help/how-to-ask),因此您始終可以確定您的問題很容易回答並儘可能清晰。一定要包括你爲解決你遇到的問題所做的任何努力,以及當你嘗試修復這些問題時發生了什麼。另外不要忘記你的代碼和任何錯誤信息! –

+1

所有產品數據爲後期元。但我同意馬修的意見,不清楚你在問什麼。 – helgatheviking

回答

1

當你建設了查詢設置的崗位類型爲產品

$query->set('post_type', 'product'); 

而這隻能通過woocommerce產品搜索。

另外,如果您要創建主題,則可以從/wp-content/plugins/woocommerce/templates/目錄中取出任何文件,並將其放入/wp-content/themes/<yourTheme>/woocommerce以覆蓋任何woocommerce頁面。

3

這樣,您就可以通過wp_query得到所有產品:

global $wpdb; 

$all_product_data = $wpdb->get_results("SELECT ID,post_title,post_content,post_author,post_date_gmt FROM `" . $wpdb->prefix . "posts` where post_type='product' and post_status = 'publish'"); 

如果你想了解更多的產品數據,那麼這將是這樣的:

$product = wc_get_product($product_id); 
$product->product_type; 
get_post_meta($prodyct_id, '_regular_price'); 
$product->get_available_variations(); 
1

感謝所有reply.I有決心就像波濤洶涌

$full_product_list = array(); 
$loop = new WP_Query(array('post_type' => array('product', 'product_variation'), 'posts_per_page' => -1)); 

    while ($loop->have_posts()) : $loop->the_post(); 
     $theid = get_the_ID(); 
     $product = new WC_Product($theid); 
     if (get_post_type() == 'product_variation') { 

      // ****************** end error checking ***************** 
     } else { 
      $sku = get_post_meta($theid, '_sku', true); 
      $selling_price = get_post_meta($theid, '_sale_price', true); 
      $regular_price = get_post_meta($theid, '_regular_price', true); 
      $description=get_the_content(); 
      $thetitle = get_the_title(); 

     } 
     // add product to array but don't add the parent of product variations 
     if (!empty($sku)) 
      $full_product_list[] = array("PartyID" => (int) $party_id,"Description"=> $description, 
       "ExternalNumber" => $theid, "ProductName" => $thetitle, "sku" => $sku, 
       "RegularPrice" => $regular_price, "SellingPrice" => $selling_price, 
       "ExternalProductCategoryId" => $cat_id, "ExternalProductCategoryName" => $cat_name); 
    endwhile; 
相關問題