gpt4 book ai didi

php - WooCommerce 按 id 返回产品对象

转载 作者:IT王子 更新时间:2023-10-29 01:02:58 24 4
gpt4 key购买 nike

我正在为 woocommerce 创建自定义主题,我需要能够创建迷你产品展示。我在查找有关 woocommerce api 的文档时遇到问题。我有一个以逗号分隔的产品 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;
}

任何帮助将不胜感激。

谢谢,

提姆

最佳答案

使用这个方法:

$_product = wc_get_product( $id );

官方 API 文档:wc_get_product

关于php - WooCommerce 按 id 返回产品对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12561611/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com