2017-02-01 33 views

回答

0

完全未經測試,並非特別理想,因爲如果您有很多產品,因爲它可能會超時,但理論上您可以遍歷所有可變產品並更新_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(); 
    } 
} 
相關問題