- Posts: 4
- Thank you received: 0
function addAttachment in onAfterOrderCreate
2 years 10 months ago #356359
by Dasa
function addAttachment in onAfterOrderCreate was created by Dasa
-- 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?
-- 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.
2 years 10 months ago #356376
by nicolas
Replied by nicolas on topic function addAttachment in onAfterOrderCreate
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
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.
2 years 10 months ago #356378
by Dasa
Replied by Dasa on topic function addAttachment in onAfterOrderCreate
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.
2 years 10 months ago #356387
by nicolas
Replied by nicolas on topic function addAttachment in onAfterOrderCreate
Hi,
Well, without knowing the code you're using to generate the QRCode I can't say much.
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.
2 years 10 months ago - 2 years 10 months ago #356393
by Dasa
Replied by Dasa on topic function addAttachment in onAfterOrderCreate
I used simple PHP QR Code library (
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.
$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: 2 years 10 months ago by Dasa.
Please Log in or Create an account to join the conversation.
2 years 10 months ago #356400
by nicolas
Replied by nicolas on topic function addAttachment in onAfterOrderCreate
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.
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.
2 years 10 months ago #356454
by Dasa
Replied by Dasa on topic function addAttachment in onAfterOrderCreate
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)
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.
2 years 10 months ago #356457
by nicolas
Replied by nicolas on topic function addAttachment in onAfterOrderCreate
Hi,
Check the $mail variable and you'll see that you have the necessary data in there.
I would add something like that:
Check the $mail variable and you'll see that you have the necessary data in there.
I would add something like that:
Code:
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.254 seconds