WooCommerce按ID返回产品对象 [英] WooCommerce return product object by id
问题描述
我正在为woocommerce创建一个自定义主题,我需要能够创建一个迷你产品展示.我在woocommerce api上找到文档时遇到问题.我有一个用逗号分隔的产品ID列表,我需要对其进行迭代,并依次显示每个产品的自定义迷你产品显示.
I am creating a custom theme for woocommerce and I need to be able to create a mini product display. I am having problems finding documentation on the woocommerce api. I have a comma delimited list of product IDs that I need to iterate through and display a custom mini product display for each in sequence.
$key_values = get_post_custom_values('rel_products_ids');
//get comma delimited list from product
$rel_product_ids = explode(",", trim($key_values, ","));
// create array of just the product ids
foreach ( $rel_product_ids as $pid ) {
//sequentially get each id and do something with it
$loop = new WP_Query( array( 'post__in' => $pid ) );
// also tried ...
//$loop = new WP_Query( array( 'ID' => $pid ) );
while ( $loop->have_posts() ) : $loop->the_post(); $_product = &new WC_Product( $loop->post->ID );
//do stuff here I have stripped the html in favor of getting to the meat of the issue
woocommerce_show_product_sale_flash( $post, $_product );
if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_single');
get_permalink( $loop->post->ID );
the_title();
$_product->get_price_html();
endwhile;
}
任何帮助将不胜感激.
谢谢
蒂姆
推荐答案
另一种简便方法是使用WC_Product_Factory类,然后调用函数get_product(ID)
Another easy way is to use the WC_Product_Factory class and then call function get_product(ID)
http://docs.woothemes.com/wc-apidocs/source -class-WC_Product_Factory.html#16-63
样本:
// assuming the list of product IDs is are stored in an array called IDs;
$_pf = new WC_Product_Factory();
foreach ($IDs as $id) {
$_product = $_pf->get_product($id);
// from here $_product will be a fully functional WC Product object,
// you can use all functions as listed in their api
}
然后,您可以使用其api中列出的所有函数调用: http://docs.woothemes.com/wc-apidocs/class-WC_Product.html
You can then use all the function calls as listed in their api: http://docs.woothemes.com/wc-apidocs/class-WC_Product.html
这篇关于WooCommerce按ID返回产品对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!