Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Woocommerce to Manage Stock as a default

Is there a hook that would allow setting the Inventory > Manage Stock checkbox in woocommerce globally?

All my products are single item quantity, so it seems pretty counter-D.R.Y to always have to remember to check it, especially for other employees.

like image 985
SWL Avatar asked Apr 09 '13 17:04

SWL


People also ask

How do I enable stock management in WooCommerce?

Go to: WooCommerce > Settings > Products > Inventory. Enable stock management – Inventory for physical products is auto-managed. You enter quantity, and WooCommerce subtracts items as sales are made, displaying: Stock, Out of Stock or On Backorder.

Can WooCommerce handle inventory?

You have several options for adding inventory to WooCommerce. You can create individual product pages or set up category pages that group related products. Because inventory for even small stores could number in hundreds, you need a WooCommerce inventory management plugin to manage the store operations.

Why does WooCommerce not display out of stock products?

Navigate to WooCommerce → Settings → Products. From the Products tab, select the Out of stock visibility checkbox. This will hide all out-of-stock products from your WooCommerce site so customers cannot add unavailable products to their shopping cart. Click the Save changes button.

How do I get WooCommerce stock status?

To create new stock status, go to Custom Stocks > Add New. While adding custom stock status you can customize the following options.


1 Answers

Although this is quite late, since you asked about a hook: although there doesn't appear to be a hook, action or function specifically for turning on the Manage Stock option, you can still hook into the post save function and auto-enable it that way, since it is just a post_meta value:

add_action('save_post', 'myWoo_savePost', 10, 2);

function myWoo_savePost($postID, $post) {
    if (isset($post->post_type) && $post->post_type == 'product') {

        update_post_meta($post->ID, '_manage_stock', 'yes');
    }
}

Note that stock levels will always default to 0, so you may also want to add the line:

update_post_meta($post->ID, '_stock', '1');

...which will update your stock quantity to 1. Do be aware that this will happen every time a product is saved, though.

I'm unsure as to whether this has knock-on effects elsewhere in WooCommerce for larger stock quantities, but as you're dealing with single-quantity items, I'd guess you're probably okay.

Update (with $update):

As of Wordpress 3.7, a third parameter was added to save_post so you can easily tell if it's a new post being created or an existing post being updated. As such, you can fire the function above only when creating a new post (which is arguably the desired effect):

add_action('save_post_product', 'myWoo_savePost', 10, 3);

function myWoo_savePost($postID, $post, $update) {
    if (!$update) {
        //  $update is false if we're creating a new post
        update_post_meta($post->ID, '_manage_stock', 'yes');
        update_post_meta($post->ID, '_stock', '1');
    }
}

(Thanks to Dylan for the reminder about post-type specific saves)

like image 110
indextwo Avatar answered Sep 20 '22 02:09

indextwo