0

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' );
  • What’s the ultimate goal here? Just to hide the price on out of stock products? Or do you actually need to remove it entirely for some reason? – Jacob Peattie Oct 10 '19 at 8:56
  • @JacobPeattie my ultimate goal is when product go out off stock i want to remove the price from that product. – developerme Oct 10 '19 at 8:57
  • You're not understanding. For what purpose? Just to visually hide it? Would hiding the price be sufficient? Or do you need to completely remove it? If so, why? – Jacob Peattie Oct 10 '19 at 9:07
  • We need to remove that price for our inventory management – developerme Oct 10 '19 at 9:35
  • Yes i can confirm that init hook creating the problem but with out that init hook the function is not working – developerme Oct 10 '19 at 9:51

Your Answer

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

Browse other questions tagged or ask your own question.