0

I'm trying to include the custom image field inside the PDF file of Craft Commerce. I added the following code to the module:

use craft\commerce\events\PdfEvent;
use craft\commerce\services\Pdfs;

  Event::on(
            Pdfs::class,
            Pdfs::EVENT_BEFORE_RENDER_PDF,
            function(PdfEvent $event) {
                // Modify `$event->order`, `$event->option`, `$event->template`,
                // and `$event->variables` to customize what gets rendered into a PDF;
                // or render your own PDF and set its string on `$event->pdf`
                // ...

                $event->variables['productImages'] = array_map(function ($value) {

                    $variant = Plugin::getInstance()->variants->getVariantById($value['purchasableId']);
                    
                    $variantImage = $variant->getFieldValue('productImage')->one();
                    
                    return $variantImage ? $variantImage->getUrl() : null;
                }, $event->order->lineItems);
            }
        );

Dumping the variable actually gives me the result, so it should be accessible:

enter image description here

But when I'm trying to access the field 'variables' inside the template, it downloads the pdf file with an error text inside the file:

enter image description here

enter image description here

enter image description here

enter image description here

Can you tell me please what can be the cause of the issue here, or I'm doing something wrong? Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.