HEX
Server: Apache/2.4.59 (Debian)
System: Linux keymana 4.19.0-21-cloud-amd64 #1 SMP Debian 4.19.249-2 (2022-06-30) x86_64
User: lijunjie (1003)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/dk/wp-content/themes/food/api/product/get_product_by_search.php
<?php
/**
 * 通过cate获取指定数量产品
 */
$tabs = new stdClass();

$args = array(
    'taxonomy' => 'product_cat',
    'hide_empty' => 0
);
$tabs->productCat = array();
$productTabs = get_terms('product_cat', $args);

//print_r($productTabs);
if (isset($_REQUEST['web_type'])) {
//    echo 'isset($_REQUEST[web_type])';

    $web_type = $_REQUEST['web_type'];
    if ('MedicalMediaChannel' == $web_type) {

        $term_taxonomy_id = 39;
        foreach ($productTabs as $key => $value) {

            if ($value->name == 'Medical Media Channel') {
//                array_push($tabs->productCat, $value->name);
                $term_taxonomy_id = $value->term_taxonomy_id;
            }

            if ($value->parent == $term_taxonomy_id) {
                array_push($tabs->productCat, $value->name);
            }
        }
    }

} else {
    foreach ($productTabs as $key => $value) {
        if ($value->name != 'Uncategorized') {
            array_push($tabs->productCat, $value->name);
        }
    }

//    Array
//    (
//        [0] => Medical Media Channel
//    [1] => 健康產品
//    [2] => 影視專區
//    [3] => 積分兌換商品
//    [4] => 醫藥保健
//    [5] => 醫藥保健
//    [6] => 零食專區
//    [7] => 電子產品
//    [8] => 電子產品專區
//    [9] => 首頁專區
//)
}
//print_r($tabs->productCat);
//
//die();

$limit = empty($_POST['limit']) ? 6 : intval($_POST['limit']);
$page_num = empty($_POST['page_num']) ? 1 : intval($_POST['page_num']);
$price_pre = empty($_POST['price_pre']) ? 0 : intval($_POST['price_pre']);
$price_next = empty($_POST['price_next']) ? 0 : intval($_POST['price_next']);
$order_name = empty($_POST['order_name']) ? '日期' : $_POST['order_name'];
$filter_cate = !is_array($_POST['filter_cate']) ? [] : $_POST['filter_cate'];
$search_text = empty($_POST['search_text']) ? "" : $_POST['search_text'];

$args = array('category' => $filter_cate, 'limit' => $limit, 'page' => $page_num);

if ($price_pre > 0) {
    $args['min_price'] = "$price_pre";
}

if ($price_next > 0) {
    $args['max_price'] = "$price_next";
}

switch ($order_name) {
    case '日期':
        $args['orderby'] = 'date';
        $args['order'] = 'DESC';
        break;
    case '價格升序':
        $args['price_order'] = 'ASC';
        break;
    case '價格降序':
        $args['price_order'] = 'DESC';
        break;
    default:
        $args['orderby'] = 'rand';
        break;
}
if (!empty($search_text)) {
    $args['s'] = $search_text;
}
$result = [];

$products = wc_get_products($args);

foreach ($products as $product) {
    $data = new stdClass();
    $imageId = $product->get_image_id();
    // var_dump($imageId);
    $image_arr = wp_get_attachment_image_src($imageId, "full");
    // var_dump($image_arr);

    if (!empty($image_arr) && is_array($image_arr)) {
        $image = $image_arr[0];
    } else {
        $image = "";
    }

    $data->id = $product->get_id();
    $data->img_url = $image;
    $data->product_name = $product->get_name();
    $arr = $product->get_category_ids();
    if (count($arr) >= 2) {
        $catelist = [];
        foreach ($arr as $a) {
            $std = new stdClass();
            $std = $wpdb->get_var($wpdb->prepare("select name from `wp_terms` where term_id=%d", $a));
            array_push($catelist, $std);
        }
        $data->cate = $catelist;
        $data->cate_single = $catelist[0];
    } else {
        $data->cate_single = $wpdb->get_var($wpdb->prepare("select name from `wp_terms` where term_id=%d", $product->get_category_ids()));
        $data->cate = [$data->cate_single];
    }
    $data->product_price = $product->get_price();
    $data->sale_price = $product->get_sale_price();
    $data->regular_price = empty($product->get_regular_price()) ? '0' : $product->get_regular_price();
    $data->description = $product->get_description();
    array_push($result, $data);
}

$res = $result;