등급 (백엔드 또는 관리자 패널에서 등급 지정을위한 분류/속성을 추가해야 함) "등급"을 추가 했으므로 slug
이 pa_rating
이 될 것입니다.
속성을 추가 한 후에 용어/값을 추가해야하는 경우 5.0, 4.5를 추가하면 슬러그가 각각 5-0
및 4-5
이됩니다.
이제 활성 어린이 테마 (또는 테마)의 function.php
파일에 아래 기능을 복사하여 복사하십시오. 또는 모든 플러그인 PHP 파일에서. 사이트를 한 번만 실행 한 후 아래 코드을 삭제하십시오.
add_action('wp', 'wh_addRatingtoProduct');
function wh_addRatingtoProduct() {
$args = ['post_type' => 'post', 'posts_per_page' => -1];
$loop = new WP_Query($args);
if (!empty($loop->posts)):
$attribute_name = 'pa_rating'; //slug of the attribute(taxonomy) with prefix 'pa_'
foreach ($loop->posts as $post) :
$content = $post->post_content; //getting product content
$product_id = $post->ID;
//for 5 star
if (strpos($content, "5.0.png") !== false) {
$attribute_value = '5'; //slug of the attribute value (term)
}
//for 4.7 star
elseif (strpos($content, "4.7.png") !== false) {
$attribute_value = '4-7'; //slug of the attribute value (term)
}
//for 4.8 star
elseif (strpos($content, "4.8.png") !== false) {
$attribute_value = '4-8'; //slug of the attribute value (term)
}
//...
//...
//Appending term to the object/product.
$term_taxonomy_ids = wp_set_object_terms($product_id, $attribute_value, $attribute_name, true);
$data = [
$attribute_name => [
'name' => $attribute_name,
'value' => '',
'is_visible' => '1',
'is_variation' => '0',
'is_taxonomy' => '1'
]
];
//getting the Post Meta
$_product_attributes = get_post_meta($product_id, '_product_attributes', TRUE);
//Updating the Post Meta
update_post_meta($product_id, '_product_attributes', array_merge($_product_attributes, $data));
endforeach;
endif;
}
는 속성 값의 원하는 슬러그와 모든 $attribute_value
바꾸기. 이렇게하기 전에 데이터베이스 백업을 수행하는 것이 좋습니다.
코드가 테스트되었으며 작동합니다.
참고 :이 도움이 Add Attribute to WooCommerce Product Programmatically
희망! 여기
가능한 방법 : 제품 중 하나에 등급을 적용하고 데이터베이스에 제품 등급이 저장되어 있는지 확인하십시오. 그런 다음 SQL 쿼리를 작성하여이 학습을 기반으로 모든 제품을 업데이트하십시오. 테스트를 위해 실제 데이터베이스의 덤프를 사용하십시오. –
시도해보기 : https://wordpress.org/plugins/product-import-export-for-woo –