Automatically check the option “Enable stock management at product level” on product creation

To set default values of a new post, you can try this code :

$postType = "product";

add_action("save_post_" . $postType, function ($post_ID, \WP_Post $post, $update) {

    if (!$update) {

        // default values for new products

        update_post_meta($post->ID, "_manage_stock", "yes");
        update_post_meta($post->ID, "_stock", 1);


        return;

    }


    // here, operations for updated products


}, 10, 3);

Leave a Comment