Jump to content

Add Custom Fields To Mail Alert New Ordee Email.


babyewok

Recommended Posts

In mailalerts/mailalerts.php

    public function hookNewOrder($params)
    {
       

Need to check whether $params contains the new custom registration fields. If so you can use them. If not, need to find a way for retrieving the custom registration fields based on userid or username or email etc.

Edited by chrisranjana.com (see edit history)
Link to comment
Share on other sites

  • 1 year later...

I know that this is a pretty old post, but I just had the same problem, wanting information from the Custom Field module. Here's the solution:

 

The database table is called 'ps_customer_custom_field_value'. Here you have a cell called 'id_custom_field' and a cell called 'value'. If you want to know which field is which, look into 'ps_custom_field_lang' to get the 'id_custom_field'.

 

You'll have to edit /modules/mailalerts/mailalerts.php.

 

Look for the function

public function getAllMessages($id)

and below this add the following new function:

public function getCustomField($id)
  {
    $custom_fields = Db::getInstance()->executeS('
      SELECT `value`
      FROM `'._DB_PREFIX_.'customer_custom_field_value`
      WHERE `id_customer` = '.(int)$id.'
      AND `id_custom_field` = 2'); // Set the number corresponding the field you want to get the value from
    foreach ($custom_fields as $custom_field)
      $result = $custom_field['value'];

    return $result;
  }

Now scroll down to:

$message = $this->getAllMessages($order->id);

  if (!$message || empty($message))
    $message = $this->l('No message');

and below you'll type

$yourCustomField = $this->getCustomField($customer->id);

Scroll down to the section

// Filling-in vars for email
  $template_vars = array(

and find

'{message}' => $message

here you change the message and add the line with your custom variable

'{message}' => $message,
'{customField}' => $yourCustomField

Further down in the file, you have a similar section again. Replace the message line one again with the above.

 

Now you have to edit the e-mail template. You'll find it in BO > Localization > Translations. In 'Type of translation' choose 'Email templates translations', your theme and language.

 

Find the 'mailalerts' section and 'new_order' template. Edit the template and add '{customField}' whereever you want.

 

Hope it helps!

 

/Carl

  • Like 1
Link to comment
Share on other sites

  • 3 months later...

I know that this is a pretty old post, but I just had the same problem, wanting information from the Custom Field module. Here's the solution:

 

The database table is called 'ps_customer_custom_field_value'. Here you have a cell called 'id_custom_field' and a cell called 'value'. If you want to know which field is which, look into 'ps_custom_field_lang' to get the 'id_custom_field'.

 

You'll have to edit /modules/mailalerts/mailalerts.php.

 

Look for the function

public function getAllMessages($id)

and below this add the following new function:

public function getCustomField($id)
  {
    $custom_fields = Db::getInstance()->executeS('
      SELECT `value`
      FROM `'._DB_PREFIX_.'customer_custom_field_value`
      WHERE `id_customer` = '.(int)$id.'
      AND `id_custom_field` = 2'); // Set the number corresponding the field you want to get the value from
    foreach ($custom_fields as $custom_field)
      $result = $custom_field['value'];

    return $result;
  }

Now scroll down to:

$message = $this->getAllMessages($order->id);

  if (!$message || empty($message))
    $message = $this->l('No message');

and below you'll type

$yourCustomField = $this->getCustomField($customer->id);

Scroll down to the section

// Filling-in vars for email
  $template_vars = array(

and find

'{message}' => $message

here you change the message and add the line with your custom variable

'{message}' => $message,
'{customField}' => $yourCustomField

Further down in the file, you have a similar section again. Replace the message line one again with the above.

 

Now you have to edit the e-mail template. You'll find it in BO > Localization > Translations. In 'Type of translation' choose 'Email templates translations', your theme and language.

 

Find the 'mailalerts' section and 'new_order' template. Edit the template and add '{customField}' whereever you want.

 

Hope it helps!

 

/Carl

i know this is something old but...

 

Man, thanks you, this work i make 4 function ( getCustomField1, getCustomField2,getCustomField3,getCustomField4 ) and this work so well. very very thanks you.

 

now i have a question, i make a list with custom field, and try to select and send by mail this show

Region: a:1:{i:0;s:1:"2";}   the  " a:1:{i:0;s:1:"2";} "  is the option i choice and doesnt show his name only this code.

 

if i make a filter like this on fuction maybe this work?

 

Select the result = Compare with 15 codes like " a:1:{i:0;s:1:"2"; " and send a name

 

if 
 
 
a:1:{i:0;s:1:"2";} = "Región de Tarapacá"
a:1:{i:0;s:1:"9";} = "Región del Bio Bio"
 
i explain my self? sorry for bad english
Link to comment
Share on other sites

 

i know this is something old but...

 

Man, thanks you, this work i make 4 function ( getCustomField1, getCustomField2,getCustomField3,getCustomField4 ) and this work so well. very very thanks you.

 

now i have a question, i make a list with custom field, and try to select and send by mail this show

Region: a:1:{i:0;s:1:"2";}   the  " a:1:{i:0;s:1:"2";} "  is the option i choice and doesnt show his name only this code.

 

if i make a filter like this on fuction maybe this work?

 

Select the result = Compare with 15 codes like " a:1:{i:0;s:1:"2"; " and send a name

 

if 
 
 
a:1:{i:0;s:1:"2";} = "Región de Tarapacá"
a:1:{i:0;s:1:"9";} = "Región del Bio Bio"
 
i explain my self? sorry for bad english

 

 

 

OOOK, i fixed my self and this work. I hope it works for someone

/* CustomField ID 5 = Region */
	public function getCustomField5($id)
  {
    $custom_fields5 = Db::getInstance()->executeS('
      SELECT `value`
      FROM `'._DB_PREFIX_.'customer_custom_field_value`
      WHERE `id_customer` = '.(int)$id.'
      AND `id_custom_field` = 5'); // Set the number corresponding the field you want to get the value from
    foreach ($custom_fields5 as $custom_field5)
      $results = $custom_field5['value'];
		if($results == 'a:1:{i:0;s:1:"1";}')
		  {
			  $result = 'Region de Arica y Parinacota';
			return $result;
		  }
		if($results == 'a:1:{i:0;s:1:"2";}')
		  {
			  $result = 'Region de Arica y Parinacota';
			return $result;
		  }
		if($results == 'a:1:{i:0;s:1:"3";}')
		  {
			  $result = 'Region de Antofagasta';
			return $result;
		  }
		if($results == 'a:1:{i:0;s:1:"4";}')
		  {
			  $result = 'Region de Atacama';
			return $result;
		  }
		if($results == 'a:1:{i:0;s:1:"5";}')
		  {
			  $result = 'Region de Coquimbo';
			return $result;
		  }
		if($results == 'a:1:{i:0;s:1:"6";}')
		  {
			  $result = 'Region de Valparaiso';
			return $result;
		  }
		if($results == 'a:1:{i:0;s:1:"7";}')
		  {
			  $result = 'Region del Libertador General Bernardo O`Higgins';
			return $result;
		  }
		if($results == 'a:1:{i:0;s:1:"8";}')
		  {
			  $result = 'Region del Maule';
			return $result;
		  }
		if($results == 'a:1:{i:0;s:1:"9";}')
		  {
			  $result = 'Region del Bío Bío';
			return $result;
		  }
	  if($results == 'a:1:{i:0;s:2:"10";}')
		  {
			  $result = 'Region de la Araucania';
			return $result;
		  }
	  if($results == 'a:1:{i:0;s:2:"11";}')
		  {
			  $result = 'Region de los Rios';
			return $result;
		  }
	  if($results == 'a:1:{i:0;s:2:"12";}')
		  {
			  $result = 'Region de los Lagos';
			return $result;
		  }
	  if($results == 'a:1:{i:0;s:2:"13";}')
		  {
			  $result = 'Region Aysen del General Carlos Ibañez del Campo';
			return $result;
		  }
	  if($results == 'a:1:{i:0;s:2:"14";}')
		  {
			  $result = 'Region de Magallanes y de la Antartica Chilena';
			return $result;
		  }
	  if($results == 'a:1:{i:0;s:2:"15";}')
		  {
			  $result = 'Region Metropolitana';
			return $result;
		  }
 
    return $results;
  }

 d2c6a31ec442437bb960b6ad3e6cf86f.png

Old NewOrder Mail

a47121e3aa864e0d859161f3cb432377.png

 

New NewOrder Mail:

 

e680be817e9c4dcdbc04eff721384551.png

Edited by xmathiasdf (see edit history)
Link to comment
Share on other sites

  • 3 years later...

Hi, I'm in a similar situation here, would appreciate some help:

I'm on PS 1.7.6.4. My tables at sql database are named ps_custom_field_userdata which stores id_order value and field_value with the employe seller number, then I have another table called ps_custom_field_values which stores field_value_id (same as field_value from previous table) and field_value with employe name.

I've tried editing /modules/ps_emailalerts/ps_emailalerts.php as in your answers in this thread adding:

before "hookActionValidateOrder" function:

public function getCustomField($id)
{
$idvendedor = Db::getInstance()->executeS('
SELECT `field_value`
FROM `'._DB_PREFIX_.'custom_field_userdata`
WHERE `id_order` = '.(int)$order->id.'
AND `id_custom_field` = 4'); // Set the number corresponding the field you want to get the value from

$custom_fields = Db::getInstance()->executeS('
SELECT `field_value`
FROM `'._DB_PREFIX_.'ps_custom_field_values`
WHERE `field_value_id` = '.$idvendedor);

foreach ($custom_fields as $custom_field)
$result = $custom_field['field_value'];

return $result;
}

before "$message = $this->getAllMessages($order->id);":

$yourCustomField = $this->getCustomField($customer->id);

then added in both "$template_vars = array(" below "$message = $this->getAllMessages($order->id);":

'{customField}' => $yourCustomField

And added '{customField}' to new-order email template of my theme but value is empty at every new order.

 

Link to comment
Share on other sites

  • 1 year later...
On 12/16/2016 at 2:48 PM, carlroger said:

I know that this is a pretty old post, but I just had the same problem, wanting information from the Custom Field module. Here's the solution:

 

The database table is called 'ps_customer_custom_field_value'. Here you have a cell called 'id_custom_field' and a cell called 'value'. If you want to know which field is which, look into 'ps_custom_field_lang' to get the 'id_custom_field'.

 

You'll have to edit /modules/mailalerts/mailalerts.php.

 

Look for the function

public function getAllMessages($id)

and below this add the following new function:

public function getCustomField($id)
  {
    $custom_fields = Db::getInstance()->executeS('
      SELECT `value`
      FROM `'._DB_PREFIX_.'customer_custom_field_value`
      WHERE `id_customer` = '.(int)$id.'
      AND `id_custom_field` = 2'); // Set the number corresponding the field you want to get the value from
    foreach ($custom_fields as $custom_field)
      $result = $custom_field['value'];

    return $result;
  }

Now scroll down to:

$message = $this->getAllMessages($order->id);

  if (!$message || empty($message))
    $message = $this->l('No message');

and below you'll type

$yourCustomField = $this->getCustomField($customer->id);

Scroll down to the section

// Filling-in vars for email
  $template_vars = array(

and find

'{message}' => $message

here you change the message and add the line with your custom variable

'{message}' => $message,
'{customField}' => $yourCustomField

Further down in the file, you have a similar section again. Replace the message line one again with the above.

 

Now you have to edit the e-mail template. You'll find it in BO > Localization > Translations. In 'Type of translation' choose 'Email templates translations', your theme and language.

 

Find the 'mailalerts' section and 'new_order' template. Edit the template and add '{customField}' whereever you want.

 

Hope it helps!

 

/Carl

Hello, 

i tried to use the code you posted, but it doesn't work for me. My Prestashop version is 1.6.1.4.

My database table is called 'ps_custom_field_userdata'. Here I have a cell called 'id_custom_field' and a cell called 'field_value'.

What I did was edit /modules/mailalerts/mailalerts.php as follows:

1) I added this feature

    // FIRST ADDITION
     public function getCustomField ($ id)
     {
     $ custom_fields = Db :: getInstance () -> executeS ('
       SELECT `field_value`
       FROM `'._DB_PREFIX _.' custom_field_userdata`
       WHERE `id_order` = '. (Int) $ id.'
       AND `id_custom_field` = 4 '); // number corresponding to the field from which you want to get the value
       // $ result = array ();
       foreach ($ custom_fields as $ custom_field)
           // $ result [] = $ custom_field ['field_value'];
           $ result = $ custom_field ['field_value'];

     // return implode ($ result);
       return $ result;
     }

     under the function public function getAllMessages ($ id)

2) I added

     $ yourCustomField = $ this-> getCustomField ($ order-> id);

     under the strings

     $ message = $ this-> getAllMessages ($ order-> id);

     if (! $ message || empty ($ message))
     $ message = $ this-> l ('No message');

3) I added the line with the custom variable

     '{customField}' => $ yourCustomField

     under the variable

     '{message}' => $ message

4) I modified new_order.tpl present in the module emails, adding

     '{customField}'

Unfortunately in the email the field is empty, the data entered in the front-end does not appear.

Could you help me solve the problem? I also ask you why if I substitute the query

WHERE `id_order` = '. (Int) $ id.' present in the function created with

WHERE `id_order` = 552 (where 552 is the ID of an old order) in the email appears the value?

Please help me, where am I wrong? I hope I was clear in the exposition.

Thank you

Link to comment
Share on other sites

10 minutes ago, ventura said:

First make sure that the query is getting a result. 

If you are getting more than one result, it will be necessary to contain it because it is not possible to add an array.

 

Hello,

thanks for the answer, the query works but i am getting a result only if i use the ID of an old order, as in the code

WHERE `id_order` = 552 

With this code, it appear in mail alert order email custom fields i want, but with this

WHERE `id_order` = '. (Int) $ id.'

fields don't appear.

I don't understand what am I doing wrong, if u can help me i am glad.

Thanks 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...