verbb / gift-voucher

A Craft Commerce plugin to provide Gift Certificate/Voucher functionality.
Other
14 stars 16 forks source link

Empty PDF voucher #87

Open JoeriE opened 2 years ago

JoeriE commented 2 years ago

Describe the bug

I attach a PDF voucher to my order confirmation emails immediately after an order is finished. The PDF voucher is always blank. If I resend the confirmation mail via the admin, the PDF voucher is correct.

It seems that the codes are not found. If I loop through the code, it always shows 'No code found' after the automatic email.

{% for code in codes %}
  {# PDF template #}
{% else %}
  No code found
{% endfor %}

Steps to reproduce

  1. Attach PDF to email that is directly send after order is completed.

Craft CMS version

3.7.34

Plugin version

2.7.4

Multi-site?

No

Additional context

No response

engram-design commented 2 years ago

When you say I attach a PDF voucher to my order confirmation emails immediately after an order is finished how do you do this? Through the plugins Settings > Emails, or are you doing this yourself via events?

Secondly how is your queue being processed (where Commerce sends the emails), from a web request, automatically, or via the CLI? There should be no real difference between the methods.

JoeriE commented 2 years ago

@engram-design It's all just Craft Commerce settings. These are the settings of the e-mail which the PDF is attached to:

image

And this e-mail is sent when an order get status 'New', which is de default order status. So an order gets that status after the payment is done and the order is finished.

image
engram-design commented 2 years ago

Right, but are you also attaching the Gift Voucher PDF using these settings?

image
JoeriE commented 2 years ago

@engram-design Yes, I attached the gift voucher PDF using these settings. The PDF is empty when the email is sent automatically after an order is confirmed. If I go to the Admin > Commerce > Order and I click 'Send email' the PDF is correct.

engram-design commented 2 years ago

Okay, thanks. Can you please let me know how your queue is run? The fact that you don't have blank PDFs with run via Commerce > Order tells me that it's a difference when the emails are sent via the queue. Sending the email through the control panel doesn't use the queue, which is why it seems to work. Something must be different in your queue setup.

Can you also show me your full Twig template used in your PDF, in case there's something not right there?

JoeriE commented 2 years ago

@engram-design I didn't change anything how the queue is going. The queue is automatically running after an order so the e-mail is sent immediately.

This is my full twig template:

<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <style>
        html {
            margin: 0px !important;
        }

        @page {
            margin: 0px;
        }

        body {
            font-family: Times, 'Times New Roman', serif;
            font-size: 18px;
            line-height: 1.4em;
            margin: 0px;
            color: #8FA8C2;
        }

        * {
          box-sizing: border-box;
        }

        h1, h2, h3, h4, h5, .h1, .h2, .h3, .h4, .h5{
          font-family: Times, 'Times New Roman', serif;
          font-weight: 400;
        }

        h1,
        .h1 {
          text-transform: uppercase;
          font-size: 30px;
        }

        h2,
        .h2 {
          font-size: 24px;
        }

        .voucher {
            width: 100%;
            height: 4in;
            position: relative;
            display: block;
            background-color: #F8F5F1;
        }

        .voucher-img {
          width: 40%;
          height: 100%;
          display: block;
          background-size: cover;
        }

        .voucher-content {
          width: 60%;
          padding: 30px;
          display: block;
          margin-left: 40%;
          margin-top: -4in;
        }

    </style>
</head>

<body>

{% for code in codes %}
  <div style="margin-bottom: 30px; clear: both; page-break-after: always;">
    <div class="voucher">
        {% set url = labels.labels.giftCardImage.one().setTransform({width: 800}) %}
        <div class="voucher-img" style="background-image: url(data:image/jpg;base64,{{ image64(url) }});"></div>

        <div class="voucher-content">
          <h1>Nimzu cadeaubon</h1>

          <br>

          <div class="field-amount">
              ter waarde van
              <span class="h2">{{ code.currentAmount | commerceCurrency('EUR', stripZeros=true) }}</span>
          </div>

          <br>

          <div class="field-content">
              <div class="field-code">
                in te wisselen via de webshop <br>
                met coupon code: <span class="h2">{{ code.codeKey }}</span>
              </div>

              <br>

              {% if code.expiryDate %}
                  <div class="field-expiry">geldig tot en met {{ code.expiryDate | date("d/m/Y") }}</div>
              {% endif %}
          </div>
        </div>
     </div>
    </div>
{% endfor %}

</body>
</html>
engram-design commented 2 years ago

Okay, so the default behaviour is to use a web request, and the queue being sent automatically. Just good to know as sometimes people run the queue from the command line, and that can make a difference.

I still can't replicate this, but must have something to do with this not working https://github.com/verbb/gift-voucher/blob/6ec71133fe924c88e95d1860ee98a2a7559a319f/src/services/PdfService.php#L71-L80

WHITE-developer commented 1 year ago

@engram-design seeing the same after a Craft 4 update, It looks like that the EVENT_BEFORE_SEND_MAIL event is getting fired (which sends the email and generates the pdf), before that the EVENT_AFTER_COMPLETE_ORDER event is getting fired (that is creating the codes for the order

engram-design commented 1 year ago

Hmmm, now that would be interesting if things happened in that order. I'm not quite sure how that'd be possible, as I would assume the order needs to be completed before the mail is sent out.

Still, that shouldn't be an issue. I'm wondering if you can confirm if there's no codes present in the lines above, by adding:

Craft::dd(Code::find()->orderId($order->id)->all());

Because at that point, it can't find any codes for the order, but it can when run later on-demand.

WHITE-developer commented 1 year ago

We where sending the emails our self (not in a queue job) in a custom module in a EVENT_AFTER_COMPLETE_ORDER to check if a voucher has been ordered and a email/pdf should been sent.

So I guess our module is getting triggered before your Plugin. Although in Craft 3 it did go fine. But if we moved it to a queue job it would probably also fix it