I have a sheet that is exported from my customers Back end stock system.
It includes sale price and retail price. The problem is for the sale pricing if there is no price they are using “0” as the value which in turn means that when the product update runs the product essentially becomes free
I have tried a few options from here. This one seemed to work the best
add_action ('woocommerce_before_shop_loop_item', 'check_sale_price', 40 );
add_action('woocommerce_before_single_product', 'check_sale_price', 40 );
function check_sale_price() {
global $product;
if ( $product->sale_price == '0' ) {
$price = $product->regular_price;
$product->sale_price = $price;
$product->price = $price;
global $wpdb;
$wpdb->get_results( 'UPDATE wp_postmeta SET meta_value='.$price.' WHERE meta_key="_sale_price" AND post_id='.$product->id, OBJECT );
$wpdb->get_results( 'UPDATE wp_postmeta SET meta_value='.$price.' WHERE meta_key="_price" AND post_id='.$product->id, OBJECT );
}
}
But this did not update all my variable products only simple products
It would be great if this code could also update product sale pricing that is “0” in product variations as well
3
Answers
Tested and works with query you can use it in your function.
Try this (where,
$price = "set you price"
,$product->id = "product id"
)Try this for update product price
Try This