Home > Software design >  WooCommerce - turn OFF manage stock by default
WooCommerce - turn OFF manage stock by default

Time:05-09

In WooCommerce, I would like to prevent "Enable stock management at product level" from staying checked in the Inventory tab.

When a product is imported or created, we want the box to be unchecked.

If it has been checked accidentally during editing, we want to uncheck it once the user saves or updates the product.

The box should never be able to stay checked. We only want stock management for variations.

add_action('save_post_product', 'default_uncheck_managestock_products', 10, 2);

function default_uncheck_managestock_products($postID, $post) {
        update_post_meta($post->ID, '_stock', '0');
        update_post_meta($post->ID, '_manage_stock', 'no'); 
}

This code will work when I create a new product. It is failing when I update an existing product, and it is failing when I import a product and then edit it.

Am I using the wrong hook? Is this a more complicated problem than I think it is? Thank you.

CodePudding user response:

add_action("woocommerce_before_product_object_save", function ($product, $data_store) {

    if ("variation" === $product->get_type()) {
        return;
    }

    $product->set_props([
        "manage_stock" => 'no'
    ]);


}, 10, 2);
  • Related