doofinder / doofinder-woocommerce

🌐⚡ Integrate Doofinder in your WooCommerce site with (almost) no effort
GNU General Public License v3.0
2 stars 8 forks source link

Update on save needs some fixes #313

Open eugeniobonifacio opened 1 month ago

eugeniobonifacio commented 1 month ago

Hello,

I've been struggling with the "update on save" cron not working. I found a couple of issues that need to be fixed:

  1. Cron schedules must be registered always, not only when in the admin interface. Otherwise the cron schedule is being deleted.
  2. The Endoint API uses the underlaying Woocommerce API. However inside a cron job there's no user, which results in a 401 error code.

I temporary fixed them using the following hooks:

add_filter('woocommerce_rest_check_permissions', function ( $permission, $context, $object_id, $post_type ) {
if(wp_doing_cron() && $context === 'read' && doing_filter('doofinder_update_on_save')) {
        return true;
    }

    return $permission;
}, 100, 4);

add_filter('init', function () {
    if(!is_admin()) {
        add_filter( 'cron_schedules', array( Settings::class, 'add_schedules' ), 100, 1 );
    }
});

I hope this can be helpful to improve the plugin.

eugeniobonifacio commented 1 month ago

Sorry, the 'init' hook is not a filter but an action:

add_action('init', function () {
    if(!is_admin()) {
        add_filter( 'cron_schedules', array( Settings::class, 'add_schedules' ), 100, 1 );
    }
});