2

I have the product id:- $posts. Now i have to change product variable variation stock to outofstock using code.I have bellow snippet code but it's not updating the variation stock of the product. so any one tell me how to do this.

$children = get_posts( array(
      'post_parent'   => $posts,
      'posts_per_page'=> -1,
      'post_type'   => 'product_variation',
      'fields'     => 'ids',
      'post_status'  => 'publish'
    ) );

    $out_of_stock_staus = 'outofstock';

    foreach ( $children as $child_id ) {
        echo $child_idss= $child_id->ID;
      echo $child_stock_status = get_post_meta( $child_id, '_stock_status', true );
     // $child_stock_status = $child_stock_status ? $child_stock_status : 'instock';
        //wc_update_product_stock_status( $product_id, $stock_statusss );
        update_post_meta( $child_id, '_stock_status', wc_clean( $out_of_stock_staus ) );
    }
2

Most parts of your code were unnecessary.

Assuming you get the correct set of posts with your get_posts(), you can just loop through those posts, and update the meta.

You also had a confusion of the variables and objects in your loop, and you tried to update/get information passing a post object instead of an $id.

$children = get_posts( array(
    'post_parent'   => $posts,
    'posts_per_page'=> -1,
    'post_type'   => 'product_variation',
    'post_status'  => 'publish'
) );

$out_of_stock_staus = 'outofstock';

foreach ( $children as $thischild ) {

    update_post_meta( $thischild->ID, '_stock_status', wc_clean( $out_of_stock_staus ) );

}

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.