I don't know how many products are like this, but I know there are many.
in the db;
The _regular_price
value of the post(wc product) is higher than the _price
value. I don't know how or when this happened, but the front end is displaying _price
- the lower of the two. ( this is bad, we are losing money )
I am trying to run a query that will find all of these post_id
s so that I can update them. I would like to update _price
with the value of _regular_price
at first I tried this;
SELECT post_id
FROM wp_postmeta
WHERE meta_key='_regular_price' > meta_key='_price'
this returns a very long list of post_id
many duplicates, and not always a product.
I then tried to adapt a different answer from stackexchange I came up with this
SELECT post_id,
post_title
FROM wp_posts
INNER JOIN wp_postmeta
ON post_id = post_id
WHERE post_type = 'product'
AND meta_key='_regular_price' > meta_key='_price'
however I am not exactly understanding inner join
and this query does not return the desired result either.
I believe i need to join
wp_posts & wp_postmeta here becuase post_type='product'
would be pulled from wp_posts
& meta_key='_regular_price' > meta_key='_price'
would be pulled from wp_postmeta
could it be that meta_key='_regular_price'
is not returning an integer?
my next thought was to compare these values with a function, and then update if needed ie;
//functions.php
add_action( 'after_theme_setup', 'benz_update_wc_meta' );
function benz_update_wc_meta() {
global $woocommerce, $post;
$reg_price = get_post_meta( get_the_ID(), '_regular_price', true );
$basic_price = get_post_meta( get_the_ID(), '_price', true );
if ( $reg_price > $basic_price ) {
update_post_meta( get_the_ID(), '_price', $reg_price );
}
}
or something like that -- but no luck there either.
Let me know if you have any advice. thanks.
I have also tried echoing out
$reg_price = get_post_meta( get_the_ID(), '_regular_price', true );
in the theme template woocommerce files -- and this displays the correct price, but when you add product to cart, the wrong price is still used.
_price
with_regular_price
value, regardless ifis greater
-- that would catch everything