2

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.

  • Why do not ask WooCommerce for support? Do you see woocommerce in the URL or title? – Max Yudin Feb 25 '17 at 15:51
  • Woocommerce takes days to get back to you and gives vague answers. I'll edit my post with more detail. – Robert Fitzpatrick Feb 25 '17 at 15:59
  • I upvoted but please try to remember that plugin specific questions are off-topic. The only way that you can make them on-topic is by adding all of the relevant code from the plugin if needed. – Mark Kaplun Feb 25 '17 at 16:34
0

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

0

Ended up doing my own autosave posting to post.php using AJAX....

add_action( 'admin_head', 'woocommerce_admin_init' );
function woocommerce_admin_init() {
  $screen = get_current_screen();
  if ( $screen->post_type == "shop_order" && $screen->base == "post" ) {
  ?>
  <script type="text/javascript">
    jQuery(document).ready(function ($) {
            var unsaved = false;
            $('#post input').on('change', function() {
                unsaved = true;
            });
            function wc_autosave(){
                //alert(unsaved);
                if (unsaved) {
                    $("#post").block({ message: "<h1>Please wait...</h1><h3>saving</h3>", centerY: false, css: { top: '50px' } });
                    $.ajax({
                        data: $("#post").serialize(),
                        type: "POST",
                        url: '<?php echo admin_url('post.php'); ?>',
                        async: false
                    }).complete(function(){
                        unsaved = false;
                        $("#post").unblock();
                        setTimeout(function(){wc_autosave();}, 10000);
                    });
                } else {
                    setTimeout(function(){wc_autosave();}, 10000); return;
                }
            }
            wc_autosave();
    });
  </script>
  <?php
  }
}

Probably need some tweaking, might consider only applying to new posts and not when editing to avoid leaving unchanged records. But seems to work nicely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.