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.
我正在为woocommerce创建一个自定义主题,我需要能够创建一个迷你产品展示。我在找到woocommerce api的文档时遇到了问题。我有一个逗号分隔的产品ID列表,我需要迭代并按顺序显示每个产品ID的自定义迷你产品显示。
$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;
}
Any help would be appreciated.
任何帮助,将不胜感激。
Thank you,
谢谢,
Tim
蒂姆
4 个解决方案
#1
46
Another easy way is to use the WC_Product_Factory class and then call function get_product(ID)
另一个简单的方法是使用WC_Product_Factory类,然后调用函数get_product(ID)
http://docs.woothemes.com/wc-apidocs/source-class-WC_Product_Factory.html#16-63
http://docs.woothemes.com/wc-apidocs/source-class-WC_Product_Factory.html#16-63
sample:
样品:
// 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
}
You can then use all the function calls as listed in their api: http://docs.woothemes.com/wc-apidocs/class-WC_Product.html
然后,您可以使用其api中列出的所有函数调用:http://docs.woothemes.com/wc-apidocs/class-WC_Product.html
#2
38
Use this method:
使用此方法:
$_product = wc_get_product( id );
Official API-docs: wc_get_product
官方API-docs:wc_get_product
#3
3
Alright, I deserve to be throttled. definitely an RTM but not for WooCommerce, for Wordpress. Solution found due to a JOLT cola (all hail JOLT cola).
好吧,我应该受到扼杀。绝对是RTM但不适用于WooCommerce,适用于Wordpress。由JOLT可乐(所有冰雹JOLT可乐)发现的解决方案。
TASK: Field named 'related_product_ids' added to a custom post type. So when that post is displayed mini product displays can be displayed with it.
任务:名为“related_product_ids”的字段已添加到自定义帖子类型中。因此,当显示该帖子时,可以显示迷你产品显示。
PROBLEM: Was having a problem getting the multiple ids returned via WP_Query.
问题:通过WP_Query返回多个ID时遇到问题。
SOLUTION:
解:
$related_id_list = get_post_custom_values('related_product_ids');
// Get comma delimited list from current post
$related_product_ids = explode(",", trim($related_id_list[0],','));
// Return an array of the IDs ensure no empty array elements from extra commas
$related_product_post_ids = array( 'post_type' => 'product',
'post__in' => $related_product_ids,
'meta_query'=> array(
array( 'key' => '_visibility',
'value' => array('catalog', 'visible'),'compare' => 'IN'
)
)
);
// Query to get all product posts matching given IDs provided it is a published post
$loop = new WP_Query( $related_posts );
// Execute query
while ( $loop->have_posts() ) : $loop->the_post(); $_product = get_product( $loop->post->ID );
// Do stuff here to display your products
endwhile;
Thank you for anyone who may have spent some time on this.
感谢任何可能花费一些时间的人。
Tim
蒂姆
#4
1
global $woocommerce;
var_dump($woocommerce->customer->get_country());
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = new WC_product($cart_item['product_id']);
var_dump($product);
}
#1
46
Another easy way is to use the WC_Product_Factory class and then call function get_product(ID)
另一个简单的方法是使用WC_Product_Factory类,然后调用函数get_product(ID)
http://docs.woothemes.com/wc-apidocs/source-class-WC_Product_Factory.html#16-63
http://docs.woothemes.com/wc-apidocs/source-class-WC_Product_Factory.html#16-63
sample:
样品:
// 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
}
You can then use all the function calls as listed in their api: http://docs.woothemes.com/wc-apidocs/class-WC_Product.html
然后,您可以使用其api中列出的所有函数调用:http://docs.woothemes.com/wc-apidocs/class-WC_Product.html
#2
38
Use this method:
使用此方法:
$_product = wc_get_product( id );
Official API-docs: wc_get_product
官方API-docs:wc_get_product
#3
3
Alright, I deserve to be throttled. definitely an RTM but not for WooCommerce, for Wordpress. Solution found due to a JOLT cola (all hail JOLT cola).
好吧,我应该受到扼杀。绝对是RTM但不适用于WooCommerce,适用于Wordpress。由JOLT可乐(所有冰雹JOLT可乐)发现的解决方案。
TASK: Field named 'related_product_ids' added to a custom post type. So when that post is displayed mini product displays can be displayed with it.
任务:名为“related_product_ids”的字段已添加到自定义帖子类型中。因此,当显示该帖子时,可以显示迷你产品显示。
PROBLEM: Was having a problem getting the multiple ids returned via WP_Query.
问题:通过WP_Query返回多个ID时遇到问题。
SOLUTION:
解:
$related_id_list = get_post_custom_values('related_product_ids');
// Get comma delimited list from current post
$related_product_ids = explode(",", trim($related_id_list[0],','));
// Return an array of the IDs ensure no empty array elements from extra commas
$related_product_post_ids = array( 'post_type' => 'product',
'post__in' => $related_product_ids,
'meta_query'=> array(
array( 'key' => '_visibility',
'value' => array('catalog', 'visible'),'compare' => 'IN'
)
)
);
// Query to get all product posts matching given IDs provided it is a published post
$loop = new WP_Query( $related_posts );
// Execute query
while ( $loop->have_posts() ) : $loop->the_post(); $_product = get_product( $loop->post->ID );
// Do stuff here to display your products
endwhile;
Thank you for anyone who may have spent some time on this.
感谢任何可能花费一些时间的人。
Tim
蒂姆
#4
1
global $woocommerce;
var_dump($woocommerce->customer->get_country());
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = new WC_product($cart_item['product_id']);
var_dump($product);
}