phpsqlwordpresswoocommercepost-meta

Update product variations prices in Woocommerce with WPDB query


I am using WordPress 4.7.4 and WooCommerce 3.0.5.

I updated _sale_price, _price, _regular_price using meta_id with code below:

   $meta_tbl = $wpdb->prefix.'postmeta';
   foreach ($_POST['loop'] as $loop_k => $loop_v) {
        $wpdb->update(
            $meta_tbl,
                array( 'meta_value' => $loop_v['price'] ),
                array( 'meta_id' => $loop_v['price_meta_id'] ),
                array( '%d' )
            );
        $wpdb->update(
            $meta_tbl,
                array( 'meta_value' => $loop_v['regular_price'] ),
                array( 'meta_id' => $loop_v['regular_price_meta_id'] ),
                array( '%d' )
            );
        $wpdb->update(
            $meta_tbl,
                array( 'meta_value' => $loop_v['sale_price'] ),
                array( 'meta_id' => $loop_v['sale_price_meta_id'] ),
                array( '%d' )
            );
  }

But still the shop page is displaying the old price of products! How can I do it properly?


Solution

  • Try by adding this line after your code.

    wc_delete_product_transients( $post_id );
    

    // $post_id replace with product ID