WoocommerceWoocommerce Hooks

Woocommerce Hook – make Product Status Out of Stock after Order Place

Woocommerce Hook - make Product Status Out of Stock after Order Place

woocommerce
312Views

Hello, welcome to therichpost.com. In this post, I will tell you , Woocommerce Hook – make Product Status Out of Stock after Order Place. WordPress is the best cms and Woocommerce is the best Ecommerce plugin. WordPress hooks(add_action, add_filter) give us the power to edit or change the code without interruption into the files and this is the best thing about wordpress. Now I am going to tell you how the hooks work.

After placing the order, I am making ordered product of out of stock with hook.

Woocommerce Hook - make Product Status Out of Stock after Order Place

Here is the working and tested code for Woocommerce Hook – make Product Status Out of Stock after Order Place and you need to add this into your theme’s functions.php file:
add_action( 'woocommerce_thankyou', 'change_product_status', 10, 1 );
function change_product_status( $order_id ) {
if ( ! $order_id )
return;

$order = wc_get_order( $order_id );

foreach ($order->get_items() as $item_id => $item_values) {
$product_id = $item_values['product_id'];

update_post_meta($product_id, '_stock', 0);
wc_update_product_stock_status($product_id, "outofstock");
}
}

If you have any query related to this post, then you can comment below or ask question.

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

Leave a Reply

Positive SSL