I found that Woocommerce is disabling autosave, maybe for good reason, but I’d like to have it turned on and see if it presents an issue. This is what I found in the __construct in their post type class, from wp-content/plugins/woocommerce/includes/admin/class-wc-admin-post-types.php….

// Disable Auto Save
add_action( 'admin_print_scripts', array( $this, 'disable_autosave' ) );

Then further down I see this function….

/**
 * Disable the auto-save functionality for Orders.
 */
public function disable_autosave() {
       global $post;
       if ( $post && in_array( get_post_type( $post->ID ), wc_get_order_types( 'order-meta-boxes' ) ) ) {
                  wp_dequeue_script( 'autosave' );
            }
    }

I tried commenting out the wp_dequeue_script call above just to test if that script was enqueued would it work and still does not autosave. But I’d rather not as it is in the core Woocommerce and subject to updates. How can I re-enable without altering the plugin? Or does anyone have any experience as to why I should not?

The way I am testing is by adding this to the save_post hook:

add_action( 'save_post', 'save_product_meta' );
function save_product_meta( $post_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        error_log("DOING AUTOSAVE for " . $post_id);
        return $post_id;
}

In WordPress admin, I get the log entry while letting a normal post sit on screen without saving, but not when a shop_order type post is created.

2 Answers
2

You need to remove the action that WC added by doing something like

remove_action( 'admin_print_scripts', array( $this, 'disable_autosave' ) );

There are two tricky parts to doing that well

  1. This needs to run after WC add its own action
  2. you need to figure out how to find the exact same $this value that was used when the action was added.

I would still advice to get in touch with WC support because only they might guaranty that disable_autosave is a stable function name that they are unlikely to change in the next version

Leave a Reply

Your email address will not be published. Required fields are marked *