0
有860多種不同變體的產品。任務是允許用戶批量編輯「用於變體」字段的某些屬性。例如,我們的尺寸,性別仍然需要用於變體,但我們也有材質,僅用於過濾器,因此刪除此屬性不是解決方案。「用於變體」以編程方式切換
有860多種不同變體的產品。任務是允許用戶批量編輯「用於變體」字段的某些屬性。例如,我們的尺寸,性別仍然需要用於變體,但我們也有材質,僅用於過濾器,因此刪除此屬性不是解決方案。「用於變體」以編程方式切換
完全未經測試,並非特別理想,因爲如果您有很多產品,因爲它可能會超時,但理論上您可以遍歷所有可變產品並更新_product_attributes
元值。我強烈建議你有一個數據備份和/或這是一箇中轉站點。
function so_41978670_run_once(){
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
'tax_query' => array(
array(
'taxonomy' => 'post_type',
'field' => 'slug',
'terms' => 'variable',
),
),
);
$products = new WP_Query($args);
if($products->have_posts()){
while ($products->have_posts()) {
$products->the_post();
$product_id = get_the_ID();
$product = wc_get_product($product_id);
$attributes = $product->get_attributes();
$meta_values = array();
if ($attributes) {
foreach ($attributes as $attribute_key => $attribute) {
if(in_array($attribute_key, array('size', 'gender'))){
// Modify the attributes meta.
$attributes[ $attribute_key ]['is_variation'] = 1;
}
}
}
update_post_meta($product->get_id(), '_product_attributes', $attributes);
}
wp_reset_postdata();
}
}