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.
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.
Recent Comments