Woocommerce get_products 返回空对象 [英] Woocommerce get_products return empty objects
本文介绍了Woocommerce get_products 返回空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Woocommerce 产品构建自定义端点,查询返回 6 个对象,但现在不为空,这是为什么?我在下面的代码中缺少什么?
I'm building custom endpoint using Woocommerce Products and the query return 6 objects but empty don't now why is that? what I'm missing on my code below?
代码
add_action('rest_api_init', function () {
register_rest_route( 'hash', 'related-products',array(
'methods' => 'GET',
'callback' => 'hash_realated_products'
));
});
function hash_realated_products() {
// Get 10 most recent product IDs in date descending order.
$query = new WC_Product_Query( array(
'limit' => 6,
'status' => 'publish',
'orderby' => 'rand',
'tax_query' => array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => '257,352' 'operator' => 'IN'
)
) );
$products = $query->get_products();
return $products;
}
推荐答案
问题是我没有从 products 数组中调用数据!
The problem was I didn't call for the data from the products array!
所以最终的代码将是:
$products_query = $query->get_products();
$products = array();
foreach ( $products_query as $product ) {
$products[] = $product->get_data();
}
return $products;
这篇关于Woocommerce get_products 返回空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文