prosource

워드프레스 데이터베이스에서 제품 속성을 가져오는 방법

probook 2023. 3. 4. 14:59
반응형

워드프레스 데이터베이스에서 제품 속성을 가져오는 방법

워드프레스 데이터베이스로 제품 세부 정보 페이지를 만들기 위해 사용자 지정 코드를 작성하는 중입니다.

상품 제목, 설명, 가격, 재고 등을 표시해 두고 상품 속성에 집착하고 있습니다.데이터베이스에서는 _product_attribute가 데이터베이스의 wp_postmeta 테이블에 시리얼화된 방식으로 저장됩니다.그리고 그것으로부터 속성을 지울 수 없었어요.그러나 저는 각각의 속성값과 자체 가격이 다른 post_id의 wp_postmeta에 저장되어 있음을 발견했습니다.

예를 들어, post_id=55인 제품은 값이 14와 18이고 가격이 300과 350인 속성 이름 'Size value'를 가지며, post_id=110,126에 속성 값 및 가격으로 표시됩니다.

wp_postmeta wp-postmeta-meta-interminal의

뒤에 공식이 있나요?이 제품의 속성값과 그에 상응하는 가격값을 찾을 수 있을까요?

조금 다른 접근방식을 취해서 데이터베이스에 저장 프로시저를 만들었습니다.이 프로시저는 woocommerce 제품과 관련된 모든 용어를 반환합니다.두 가지 언어로 기능을 쓸 필요 없이 워드프레스 사이트와 만들고 있는 데스크톱 앱에서 절차를 호출할 수 있기 때문에 이 경로를 선택하게 되었습니다.

다른 사람들이 사용할 수 있도록 여기에 올리긴 했지만요.

CREATE DEFINER=`database_name_here`@`%` PROCEDURE `get_product_attributes`(IN ProductName TEXT)
BEGIN
SELECT DISTINCT
     p.post_title AS 'Product Name',
     t.name AS 'Term Name',
     tt.taxonomy AS 'Term Type',
     tt.description AS 'Term Description'
FROM
     wp_posts AS p
INNER JOIN
     wp_term_relationships AS tr ON p.id = tr.object_id
INNER JOIN
     wp_term_taxonomy AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
INNER JOIN
     wp_terms AS t ON t.term_id = tt.term_id
WHERE
     p.post_title= ProductName
AND
     p.post_type = 'product';
END

제품 속성은 wp_terms, wp_term_taxonomy 및 wp_term_relations의 두 위치에 저장되며(각 속성은 분류명의 pa_ 앞에 있음, 예를 들어 색상 속성이 있는 경우 pa_color 아래에 있음), wp_postmeta의 '_product_attributes' 아래에 PHP 시리얼화된 배열로도 저장됩니다.

seriliazed Atribute 배열을 구성하는 방법은 다음과 같습니다.

https://github.com/woothemes/woocommerce/blob/master/includes/class-wc-ajax.php

save_attributes() 함수와 add_attribute를 검색하여 시리얼화된 배열의 구조를 확인합니다.

업데이트: 이후 버전의 wooCommerce도 _transient_wc_attribute_taxonomies 키 아래의 wp_options에 시리얼화된 배열과 wp_woocommerce_attribute_taxonomies라는 새로운 테이블이 있습니다.

Fütemire의 훌륭한 답변을 바탕으로 포스트 ID를 통해 속성을 얻는 방법

SELECT DISTINCT
    p.ID,
    t.name AS 'Term Name',
    tt.taxonomy AS 'Term Type',
    tt.description AS 'Term Description',
    (
        SELECT
            wat.attribute_label
        FROM
            wp_woocommerce_attribute_taxonomies wat
        WHERE
            wat.attribute_name LIKE REPLACE(tt.taxonomy, 'pa_', '')
    ) AS 'Attribute Name'
FROM
    wp_posts AS p
INNER JOIN
    wp_term_relationships AS tr
    ON p.id = tr.object_id
INNER JOIN
    wp_term_taxonomy AS tt
    ON tt.term_taxonomy_id = tr.term_taxonomy_id
INNER JOIN
    wp_terms AS t
    ON t.term_id = tt.term_id
WHERE
    p.ID = 15870
    AND
    p.post_type = 'product'
    AND
    tt.taxonomy LIKE 'pa_%'

모든 속성을 새 열로 가져오려면 다음과 같은 필터를 사용하여 여러 조인을 추가할 수 있습니다.

select 
      inv_sku.meta_value as sku, 
      wp.post_title as title, 
      inv_category.name as category,
      inv_subcategory.name as subcategory,
      inv_brand.name as brand,
      inv_price.meta_value as sale_price 
from wp_posts as wp 
inner join wp_postmeta as inv_sku on inv_sku.post_id =wp.ID and inv_sku.meta_key ='_sku'
inner join wp_postmeta as inv_price on inv_price.post_id =wp.ID and inv_price.meta_key ='_sale_price'
left join (
select tr.object_id, t.name  from wp_term_relationships AS tr 
left join wp_term_taxonomy AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
left join  wp_terms AS t ON t.term_id = tt.term_id
where tt.taxonomy ='pa_brand'
) as inv_brand on wp.ID =inv_brand.object_id 
left join (
select tr.object_id, t.name  from wp_term_relationships AS tr 
left join wp_term_taxonomy AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
left join  wp_terms AS t ON t.term_id = tt.parent 
where tt.taxonomy ='product_cat'
) as inv_category on wp.ID =inv_category.object_id
left join (
select tr.object_id, t.name  from wp_term_relationships AS tr 
left join wp_term_taxonomy AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
left join  wp_terms AS t ON t.term_id = tt.term_id
where tt.taxonomy ='product_cat'
) as inv_subcategory on wp.ID =inv_subcategory.object_id
where wp.post_type ='product'
$args = array( 'post_type' => 'product','' );
$products = get_posts( $args );

foreach ($products as $product) {
    $data = get_post_meta($product->ID);            
    $pr['regular_price'] = $data['_regular_price']['0'];
    $pr['sale_price'] = $data['_sale_price']['0'];
}

언급URL : https://stackoverflow.com/questions/22034862/how-to-get-product-attributes-from-wordpress-database

반응형