0

I need to update all prices in my store to end in .99, both on the front end and in the database. I have the following code which appears to do it when I run a shortcode, but I have a problem.

When I look at the products in the admin they sill show the old prices. I've cleared the transients and regenerated the product lookup tables but the old prices are still showing, which makes me think the new prices are only shown on the front end, even though the code is clearly updating the database. Bit confused.

add_shortcode( 'shortcode_update_price','shortcode_update_price' );
function shortcode_update_price(){
  global $wpdb;

  $prefix = $wpdb->prefix;
  $results = $wpdb->get_results( "SELECT `meta_id`, `meta_value` FROM {$prefix}postmeta WHERE `meta_key`='_price'", ARRAY_A );
  if ( !empty($results) ) {
    foreach ($results as $key => $value) {
      $new_price = ceil($value['meta_value']) - 0.01; 
      $wpdb->query( "UPDATE {$prefix}postmeta SET `meta_value`=$new_price WHERE `meta_id`=$value[meta_id]" );
    }
  }

}

enter image description here

What am I doing wrong? Is this likely still just a caching issue?

0

You should try to use _regular_price meta key in your SQL query.

Regular price from the admin use the value of _regular_price key while the price shown at the frontend use the value of _price key. Keep in mind that _price is the active price of the product, if the product is currently on sale then the _price will use the value of _sale_price. The value of _price will be updated daily by default WC hook cron.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.