I want to empty price of simple product and variable product when product changed into out off stock. I have added one code for that its working well. But when adding this code on my site then when i uploading images into media library images are uploaded into wrong folder. If i upload image in 10th October 2019 then image is uploaded into /uploads/2019/04/ actually image need to upload into /uploads/2019/10/ folder. That issue due to my empty price code. so with out this conflict how i can empty price of out off stock product. My current code is following.
function wph_empty_price1() {
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => '=',
)
)
);
$loop = new WP_Query( $args ); // get all out of stock products
//var_dump($query);
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$args_variable = array(
'post_type' => 'product_variation',
'post_status' => array( 'publish' ),
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'asc',
'post_parent' => get_the_ID(),
'meta_query' => array(
array(
'key' => '_regular_price',
'value' => '',
'compare' => '!=',
)
)
);
$args_simple = array(
'post_type' => 'product',
'post_status' => array( 'publish' ),
'numberposts' => -1,
'p' => get_the_ID(),
'orderby' => 'menu_order',
'order' => 'asc',
'meta_query' => array(
array(
'key' => '_regular_price',
'value' => '',
'compare' => '!=',
)
)
);
$variations = get_posts( $args_variable ); // get all variations of out of stock product
$simples = get_posts( $args_simple ); // get all simple product of out of stock product
foreach ( $variations as $variation ) {
$variation_id = $variation->ID;
// empty price fields in backend
update_post_meta( $variation_id, '_price', '');
update_post_meta( $variation_id, '_regular_price', '');
update_post_meta( $variation_id, '_sale_price', '');
}
foreach ( $simples as $simple ) {
$simple_id = $simple->ID;
// empty price fields in backend
update_post_meta( $simple_id, '_price', '');
update_post_meta( $simple_id, '_regular_price', '');
update_post_meta( $simple_id, '_sale_price', '');
}
endwhile;
}else {
echo __( 'No products found' );
}
wp_reset_postdata();
}
add_action( 'init', 'wph_empty_price1' );