1

So I've actually created a fix for this, but it's a bit complicated. Wanted to share in case anyone else runs into this problem.

Working in Craft 2.6.2954 and Charge Pro 2.18

When creating new subscriptions, the emails were not being sent properly. I checked the charge logs in the charge plugin and it was giving me an error of "Failed to find email with the handle : Welcome Email". I figured out that "Welcome Email" was the correct name of my email, but it wasn't the handle, which is why it wasn't being found. I created a new function in the Charge_EmailService.php file called _getEmailModelByName, you can see it here:

private function _getEmailModelByName($handle = null)
{
    if ($handle) {
        $emailRecord = Charge_EmailRecord::model()->findByAttributes(
            ['name' => $handle]);

        if (!$emailRecord) {
            return false;
        }

        $model = Charge_EmailModel::populateModel($emailRecord);

        return $model;
    }

    return false;
}

I then updated the sendByHandle function to do a few checks on the handle and try to find the email a different way. Here's the new sendByEmail function:

public function sendByHandle($handle, ChargeModel $chargeModel, $extra = [])
{

    if(is_int($handle)){
        $emailModel = $this->_getEmailModelById($handle);
    } else {
        $emailModel = $this->_getEmailModelByHandle($handle);
    }

    if($emailModel == false) {
        $emailModel = $this->_getEmailModelByName($handle);
    }


    if($emailModel == false) {
        craft()->charge_log->error('Failed to find email with the handle : '.$handle, ['extra' => $extra, 'charge' => $chargeModel]);
        return false;
    }

    $data['charge'] = $chargeModel;
    foreach($extra as $key => $arr) {
        $data[$key] = $arr;
    }

    $emailModel->send($data);
}

Looks like this is now working as it should. Hope it saves someone else some frustration.

  • Can you add your solution as official answer to the original question? I'd also suggest contact the author of the plugin to look into the original problem in case it's a bug and so you wouldn't have to maintain hacking core plugin files across updates. – Brad Bell Dec 22 '16 at 18:27
  • Thanks for the suggestions! I am in contact with the author of the plugin, but they have been kinda unresponsive. I also pointed them to this thread so that they can see what changes I made. – Natalie Dec 23 '16 at 19:48
  • @Natalie I'm the developer of Charge here. Unfortunately, this fix isn't ideal. By design the emails must be passed via the handle, not via the email title. Titles aren't checked for uniqueness or validated in other ways, so we can't be sure the email is correct. I've added a new patch into the logging system to watch for the situation and pass a direct message with the appropriate fix - which is to explicitly pass in the handle, in this case welcome_email – Joel Bradbury Apr 10 '17 at 8:41
2

I've actually answered the question in the question. Not sure why it's not showing as is, but the solution is updating the plugin file as mentioned above.

  • This worked for me. Can the developer of Charge please add this into the next release. Thanks – Adam Menczykowski Apr 5 '17 at 19:13
2

I'm the developer of Charge.

The above issue is caused because you're passing in the title of the email, not the handle.

We explicitly require the handle to be passed to ensure uniqueness. While the above patch would bypass the issue and let you define it by title, it's not ideal, and the proper fix is to the simply pass it by the handle in your templates.

From the above example, the handle would be welcome_email. It's directly shown in the email table in your Charge > Settings > Emails.

To help other's who might end up mistakenly passing the title instead of the handle, I've added a patch to the 2.2.1 release which explicitly watches for instances of a passed title instead of a handle, and throws a new message in the logging to notify the developer.

  • FYI I used the JS 'picker' in the admin area of charge plugin to pick the emails to be sent, I did not set up the emails in my templates using the success property in the object. – Adam Menczykowski Apr 10 '17 at 16:21
  • Still getting the same issue Joel. I just updated the plugin to 2.2 locally and tested a purchase in Stripe test mode. The logs state that Failed to find email with the handle : Welcome Email and after payment was accepted the front end displayed a Recoverable Error Object of class Craft\Charge_SubscriptionModel could not be converted to string – Adam Menczykowski Apr 10 '17 at 16:27
  • Ah. Thanks, that's a lot clearer. I'll patch that in the next release. From the above, I'd understood the issue as defining the email at the template level. I'll get a patch out shortly. – Joel Bradbury Apr 11 '17 at 9:32

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.