首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在WooCommerce中程序化地获取所有产品?

如何在WooCommerce中程序化地获取所有产品?
EN

Stack Overflow用户
提问于 2016-04-03 09:46:01
回答 5查看 14.9K关注 0票数 3

我想获取WooCommerce中的所有产品数据(产品sku、名称、价格、库存数量、供货情况等)我可以使用wp-query来做这件事吗?

EN

回答 5

Stack Overflow用户

发布于 2016-04-04 12:24:27

这样你就可以通过wp_query获取所有产品:

代码语言:javascript
复制
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'");

如果您想要更多的产品数据,那么它将如下所示:

代码语言:javascript
复制
$product = wc_get_product($product_id);
$product->product_type;
get_post_meta($prodyct_id, '_regular_price');
$product->get_available_variations();
票数 4
EN

Stack Overflow用户

发布于 2016-04-04 23:30:53

谢谢你所有的回复。我已经像巨浪一样解决了

代码语言:javascript
复制
$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; 
票数 2
EN

Stack Overflow用户

发布于 2016-04-03 10:51:39

在构建查询时,将帖子类型设置为product

代码语言:javascript
复制
$query->set('post_type', 'product');

而这只能搜索woocommerce的产品。

此外,如果您正在创建一个主题,您可以从/wp-content/plugins/woocommerce/templates/目录中获取任何文件,并将其放入/wp-content/themes/<yourTheme>/woocommerce中,以覆盖任何woocommerce页面。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36380749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档