How to get Template Variables inside sendMessage() function in Magento 2? - magento

I am trying to send transactional emails from Magento through Listrack. I have override the default sendMessage function using Magento plugin method. Now I have to fetch template variables and process the data inside this function. I tried a lot and was not able to fetch any details regarding the template. Can anyone please help me to resolve this?
sendMessage function is located in Transport.php file.

Replace the "Email Template Name" with your Template Name
$templateId = Mage::getModel('core/email_template')->loadByCode('Email Template Name')->getId();
$customerEmail = 'your mail Id';
$customerName = 'your name';
$senderName=Mage::getStoreConfig('trans_email/ident_general/name');
$senderEmail= Mage::getStoreConfig('trans_email/ident_general/email');
$sender = Array('name' => $senderName,'email' => $senderEmail);
$vars = array(
'customerName' => $customerName,
'date' => $date,
'url'=>$url,
'number'=>$number,
'barcode'=>$serial_barcode,
);
$translate = Mage::getSingleton('core/translate');
Mage::getModel('core/email_template')->sendTransactional($templateId, $sender, $customerEmail, $customerName, $vars, null);
$translate->setTranslateInline(true);

Related

Mailchimp PHP Marketing API pass request parameters

I am using Mailchimp's Marketing PHP API (https://github.com/mailchimp/mailchimp-marketing-php) to retrieve the templates list. It has the following code.
<?php
require_once '/path/to/MailchimpMarketing/vendor/autoload.php';
$client = new MailchimpMarketing\ApiClient();
$client->setConfig([
'apiKey' => 'YOUR_API_KEY',
'server' => 'YOUR_SERVER_PREFIX',
]);
$response = $client->templates->list();
print_r($response);
And it returns the whole set of other default templates (128 templates!), the api request parameter has type field which can be used to filter these templates. But I could not find anyway to pass the request parameter. Any idea?
I am not sure if this is a correct approach but after going through API library source code, I found the correct function to include request parameters.
<?php
require_once '/path/to/MailchimpMarketing/vendor/autoload.php';
$client = new MailchimpMarketing\ApiClient();
$client->setConfig([
'apiKey' => 'YOUR_API_KEY',
'server' => 'YOUR_SERVER_PREFIX',
]);
$response = $this->client->templates->listWithHttpInfo($fields = null, $exclude_fields = null, $count = '10', $offset = '0', $created_by = null, $since_created_at = null, $before_created_at = null, $type = 'user', $category = null, $folder_id = null, $sort_field = null);
print_r($response);

Magento controller url redirects to dashboard

Basically in trying to create an inbox message which "read details" should redirect the user to a custom controller, however i can see the desired url in the browser for a second and then it redirects to the dashboard; this is how, currently, im trying to achieve that:
$myId = $myJson['id'];
$title = "Title of my notice";
$description = $myJson['text'];
$url= Mage::helper("adminhtml")->getUrl('My_Module/Controller/index', array('id' => $myId));
$sendingMessage = Mage::getModel('adminnotification/inbox')->addNotice($title,$description,$url);
The code above successfully adds the message to the inbox, however as i said before, i can see the desired URL in the browser before it gets redirected to the dashboard.
I'm accessing the same controller from another one and it does it as expected, the one that is actually working is a Grid and it looks something like this:
$this->addColumn('action',
array(
'header' => __('Answer'),
'width' => '100',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => __('Answer'),
'url' => array('base'=> '*/Controller'),
'field' => 'id'
)),
'filter' => false,
'sortable' => false,
'index' => 'stores',
'is_system' => true,
));
So, am i missing something here ?
BTW, is there any way to make the "read details" link to open in the same page instead of a new tab?
==================================================================
UPDATE
Disabling the "Add Secret Key to URLs" in the security options allowed me get it work, however i would like to make use of the secret keys.
The URLs i'm generating in the first code block actually have a key/value in the URLs, they look something like this:
https://example.com/index.php/mymodule/Controller/index/id/3963566814/key/f84701848a22d2ef36022accdb2a6a69/
It looks like you're trying to generate an admin URL. In modern versions of Magento, admin urls must use the adminhtml front name, using the Magento Front Name Sharing technique (described in this article). That's must as in if you don't, the URLs won't work. Magento removed the ability to create non-adminhtml URLs in the backend.
Second, here's where Magento generates the secret keys
#File: app/code/core/Mage/Adminhtml/Model/Url.php
public function getSecretKey($controller = null, $action = null)
{
$salt = Mage::getSingleton('core/session')->getFormKey();
$p = explode('/', trim($this->getRequest()->getOriginalPathInfo(), '/'));
if (!$controller) {
$controller = !empty($p[1]) ? $p[1] : $this->getRequest()->getControllerName();
}
if (!$action) {
$action = !empty($p[2]) ? $p[2] : $this->getRequest()->getActionName();
}
$secret = $controller . $action . $salt;
return Mage::helper('core')->getHash($secret);
}
and here's where it validates the secret key
#File: app/code/core/Mage/Adminhtml/Controller/Action.php
protected function _validateSecretKey()
{
if (is_array($this->_publicActions) && in_array($this->getRequest()->getActionName(), $this->_publicActions)) {
return true;
}
if (!($secretKey = $this->getRequest()->getParam(Mage_Adminhtml_Model_Url::SECRET_KEY_PARAM_NAME, null))
|| $secretKey != Mage::getSingleton('adminhtml/url')->getSecretKey()) {
return false;
}
return true;
}
Compare the pre/post hash values of $secret to see why Magento's generating the incorrect key on your page.

update the frontend label of an attribute in magento programmatically?

I have an attribute with some attribute front end label.But I want to change frontendlabel for the product.Is there any function provided in magento to do this programmatically.
If yes,How can this be done???
Thank you,
The safest way to do that is to load the existing labels and then add your new store specific labels:
1 - retrieve the existing labels for all the stores
Mage::getModel('eav/entity_attribute')->getResource()
->getStoreLabelsByAttributeId($attributeId);
or
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$labels= $attribute->getStoreLabels();
2 - Add your custom labels
$labels[$storeId] = 'My new label for a specific store';
$attribute->setStoreLabels($labels)->save();
If you want to set up frontend store labels for each store views, you can do it next way(This code for custemer attribute, but you can set frontend labels this way for all type of EAV-attributes.):
$installer->startSetup();
$attribute = array(
'entity_type' => 'customer',
'code' => 'customer_type_id',
'translations' => array(
'store_code_en_en' => 'english name',
'store_code_at_de' => 'german name',
'store_code_fr_fr' => 'franch name'
)
);
$storeLabels = array();
foreach ($attribute['translations'] as $storeViewCode => $text) {
$storeViewId = Mage::getModel('core/store')->load($storeViewCode, 'code')->getId();
$storeLabels[$storeViewId] = $text;
}
$attributeId = $installer->getAttributeId($attribute['entity_type'], $attribute['code']);
Mage::getSingleton('eav/config')->getAttribute($attribute['entity_type'], $attributeId)
->setData('store_labels', $storeLabels)
->save();
$installer->endSetup();
I found the more simpler solution to my question...
$attributeId = Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product', 'ATTRIBUTE_CODE');
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attribute->setFrontendLabel($displayName)->save();
setFrontendLabel(string $str) is the function which best suits here.

How to display customer email in orders.csv

can someone help me? how can I add a new field in orders.csv file in Mangento? for example i want to have in csv file email and phone.
please click the link below to understand which export is talking about
http://i.stack.imgur.com/fBAHG.png
Thank you
You have to customize the Grid block : /app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php
So You can do like this. If you already enabled the local module functionality, Please copy and paste it to this path /app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php
And Open that new file ( pasted ), check this method _prepareColumns().
In this method, you have to add those attributes which you are gonna to export in order.csv.
Check the following:
....
$this->addColumn('customer_email', array(
'header' => Mage::helper('sales')->__('Email'),
'index' => 'customer_email',
));
....
After this, refresh the magento cache.
Please check the following code:
In Grid.php
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()
->join(
'customer_entity',
'main_table.customer_id = customer_entity.entity_id', array('customer_email' => 'email')
);
$this->setCollection($collection);
return parent::_prepareCollection();
}
Then in protected function _prepareColumns() function add following code:
$this->addColumn('customer_email', array(
'header' => Mage::helper('sales')->__('Email'),
'index' => 'customer_email',
));
After this, you must refresh the cache or login again.

Magento - Get Transactional Email Variables

I was just reading over
http://www.magentocommerce.com/wiki/modules_reference/english/mage_adminhtml/system_email_template/index#email_variables
Anyone know how I can find these variables on my own? The wiki page says they may not actually work, and the variable I am trying to work with is {{var shippingMethod}}, however, it is returning blank.
I am wondering, one if that is the correct variable name, and two, if that is the correct variable name, what function do I need to add to my custom shipping method?
Thank you!
Jeff
Here's the piece of code that migiht help You:
public function sendMail() {
$mailTemplate = Mage::getModel('core/email_template');
$template = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE); // template id of email template
$email = Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT); // recipient example: example#yahoo.com
$mailTemplate->setDesignConfig(array('area'=>'frontend'))
->sendTransactional(
$template,
Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY),
$email,
null,
array(
'subject' => $this->getSubject(),
'line' => $this->getLine(),
'message' => $this->getMessage()
)
);
$translate->setTranslateInline(true);
return $this;
}
the array of
array(
'subject' => $this->getSubject(),
'line' => $this->getLine(),
'message' => $this->getMessage()
)
can be get by {{var subject}} {{var line}} {{var message}}
Then how to get shippingMethod ??
You must getModel of shipping method and load the shipping method first and then set it to the array.

Resources