Categories

Sunday, October 13, 2024
#919814419350 therichposts@gmail.com
WoocommerceWoocommerce Hooks

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

How to add download pdf link in woocommerce single product page?

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

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.

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.