1

Curious if there is a way to fire order confirmation emails immediately? Basically how this was handled in Commerce 2.

There are cases, and I'm dealing with one currently, where there is no command line access to the hosting or the possibility of setting up cron jobs and the likes to trigger the queue on an interval...

Is there a workaround?

1

There are a couple of options here.

Number 1

If you have the config setting runQueueAutomatically set to false you could call the controller action queue/run via ajax from your success page. This would trigger the queue to run.

Number 2

You can use the EVENT_AFTER_COMPLETE_ORDER event to run some custom code, in a module or plugin. In that code you can include Craft::$app->getQueue()->run() this will also trigger the queue to run. e.g.

Event::on(Order::class, Order::EVENT_AFTER_COMPLETE_ORDER, function(Event $e) {
    Craft::$app->getQueue()->run();
});
| improve this answer | |
  • I'm not extremely adept with the yii/php side of things in Craft, I'm assuming this would need to be wrapped into a module's functionality. – Anders Apr 1 at 2:13
  • ... Meaning inside an init() function or something like that. – Anders Apr 1 at 4:46
  • Yea @Anders that is right 👍 – nfourtythree Apr 2 at 7:32
  • Figured it out, thanks! – Anders Apr 2 at 21:33
  • Can you share your solution? – Wonko Apr 3 at 15:49
1

Per request; I basically just dropped the code into the example module that comes with craft. I have some other modules and didn't want to clutter things. Here's the full code of the module;

<?php
namespace modules;

use Craft;
use craft\commerce\elements\Order;
use yii\base\Event;

/**
 * Custom module class.
 *
 * This class will be available throughout the system via:
 * `Craft::$app->getModule('my-module')`.
 *
 * You can change its module ID ("my-module") to something else from
 * config/app.php.
 *
 * If you want the module to get loaded on every request, uncomment this line
 * in config/app.php:
 *
 *     'bootstrap' => ['my-module']
 *
 * Learn more about Yii module development in Yii's documentation:
 * http://www.yiiframework.com/doc-2.0/guide-structure-modules.html
 */
class Module extends \yii\base\Module
{
    /**
     * Initializes the module.
     */
    public function init()
    {
        // Set a @modules alias pointed to the modules/ directory
        Craft::setAlias('@modules', __DIR__);

        // Set the controllerNamespace based on whether this is a console or web request
        if (Craft::$app->getRequest()->getIsConsoleRequest()) {
            $this->controllerNamespace = 'modules\\console\\controllers';
        } else {
            $this->controllerNamespace = 'modules\\controllers';
        }

        parent::init();

        // Custom initialization code goes here...
        Event::on(Order::class, Order::EVENT_AFTER_COMPLETE_ORDER, function(Event $e) {
            Craft::$app->getQueue()->run();
        });
    }
}

| improve this answer | |

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.