카테고리 ID별로 제품 가져오기
저는 우커머스용 가격표 플러그인을 작성하고 있습니다.사용자는 wocommerce 제품 카테고리의 ID와 함께 숏코드를 삽입하고 페이지를 업데이트한 후 제품 이름과 가격의 목록이 있는 표를 볼 수 있습니다.
어떻게 하면 그들의 카테고리의 ID로 상품 목록을 얻을 수 있을까요?!
$pid 아래의 코드는 사용자가 숏코드로 입력하는 것이고, 'object_id'는 wp_ posts 테이블에 있는 모든 제품의 ID입니다.
<?php
$products = $wpdb->get_results("SELECT object_id FROM {$wpdb->term_relationships}
WHERE term_taxonomy_id = " . $pid);
if(!empty($products))
{
foreach($products as $product)
{
//the code
}
}
?>
미리 감사드립니다.
$args = array(
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => '[ category id here ]', // When you have more term_id's seperate them by komma.
'operator' => 'IN'
)
)
);
$the_query = wp_query($args);
테스트되지 않았지만 작동해야 함
WP_Query() 또는 get_posts()를 사용해서는 안 됩니다.
wc_get_products 및 WC_Product_쿼리는 사용하기에 안전하고 향후 WooCommerce 버전의 데이터베이스 변경으로 인해 손상되지 않는 제품을 검색하는 표준 방법을 제공합니다.사용자 정의 WP_Query 또는 데이터베이스 쿼리를 구축하면 성능 향상을 위해 사용자 정의 테이블로 데이터가 이동함에 따라 향후 버전의 WoCommerce에서 코드가 손상될 가능성이 있습니다.이 방법은 플러그인 및 테마 개발자가 여러 제품을 검색할 수 있는 최상의 방법입니다.wc_get_products 및 WC_Product_쿼리는 WordPress get_posts 및 WP_Query와 유사합니다.마찬가지로 검색 기준을 정의하는 일련의 인수를 전달합니다.
제 해결책은 이렇습니다.
$product_term_ids = array(16,10,4,7);
$product_term_args = array(
'taxonomy' => 'product_cat',
'include' => $product_term_ids,
'orderby' => 'include'
);
$product_terms = get_terms($product_term_args);
$product_term_slugs = [];
foreach ($product_terms as $product_term) {
$product_term_slugs[] = $product_term->slug;
}
$product_args = array(
'post_status' => 'publish',
'limit' => -1,
'category' => $product_term_slugs,
//more options according to wc_get_products() docs
);
$products = wc_get_products($product_args);
foreach ($products as $product) {
echo $product->get_title();
}
참고: 범주 인수에는 ID가 아닌 슬러그 배열이 필요합니다.
(Wordpress 5.9.3 & WooCommerce 6.4.1과 함께 테스트 및 작동)
get_posts 워드 프레스 기능을 사용하여
카테고리별 모든 WooCommerce 제품 세부 정보 가져오기
$all_products = get_posts( array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'your_product_category', /*category name*/
'operator' => 'IN',
)
),
));
echo var_dump($all_products);
카테고리별 모든 WooCommerce 제품 ID 가져오기
$all_ids = get_posts( array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'your_product_category', /*category name*/
'operator' => 'IN',
)
),
));
foreach ( $all_ids as $id ) {
echo $id;
}
테스트를 했고 잘 작동합니다.
위의 (받아들인) 답변은 저에게 효과가 없었습니다 (예전에 있었던 것보다 더 많은 결과) (버전 4.9.8).
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => [$request['id']],
'operator' => 'IN'
)
)
위의 내용이 통합니다.저는 워드프레스 사람은 아니지만 버전이 아닐까 싶습니다.
$posts = get_posts(
array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'books-and-handbooks',
'operator' => 'IN',
),
),
)
);
$arr = array_slice( $all_ids, 0, 50 );
언급URL : https://stackoverflow.com/questions/19818406/get-products-by-category-id
'programing' 카테고리의 다른 글
| 데이터 프레임을 분할하는 방법? (0) | 2023.09.19 |
|---|---|
| Python 3의 문에 SQL 삽입을 위한 구문 형식 지정 (0) | 2023.09.19 |
| 로컬 파일을 덮어쓰지 않고 원격에서 파일을 꺼내려면 어떻게 해야 합니까? (0) | 2023.09.19 |
| 일반 파일에 이폴링 (0) | 2023.09.19 |
| MySQL이 SET 절에서 SELECT를 사용하여 UPDATE 쿼리에 인덱스 사용을 거부함 (0) | 2023.09.19 |