function addAttachment in onAfterOrderCreate

  • Posts: 4
  • Thank you received: 0
5 months 3 weeks ago #356359

-- HikaShop version -- : 5.0.0
-- Joomla version -- : 4.0.3
-- PHP version -- : 7.4.33
-- Browser(s) name and version -- : browser and version independent
-- Error-message(debug-mod must be tuned on) -- : no error message

Hi, I am trying to develope Hikashop payment plugin, what will generate a payment QR code. It works properly except one issue - I am not able to add image with QR code as attachment to email. I used this:

public function onAfterOrderCreate(&$order, &$send_email) {
$qrCodeFile = JPATH_ROOT . '/images/qrcode.png';
$mailer = JFactory::getMailer();
$mailer->addAttachment($qrCodeFile, 'qrcode.png');
$send_email = true;
}
There are no error messages, everything seems to be fine, but email order confirmation comes without the attachment. Path of the file is OK, the file exists, attachments are allowed in the settings in Hikashop. could anyone give me some advice?

Please Log in or Create an account to join the conversation.

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
5 months 3 weeks ago #356376

Hi,

You can't do it like this.
The line:
$mailer = JFactory::getMailer();
will instantiate a new mailer object.
The line:
$mailer->addAttachment($qrCodeFile, 'qrcode.png');
will add the attachment to this mailer object.
But then, you don't call the send function or add a receiver email address, etc to the mailer object so nothing happens.

HikaShop, will load its emails with its own instance of the mailer, different from yours.
If you want to have a plugin add attachment to the emails sent by HikaShop, you need to implement an event which gives you access to the mailer instance.

For example, HikaShop has the event onBeforeMailSend(&$mail, &$mailer) which you can implement. This gives you access to the mailer instance used by HikaShop so you can call the addAttachment function on it directly.
www.hikashop.com/support/documentation/6...tml#onBeforeMailSend

Please Log in or Create an account to join the conversation.

  • Posts: 4
  • Thank you received: 0
5 months 3 weeks ago #356378

Thank you. It worked for me with the onBeforeMailSend(&$mail, &$mailer), but in the wrong way. The attached QRcode came from a previous order...

Please Log in or Create an account to join the conversation.

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
5 months 3 weeks ago #356387

Hi,

Well, without knowing the code you're using to generate the QRCode I can't say much.

Please Log in or Create an account to join the conversation.

  • Posts: 4
  • Thank you received: 0
5 months 3 weeks ago #356393

I used simple PHP QR Code library ( https://sourceforge.net/projects/phpqrcode/ ), and this is the code generating QR code:
$account = "CZ560300000000011111111";
$variableSymbol = $order->order_number;
$amount = $order->order_full_price;

$qrCodeData = "SPD*1.0*ACC:$account*AM:$amount*CC:CZK*X-VS:$variableSymbol";

// setting path for QR code image
$filePath = JPATH_ROOT . '/images/qrcode.png';

// QR code generating
QRcode::png($qrCodeData, $filePath, QR_ECLEVEL_L, 6);

I assume that the problem occurs because the QR code is generated and the email is compiled at the same time, so the new version of the image is not attached to the email, but the old version with the same name is inserted.

Last edit: 5 months 3 weeks ago by Dasa.

Please Log in or Create an account to join the conversation.

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
5 months 3 weeks ago #356400

Hi,

You could make the file path dynamic, like this:
$filePath = JPATH_ROOT . '/images/qrcode'.$order->order_id.'.png';
That way, you would make sure that the QR code used could only be the one of the order being handled.

Please Log in or Create an account to join the conversation.

  • Posts: 4
  • Thank you received: 0
5 months 3 weeks ago #356454

Thank you very much for your patience, but...
The first part works correctly - it saves the image with the correct unique name, but I can't load the same image as an attachment to an email because I can't call $order_id inside the function onBeforeMailSend(&$mail, &$mailer)

Please Log in or Create an account to join the conversation.

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
5 months 3 weeks ago #356457

Hi,

Check the $mail variable and you'll see that you have the necessary data in there.
I would add something like that:

if(empty($mail->data->order_id)) {
 return; // skip attachment when order data is not available
}
$filePath = JPATH_ROOT . '/images/qrcode'.$mail->data->order_id.'.png';
if(!file_exists($filePath)) {
 // generate the file
}

// add the file to the email
$mailer->addAttachment($filePath);

Please Log in or Create an account to join the conversation.

Time to create page: 0.075 seconds
Powered by Kunena Forum