Scheduling actions

Since version 2.1 ShopMagic utilizes Action Scheduler to process all actions triggered by events in WooCommerce. This means that when the event occurs (i.e. the order is placed), the action (i.e. sending customer email) is added to the Action Scheduler queue and is slightly delayed.

We decided to use Action Scheduler to get better reliability and speed up WooCommerce events, for example, the checkout process or order fulfillment. However, this means that the actions will be processed with a slight delay when they get through the Action Scheduler process.

This delay will be less than a minute, but can also result in a longer time depending on your WP-Cron configuration.

Skipping the queue

Although this delay is small, in certain events this can still cause some user concerns. For example user registration or some other transactional events.

That's why we've also prepared a filter that you can use to skip the queue and process the actions immediately when the event happens. For all or selected events according to your needs.

All events

In order to skip the queue for all events and perform the actions immediately use the following code:

add_filter( 'shopmagic/core/queue/avoid_queue', '__return_true' );

Specific automation

The following example will skip the queue for automation with ID 99. Replace "99" with your automation ID.

add_filter( 'shopmagic/core/queue/avoid_queue', 'sm_avoid_queue_for_automation', 10, 2 );
/**
 * Skip ShopMagic queue for specific automation
 *
 * @param $avoid_queue
 * @param $automation
 *
 * @return bool
 */
function sm_avoid_queue_for_automation( $avoid_queue, $automation ) {

   $automation_id = 99; // Replace 99 with your automation ID

   if ( $automation->get_id() === $automation_id ) {
      return true;
   }

   return $avoid_queue;
}

You can find your automation ID in the URL when you edit it.

How to find automation ID

Specific events/actions

We will provide these code examples in the future.

Custom schedule with Delayed Actions extension

If you are using Delayed Actions, you can utilize another WordPress filter to set a custom schedule time for items added to the queue.

This way you can use some custom metadata from i.e. ordered product to use it as delay time (which is not possible normally, through ShopMagic placeholders).

add_filter('shopmagic/delayed_actions/delay_timestamp', function (
  int $timestamp,
  \Psr\Container\ContainerInterface $action_data,
  array $provided_data
): int {
  // Use custom timestamp only for actions with email subject 'Hello, new customer'.
  if ($action_data->get('subject_value') === 'Hello, new customer') {
    // Get meta from ordered item.
      $product_meta = $provided_data[ WC_Order::class ]->get_item_meta('my_custom_product_meta');
      // Return meta value as date timestamp.
      return (new DateTime($product_meta))->getTimestamp();
    }

  return $timestamp;
  }
 );

THe above example is just an illustration. Do not use this code in production, as it doesn't use enough type checks and data malformation.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us