Magento - Transaction E-Mails - Variables - magento

is it somehow possible to use {{var invoice.increment_id}} in the creditmemo e-mails? It don't work if I do so...
<p><strong>Bestellnummer:</strong> {{var order.increment_id}}</p>
<p><strong>Datum:</strong> {{var creditmemo.created_at}}</p>
<p><strong>Rechnung:</strong> {{var invoice.increment_id}}</p>
<p>Liebe(r) {{htmlescape var=$order.getCustomerName()}},<br/><br/>
Doesn't work. Does someone has a hint for me? Thanks!
EDIT
Is that correct?
Edit the class Mage_Sales_Model_Order_Creditmemo and adding:
//Get Invocie from order Object
if ($order->hasInvoices()) {
// "$_eachInvoice" is each of the Invoice object of the order "$order"
foreach ($order->getInvoiceCollection() as $_eachInvoice) {
$invoice = $_eachInvoice->getIncrementId();
}
//$invoice = $order->getInvoiceCollection();
}
aswell as
$mailer->setTemplateParams(array(
'order' => $order,
'creditmemo' => $this,
'comment' => $comment,
'invoice' => $invoice,
'billing' => $order->getBillingAddress()
)
);
and afterwards calling the variable {{var invoice}} in my email template?
But it doesn't show, what I'm missing here?

You should rewrite Mage_Sales_Model_Order_Creditmemo model in your module and add needed logic to sendUpdateEmail() method to get invoice object in credit memo email templates.

See my frist post. I got it right. Thanks to Zyava.
The problem was, you have to place this code one method above the sendUpdateEMail(), in sendEmail().
Thanks!

Related

How to change From Name in Laravel Mail Notification

This is the problem:
The name associated with the email shows up as "Example"
In config/mail.php
set from property as:
'from' => ['address' => 'someemail#example.com', 'name' => 'Firstname Lastname']
Here, address should be the one that you want to display in from email and name should be the one what you want to display in from name.
P.S. This will be a default email setting for each email you send.
If you need to use the Name as a variable through code, you can also call the function from() as follows (copying from Brad Ahrens answer below which I think is good to mention here):
return $this
->from($address = 'noreply#example.com', $name = 'Sender name')
->subject('Here is my subject')
->view('emails.view');
You can use
Mail::send('emails.welcome', $data, function($message)
{
$message->from('us#example.com', 'Laravel');
$message->to('foo#example.com')->cc('bar#example.com');
});
Reference - https://laravel.com/docs/5.0/mail
A better way would be to add the variable names and values in the .env file.
Example:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=example#example.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME="My Name"
MAIL_FROM_ADDRESS=support#example.com
Notice the last two lines. Those will correlate with the from name and from email fields within the Email that is sent.
In the case of google SMTP, the from address won't change even if you give this in the mail class.
This is due to google mail's policy, and not a Laravel issue.
Thought I will share it here.
For anyone who is using Laravel 5.8 and landed on this question, give this a shot, it worked for me:
Within the build function of the mail itself (not the view, but the mail):
public function build()
{
return $this
->from($address = 'noreply#example.com', $name = 'Sender name')
->subject('Here is my subject')
->view('emails.welcome');
}
Happy coding :)
If you want global 'from name' and 'from email',
Create these 2 keys in .env file
MAIL_FROM_NAME="global from name"
MAIL_FROM_ADDRESS=support#example.com
And remove 'from' on the controller. or PHP code if you declare manually.
now it access from name and from email.
config\mail.php
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'info#example.com'),
'name' => env('MAIL_FROM_NAME', 'write name if not found in env'),
],
ON my controller.
$conUsBody = '';
$conUsBody .= '<h2 class="text-center">Hello Admin,</h2>
<b><p> '.trim($request->name).' Want some assesment</p></b>
<p>Here are the details:</p>
<p>Name: '.trim($request->name).'</p>
<p>Email: '.trim($request->email).'</p>
<p>Subject: '.trim($request->subject).'</p>';
$contactContent = array('contactusbody' => $conUsBody);
Mail::send(['html' => 'emails.mail'], $contactContent,
function($message) use ($mailData)
{
$message->to('my.personal.email#example.com', 'Admin')->subject($mailData['subject']);
$message->attach($mailData['attachfilepath']);
});
return back()->with('success', 'Thanks for contacting us!');
}
My blade template.
<body>
{!! $contactusbody !!}
</body>
I think that you have an error in your fragment of code. You have
from(config('app.senders.info'), 'My Full Name')
so config('app.senders.info') returns array.
Method from should have two arguments: first is string contains address and second is string with name of sender. So you should change this to
from(config('app.senders.info.address'), config('app.senders.info.name'))

Magento: How to get current date in magento email template

How can I get Current date in Magento email template?
{{var dateAndTime}} doesn't work. I need to get current date in which the email is sent.
You have 2 options.
Option 1. is to rewrite the method that sends the e-mail and pass the current date as a parameter.
Let's say for example that you want to show the date in the order e-mail.
For that you will need to rewrite the Mage_Sales_Model_Order::sendNewOrderEmail method.
You need to change this:
$mailer->setTemplateParams(array(
'order' => $this,
'billing' => $this->getBillingAddress(),
'payment_html' => $paymentBlockHtml
)
);
To this:
$mailer->setTemplateParams(array(
'order' => $this,
'billing' => $this->getBillingAddress(),
'payment_html' => $paymentBlockHtml,
'dateAndTime' => Mage::getModel('core/date')->date('Y-m-d H:i:s'), //change format as needed.
)
);
Then you will be able to use {{var dateAndTime}} in your new order email template.
This is handy if you want to use your date and time in only one template.
If you want a more general case you need to create your own directive see option 2.
Option 2 creating your own {{}} directive.
Let's say that you want in every e-mail to use {{dateAndTime}} to print to current date and time.
You need to rewrite the class Mage_Widget_Model_Template_Filter and add a new method to it.
See this detailed explanation about how to do it.
Your new method should look like this:
public function dateAndTimeDirective($construction) {
return Mage::getModel('core/date')->date('Y-m-d H:i:s');
}
You can even take it up a notch and be able to pass the date format as a parameter like this:
{{dateAndTime format="Y-m-d"}}
in this case your method that handles the directive should look like this:
public function dateAndTimeDirective($construction) {
$params = $this->_getIncludeParameters($construction[2]);
$format = isset($params['format']) ? $params['format'] : 'Y-m-d H:i:s'
return Mage::getModel('core/date')->date($format);
}
Go to your email template from admin panel. Use block directive as follows
current date is :
{{block type='core/template' area='frontend' template='page/html/date.phtml'}}
Now create a new file date.phtml in your template as follows
app/design/frontend/yourtheme/yourtemplate/page/html/date.phtml
Add following code in php tags (use php date function and echo current date i.e date('d-m-Y'))
<pre><code>
$date = date('d-m-Y');
echo $date;
</code></pre>
Hope this helps!

Magento- new order notification email add custom field- table sales_flat_order

I would like to add custom variable on new order email notification having value populated from table sales_flat_order (i.e. heared4us ). How can I do this ?
I am using magento version 1.7.0.2
Thanks.
To add new fields to order e-mail you need to follow the following 2 steps
1) Edit sendNewOrderEmail() function located in
app/code/core/Mage/Sales/Model/Order.php
In that function you will find following code
$mailer->setTemplateParams(array(
'order' => $this,
'billing' => $this->getBillingAddress(),
'payment_html' => $paymentBlockHtml,
));
You need to add new key value pair to add new custom value
$mailer->setTemplateParams(array(
'order' => $this,
'billing' => $this->getBillingAddress(),
'payment_html' => $paymentBlockHtml,
'customvalue' => 'This is a custom value' //New custom value
));
2) Now the second part. You need to add the custom variable to new order email template.
Just edit the template add your custom parameter name. in the example it is "customvalue".
{{ var customvalue }}
For English the order e-mail template is located in
app\locale\en_US\template\email\sales\order_new.html
app\locale\en_US\template\email\sales\order_new_guest.html
So depending on your language used in the website select the proper template located inside locale folder.
Also you can edit the e-mail template from admin by navigating to
System > Transactional Emails > New Order Email
public function execute(\Magento\Framework\Event\Observer $observer) {
$transport = $observer->getEvent()->getTransport();
$transportObj = $observer->getData('transportObject');
/** #var \Magento\Framework\App\Action\Action $controller*/
$transport = $observer->getTransport();
$transportObj->setData('custom_content',"custom content 123");
return $transportObj;
}

Magento getting order grand total in onepage.php

I'm struggling with a very stupid problem.
I've edited the saveOrder() method in app/code/core/Mage/Checkout/Type/Onepage.php.
This because I wanted to prevent Magento from sending order confirmation email for some payment methods.
Instead of standard emails I'm sending a new one (coded in transactional emails in backend) with different information.
All it's ok, I've done something like:
if($order->getPayment()->getMethodInstance()->getCode()!='X') {
$order->sendNewOrderEmail();
} else {
$name = $order->getBillingAddress()->getName();
$mailer = Mage::getModel('core/email_template_mailer');
$emailInfo = Mage::getModel('core/email_info');
$emailInfo->addTo($order->getCustomerEmail(), $name);
$mailer->addEmailInfo($emailInfo);
$templateId = 3;
$storeId = Mage::app()->getStore()->getId();
$sender = Array('name' => 'XXX', 'email' => 'xxx#xxx.xxx');
$mailer->setSender($sender);
$mailer->setStoreId($storeId);
$mailer->setTemplateId($templateId);
$mailer->setTemplateParams(array(
'order' => $order
)
);
$mailer->send();
}
All works fine except for the total of the order. In the transactional email I'm printing
{{var order.getGrandTotal()}}
but I'm getting the value "0.999953719008" for a 1 euro price product and I don't know how to solve this. (The test product has got a discount)
I've tried creating a script which loads a previously registered order and sends the email using the same email template. In this case all works like a charm!
So I suppose that the problem is because the order isn't saved yet.
I've just tried passing the grand total as another variable using
$mailer->setTemplateParams(array(
'order' => $order,
'total' => $order->getGrandTotal()
)
);
and printing
{{var total}}
in the template and in this case there isn't any value for the variable.
How can I manage to solve this?
Thank in advance!
p.s.: I'm using an installation of 1.6 version of Magento.
I've had this before as well. It's due to php float rounding issues:
http://php.net/manual/en/language.types.float.php
Use round() to display your results correctly to the user. With bcadd() you can avoid the rounding issues if you change the price somewhere with custom code.

Magento New Cart Attribute

Hi well the problem I am facing seemed to be very simple at first but turned into a real nightmare now.
I was asked to add an attribute (namely point) to all the products (which was done pretty simple using the admin panel) and have its total as a cart attribute which rules can be set upon!?
I am quite positive that cart attributes are defined in:
class Mage_SalesRule_Model_Rule_Condition_Address extends Mage_Rule_Model_Condition_Abstract
{
public function loadAttributeOptions()
{
$attributes = array(
'base_subtotal' => Mage::helper('salesrule')->__('Subtotal'),
'total_qty' => Mage::helper('salesrule')->__('Total Items Quantity'),
'weight' => Mage::helper('salesrule')->__('Total Weight'),
'payment_method' => Mage::helper('salesrule')->__('Payment Method'),
'shipping_method' => Mage::helper('salesrule')->__('Shipping Method'),
'postcode' => Mage::helper('salesrule')->__('Shipping Postcode'),
'region' => Mage::helper('salesrule')->__('Shipping Region'),
'region_id' => Mage::helper('salesrule')->__('Shipping State/Province'),
'country_id' => Mage::helper('salesrule')->__('Shipping Country'),
);
$this->setAttributeOption($attributes);
return $this;
}
<...>
So if I overwrite this model and add an item to that array I will get the attribute shown in rule definition admin panel. It seems that all these attributes has a matching column in sales_flat_quote_address table except for total_qty and payment_method!
Now the problem is what should I do to have my new attribute be calculated and evaluated in rules processing? should I add a column to this table and update its value upon cart changes?
Any insight on how to do this would be of great value thanks.
I finally managed to accomplish the task and just for future reference I explain the procedure here.
The class mentioned in the question (ie: Mage_SalesRule_Model_Rule_Condition_Address) is the key to the problem. I had to rewrite it and for some odd reason I couldn't get what I needed by extending it so my class extended its parent class (ie: Mage_Rule_Model_Condition_Abstract).
As I said I added my attribute to $attributes like this:
'net_score' => Mage::helper('mymodule')->__('Net Score')
I also modified getInputType() method and declared my attribute as numeric
now what does the trick is the validate() method:
public function validate(Varien_Object $object)
{
$address = $object;
if (!$address instanceof Mage_Sales_Model_Quote_Address) {
if ($object->getQuote()->isVirtual()) {
$address = $object->getQuote()->getBillingAddress();
}
else {
$address = $object->getQuote()->getShippingAddress();
}
}
if ('payment_method' == $this->getAttribute() && ! $address->hasPaymentMethod()) {
$address->setPaymentMethod($object->getQuote()->getPayment()->getMethod());
}
return parent::validate($address);
}
as you can see it prepares an instance of Mage_Sales_Model_Quote_Address and sends it to its parent validate method. you can see that this object ($address) does not have payment_method by default so this method creates one and assigns it to it. So I did the same, simply I added the following code before the return:
if ('net_score' == $this->getAttribute() && ! $address->hasNetScore()) {
$address->setNetScore( /*the logic for retrieving the value*/);
}
and now I can set rules upon this attribute.
Hope that these information saves somebody's time in the future.

Resources