我想获取WooCommerce中的所有产品数据(产品sku、名称、价格、库存数量、供货情况等)我可以使用wp-query来做这件事吗?
发布于 2016-04-04 12:24:27
这样你就可以通过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();发布于 2016-04-04 23:30:53
谢谢你所有的回复。我已经像巨浪一样解决了
$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; 发布于 2016-04-03 10:51:39
在构建查询时,将帖子类型设置为product
$query->set('post_type', 'product');而这只能搜索woocommerce的产品。
此外,如果您正在创建一个主题,您可以从/wp-content/plugins/woocommerce/templates/目录中获取任何文件,并将其放入/wp-content/themes/<yourTheme>/woocommerce中,以覆盖任何woocommerce页面。
https://stackoverflow.com/questions/36380749
复制相似问题