admin管理员组文章数量:1130349
I am using the following code to get the best selling products and send the result in JSON. But I am unable to encode the result of wc_get_products().
$best_selling_args = array(
'meta_key' => 'total_sales',
'order' => 'DESC',
'orderby' => 'meta_value_num'
);
$products_posts = wc_get_products( $best_selling_args );
// var_dump( $products_posts );
echo wp_json_encode( $products_posts );
I am using the following code to get the best selling products and send the result in JSON. But I am unable to encode the result of wc_get_products().
$best_selling_args = array(
'meta_key' => 'total_sales',
'order' => 'DESC',
'orderby' => 'meta_value_num'
);
$products_posts = wc_get_products( $best_selling_args );
// var_dump( $products_posts );
echo wp_json_encode( $products_posts );
Share
Improve this question
asked Nov 27, 2018 at 9:34
Sagar Bahadur TamangSagar Bahadur Tamang
1331 silver badge10 bronze badges
1 Answer
Reset to default -1Try:
$products_posts_arr = (array) $products_posts;
echo json_encode( $products_posts_arr );
OR
function object_to_array($data) {
if (is_array($data) || is_object($data)) {
$result = array();
foreach ($data as $key => $value) {
$result[$key] = object_to_array($value);
}
return $result;
}
return $data;
}
$products_posts_arr = object_to_array( $products_posts );
echo json_encode( $products_posts_arr );
Encode array not object, wc_get_products will return object.
I am using the following code to get the best selling products and send the result in JSON. But I am unable to encode the result of wc_get_products().
$best_selling_args = array(
'meta_key' => 'total_sales',
'order' => 'DESC',
'orderby' => 'meta_value_num'
);
$products_posts = wc_get_products( $best_selling_args );
// var_dump( $products_posts );
echo wp_json_encode( $products_posts );
I am using the following code to get the best selling products and send the result in JSON. But I am unable to encode the result of wc_get_products().
$best_selling_args = array(
'meta_key' => 'total_sales',
'order' => 'DESC',
'orderby' => 'meta_value_num'
);
$products_posts = wc_get_products( $best_selling_args );
// var_dump( $products_posts );
echo wp_json_encode( $products_posts );
Share
Improve this question
asked Nov 27, 2018 at 9:34
Sagar Bahadur TamangSagar Bahadur Tamang
1331 silver badge10 bronze badges
1 Answer
Reset to default -1Try:
$products_posts_arr = (array) $products_posts;
echo json_encode( $products_posts_arr );
OR
function object_to_array($data) {
if (is_array($data) || is_object($data)) {
$result = array();
foreach ($data as $key => $value) {
$result[$key] = object_to_array($value);
}
return $result;
}
return $data;
}
$products_posts_arr = object_to_array( $products_posts );
echo json_encode( $products_posts_arr );
Encode array not object, wc_get_products will return object.
本文标签: woocommerce offtopicUnable to enocde the result of wcgetproducts
版权声明:本文标题:woocommerce offtopic - Unable to enocde the result of wc_get_products 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749149030a2323493.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论