首页 > 解决方案 > 如何使用 REST API 检索 woocommerce 产品数据?

问题描述

我正在使用 api php 获取 woocommerce 产品数据,但它无法正常工作。
请在下面找到代码。
任何帮助,将不胜感激。

$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

//Checking if any error occured while connecting
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    die();
}

//creating a query
$stmt = $conn->prepare("SELECT p.ID,p.post_title,p.post_status,p.post_name,pp.min_price,pp.max_price,
pi.meta_value FROM `wp_posts` p INNER JOIN `wp_wc_product_meta_lookup` pp ON pp.product_id = p.ID  
INNER JOIN  `wp_postmeta` pi ON pi.post_id=p.ID where p.post_status='publish' and pi.meta_key ='_wp_attached_file'
 and `p.post_type`='product'GROUP BY p.ID;");

//executing the query 
$stmt->execute();

//binding results to the query 
$stmt->bind_result($post_id,$post_title, $post_status, $post_name,$min_price, $max_price , $meta_value);

$courses = array(); 

//traversing through all the result 
while($stmt->fetch()){
    $temp = array();
    $temp['post_id'] = $post_id; 
    $temp['post_title'] = $post_title; 
    $temp['post_status'] = $post_status; 
    $temp['post_name'] = $post_name; 
    $temp['min_price'] = $min_price; 
    $temp['max_price'] = $max_price; 
    $temp['meta_value'] = $meta_value; 
    array_push($courses, $temp);
}
//echo count($courses);
//displaying the result in json format 
echo json_encode($courses);

标签: androidapiwoocommerce

解决方案


推荐阅读