如何以编程方式获取WooCommerce中的所有产品?
问题描述:
我想要获得WooCommerce中的所有产品数据(产品sku,名称,价格,库存量,可用性等)我可以使用wp-query来完成这项工作吗?如何以编程方式获取WooCommerce中的所有产品?
答
当你建设了查询设置的岗位类型为产品
$query->set('post_type', 'product');
而这只能通过woocommerce产品搜索。
另外,如果您要创建主题,则可以从/wp-content/plugins/woocommerce/templates/
目录中取出任何文件,并将其放入/wp-content/themes/<yourTheme>/woocommerce
以覆盖任何woocommerce页面。
答
这样,您就可以通过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();
答
感谢所有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;
在woocommerce插件的所有模板已经存在找到它并将代码复制到您的自定义模板e ..他们是用meta添加的自定义帖子类型u也可以使用自定义帖子类型查询并获得帖子meta方法 –
只是一个友好的提示,您可能想要阅读本页:[如何提问](https: //*.com/help/how-to-ask),因此您始终可以确定您的问题很容易回答并尽可能清晰。一定要包括你为解决你遇到的问题所做的任何努力,以及当你尝试修复这些问题时发生了什么。另外不要忘记你的代码和任何错误信息! –
所有产品数据为后期元。但我同意马修的意见,不清楚你在问什么。 – helgatheviking