Php 如何以程序化的方式获得商业中的所有产品?
我想获取WooCommerce中的所有产品数据(产品sku、名称、价格、库存数量、可用性等),我可以使用wp查询吗?在构建查询时,将post类型设置为产品Php 如何以程序化的方式获得商业中的所有产品?,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我想获取WooCommerce中的所有产品数据(产品sku、名称、价格、库存数量、可用性等),我可以使用wp查询吗?在构建查询时,将post类型设置为产品 $query->set('post_type', 'product'); 这只会通过woocommerce产品进行搜索 此外,如果您正在创建主题,您可以从/wp content/plugins/woocommerce/templates/目录中获取任何文件,并将其放入/wp content/themes//woocommerce以覆盖
$query->set('post_type', 'product');
这只会通过woocommerce产品进行搜索
此外,如果您正在创建主题,您可以从
/wp content/plugins/woocommerce/templates/
目录中获取任何文件,并将其放入/wp content/themes//woocommerce
以覆盖任何woocommerce页面。这样,您可以通过wp\u查询获得所有产品:
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();
谢谢你的回复,我已经下定决心了
$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;
您可以在要显示所有产品信息的页面中编写此代码
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 10
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<br /><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().' '.get_the_title().'</a>';
global $product;
//echo $product;
echo $product->get_id();
echo $product->get_name();
echo $product->get_sale_price();
echo $product->get_regular_price();
echo $product->get_sku();
echo $product->get_low_stock_amount();
echo $product->get_review_count();
echo $product->get_short_description();
$ratting = $product->get_rating_counts();
for($i = 0 ; $i < count($ratting); $i++) {
echo $ratting[i];
}
endwhile;
wp_reset_query();
?>
这是一个老问题;自2018年以来,使用
WP\u Query()
的答案已经过时
查看我的答案。在woocommerce插件中,所有模板都已经存在。找到它并将代码复制到自定义模板中。它们是添加了元的自定义帖子类型。您还可以使用自定义帖子类型查询和获取帖子元方法只是一个友好的提示,你可能想仔细阅读这一页:这样你就可以确保你的问题很容易回答并且尽可能清楚。请确保包括您为解决问题所做的任何努力,以及在尝试这些修复时发生的情况。另外,不要忘记查看您的代码和任何错误消息!所有产品数据都是后元数据。但我同意Matthew的观点,你的问题还不清楚。欢迎来到StackOverflow。在回答问题时,还请提供一些你所做的事情的背景和解释,以便阅读你答案的人能够理解你回答中的内容。