如何从WooCommerce的一个地方更新所有产品的价格?

时间:2018-10-22 07:26:29

标签: wordpress woocommerce

我有300多种产品。现在需要将所有产品的价格乘以6。例如,如果价格为10,则应该为60 ...

谢谢

1 个答案:

答案 0 :(得分:0)

要更新变化价格,请使用WP_Query循环

将以下代码粘贴到当前的活动主题functions.php文件中。

$query = array(
                'post_status' => 'publish',
                'post_type' => array('product', 'product_variation'),
                'posts_per_page' => -1

            );

    $the_query = new WP_Query($query);

     //product loop 
    if ( $the_query->have_posts() ) {
       while ( $the_query->have_posts() ) {

       global $post;


            $product = wc_get_product( $post->ID ); // An instance of the WC_Product object

         // Only for variable products
         if( $product->is_type('variable') ){

            foreach( $product->get_available_variations() as $variation_values ){

              $variation_id = $variation_values['variation_id']; // variation id

              $regular_price  = get_post_meta( $variation_id, '_regular_price', true);
              $regular_price  = $regular_price  * 6;

              // Updating active price and regular price
              update_post_meta( $variation_id, '_regular_price', $regular_price );
              update_post_meta( $variation_id, '_price', $regular_price );

              wc_delete_product_transients( $variation_id ); // Clear/refresh the variation cache
           }
           // Clear/refresh the variable product cache
           wc_delete_product_transients( $post->ID );
         }
       }
       wp_reset_postdata();
    }

希望对您有帮助。