Jump to content

Send email to the products Manufacturer or Supplier only, not all merchants


AriT

Recommended Posts

Hi, I'm a complete newbie to PrestaShop and OOP and would appreciate some help.

 

I am trying to write a override to the mailalerts module to be able to send a notification email to either the supplier or the manufacturer of the product which is ordered.

I know you can add a merchant email but the problem is that we will have many different manufacturers and suppliers from which the customer can order directly and I don't want to send everyone the email only the specific Supplier/manufacturer.

 

So far I made overrides for MailAlerts.php and mailalerts.php in a mailalerts folder, deleted cache and it all works fine.

 

In the MailAlert.php override I'm trying to use the product-id to first get either the supplier_id or the manufacturer_id and then use that to go to address database to get the correct email (I made a custom field for that and that all works fine)

 

First problem is here....... how do I echo out the variables I created here to see if that it actually makes the call to the database and get the right values?

I have tried :

 $Mail = new Mail ();

$Mail->manufacturer_mails;
$Mail->supplier_mails;
---- nothing -because it needs the product_id, I think?
 
also tried echo'ing from within the class and even tried making a function but I'm not sure how to call that function so ....still... nothing.
 
 
Second problem:
I then went and modified alerts.php and basically copied everything that was done to merchants_mails to try and get it to work here...mainly because I have very little idea of how this is supposed to be....
 
I did manage to echo out the variables from the __construct functions and it does give me the merchant_mails but my $manufacturer_mails and $supplier_mails are empty.
 
 
At the moment or before I started messing everything up the code would run but gives me a:
Fatal error: Undefined class constant 'TYPE_TEXT' in /blah blah/site/classes/PaymentModule.php on line 506
 
which is..
 
if (count($product_var_tpl_list) > 0) {
                        $product_list_txt = $this->getEmailTemplateContent('order_conf_product_list.txt', Mail::TYPE_TEXT, $product_var_tpl_list);
                        $product_list_html = $this->getEmailTemplateContent('order_conf_product_list.tpl', Mail::TYPE_HTML, $product_var_tpl_list);
                    }
 
 
Sorry the code is very messy as I've tried about a hundred different ways to get the values......
 
I would greatly apprecciate any help!
Thank you
Ari
 
Link to comment
Share on other sites

  • 4 months later...
  • 2 months later...

Hey Agilulfo,

 

IM about to write a module for this. I wish we coulde do this with a few people as a open source project, because im new to prestashop module development.

Electrostuff hi, it's not a bad idea, but the module I need to make it's a bit different. The owner of the e-commerce I'm making is selling T-shirts of varius artists, and he want to automatically send an e-mail to the corrisponding artist every time on of his deisgn is bought. I also have very short times to do this module, he needs it in maximum 1 month!

Link to comment
Share on other sites

  • 4 weeks later...

Hello to all again,

Right now I'm editing mailalerts.php to send the mail also to the manufacturer of the product.

I found the function that is sending e-mails (for exemple public function hookActionValidateOrder($params) ).

Right now  I can send a mail to a certain manufacture e-mail if I staticaly define my variable, like this:

$manufacturer_mail="[email protected]";
	
	Mail::Send(
	$mail_id_lang,
	'new_order',
	sprintf(Mail::l('New order : #%d - %s', $mail_id_lang), $order->id, $order->reference),
	$template_vars,
	$manufacturer_mail,
	null,
	$configuration['PS_SHOP_EMAIL'],
	$configuration['PS_SHOP_NAME'],
	null,
	null,
	$dir_mail,
	null,
	$id_shop
	);

My problem is that I don't know how to take the product_id and use it to firstly take the manufacturer_id associated with that product and finaly the manufacturer_mail (I insert the mail in the "name" field in db instead of creating a custom field).

 

I was thinking to using something like this to take the manufacturer mail, but I need the $id_product...

$id_manufacturer= Db::getInstance()->executeS('SELECT `id_manufacturer` FROM `'._DB_PREFIX_.'product` WHERE `id_product` = '.(int)$id_product);
$manufacturer_mail=Db::getInstance()->executeS('SELECT `name` FROM `nnw5_manufacturer` WHERE `id_manufacturer` = '.(int)$id_manufacturer);

Do you have some ideas on how to do it?

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

Hello to all again,

Right now I'm editing mailalerts.php to send the mail also to the manufacturer of the product.

I found the function that is sending e-mails (for exemple public function hookActionValidateOrder($params) ).

Right now  I can send a mail to a certain manufacture e-mail if I staticaly define my variable, like this:

$manufacturer_mail="[email protected]";
	
	Mail::Send(
	$mail_id_lang,
	'new_order',
	sprintf(Mail::l('New order : #%d - %s', $mail_id_lang), $order->id, $order->reference),
	$template_vars,
	$manufacturer_mail,
	null,
	$configuration['PS_SHOP_EMAIL'],
	$configuration['PS_SHOP_NAME'],
	null,
	null,
	$dir_mail,
	null,
	$id_shop
	);

My problem is that I don't know how to take the product_id and use it to firstly take the manufacturer_id associated with that product and finaly the manufacturer_mail (I insert the mail in the "name" field in db instead of creating a custom field).

 

I was thinking to using something like this to take the manufacturer mail, but I need the $id_product...

$id_manufacturer= Db::getInstance()->executeS('SELECT `id_manufacturer` FROM `'._DB_PREFIX_.'product` WHERE `id_product` = '.(int)$id_product);
$manufacturer_mail=Db::getInstance()->executeS('SELECT `name` FROM `nnw5_manufacturer` WHERE `id_manufacturer` = '.(int)$id_manufacturer);

Do you have some ideas on how to do it?

public $id_product line 33 mailalerts.php

Link to comment
Share on other sites

I tried but it doesn't work. Maybe the query's for getting manufacturer Id and mail are not working.
The fact is that is hard to debug this piece of code, do you have some advise?

 

Edit:

If I do like this

$id_product=25;
$id_manufacturer= Db::getInstance()->getValue('SELECT `id_manufacturer` FROM `'._DB_PREFIX_.'product` WHERE `id_product` = '.(int)$id_product);
$manufacturer_mail=Db::getInstance()->getValue('SELECT `name` FROM `nnw5_manufacturer` WHERE `id_manufacturer` = '.(int)$id_manufacturer);

everything is going fine, it get the manufacturer_mail and send the mail, so the only thing is getting the right id_product.

 

Edit 2:

I managed to obtain the product id by using the foreach ($products as $key => $product) cycle in the function and writing this inside:
$id_product=$product['product_id'];

 

Edit 3:

Ok the bug with the solution written before was that it sanded the mail only at one manufacturer becouse $id_product took the last record in the cycle. In fact, for example, of the custumer bought 2 product with different manufacturers it sanded only at the last manufacturer. 
To prevent this I could create an array of product_id then an array of manufacturer mail or do another cycle. I did the second:



		foreach ($products as $key => $product)
		{


				$id_product=$product['product_id'];
				$id_manufacturer= Db::getInstance()->getValue('SELECT `id_manufacturer` FROM `'._DB_PREFIX_.'product` WHERE `id_product` = '.(int)$id_product);
				$manufacturer_mail=Db::getInstance()->getValue('SELECT `name` FROM `nnw5_manufacturer` WHERE `id_manufacturer` = '.(int)$id_manufacturer);



				Mail::Send(
				$mail_id_lang,
				'new_order',
				sprintf(Mail::l('New order : #%d - %s', $mail_id_lang), $order->id, $order->reference),
				$template_vars,
				$manufacturer_mail,
				null,
				$configuration['PS_SHOP_EMAIL'],
				$configuration['PS_SHOP_NAME'],
				null,
				null,
				$dir_mail,
				null,
				$id_shop
				);

}
 

So with the solution wrote here (in public function hookActionValidateOrder() line 245) the module MailAlerts will send a mail also to the manufacturer (but is the same with suppliars) of the product.

Now the new problem is that it sends all the "new order" mail to them, so like in the example of before, if Ithe client bought 2 products with different manufacturer both of them will recieve both the products in the email (sort of privacy problem). I think I have to modify the $template_vars to fix this.

 

 

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

I tried but it doesn't work. Maybe the query's for getting manufacturer Id and mail are not working.

The fact is that is hard to debug this piece of code, do you have some advise?

 

Edit:

If I do like this

$id_product=25;
$id_manufacturer= Db::getInstance()->getValue('SELECT `id_manufacturer` FROM `'._DB_PREFIX_.'product` WHERE `id_product` = '.(int)$id_product);
$manufacturer_mail=Db::getInstance()->getValue('SELECT `name` FROM `nnw5_manufacturer` WHERE `id_manufacturer` = '.(int)$id_manufacturer);

everything is going fine, it get the manufacturer_mail and send the mail, so the only thing is getting the right id_product.

 

Edit 2:

I managed to obtain the product id by using the foreach ($products as $key => $product) cycle in the function and writing this inside:

$id_product=$product['product_id'];

 

Edit 3:

Ok the bug with the solution written before was that it sanded the mail only at one manufacturer becouse $id_product took the last record in the cycle. In fact, for example, of the custumer bought 2 product with different manufacturers it sanded only at the last manufacturer. 

To prevent this I could create an array of product_id then an array of manufacturer mail or do another cycle. I did the second:



		foreach ($products as $key => $product)
		{


				$id_product=$product['product_id'];
				$id_manufacturer= Db::getInstance()->getValue('SELECT `id_manufacturer` FROM `'._DB_PREFIX_.'product` WHERE `id_product` = '.(int)$id_product);
				$manufacturer_mail=Db::getInstance()->getValue('SELECT `name` FROM `nnw5_manufacturer` WHERE `id_manufacturer` = '.(int)$id_manufacturer);



				Mail::Send(
				$mail_id_lang,
				'new_order',
				sprintf(Mail::l('New order : #%d - %s', $mail_id_lang), $order->id, $order->reference),
				$template_vars,
				$manufacturer_mail,
				null,
				$configuration['PS_SHOP_EMAIL'],
				$configuration['PS_SHOP_NAME'],
				null,
				null,
				$dir_mail,
				null,
				$id_shop
				);

}
 

So with the solution wrote here (in public function hookActionValidateOrder() line 245) the module MailAlerts will send a mail also to the manufacturer (but is the same with suppliars) of the product.

 

Now the new problem is that it sends all the "new order" mail to them, so like in the example of before, if Ithe client bought 2 products with different manufacturer both of them will recieve both the products in the email (sort of privacy problem). I think I have to modify the $template_vars to fix this.

Wow, first of all: Good job man!

 

Well, to be honest, it would be much better if we write a new module for this tho. We can catch the "on status change" - Function to send a new Mail with all our needings. 

 

I tried to code something outside the prestashop coding standards for a clean start. If you want we can hook up in TeamSpeak and I can share my code with you for suggestions/inspirations.

 

Coding a new module would not take as much time while we already got the statements we need. 

 

In my code I tried to catch the case if 2 or more different Suppliers are present. We can provide this as a free module for the community.

Link to comment
Share on other sites

  • 1 month later...

First of all i want to thank you guys!
 
But there is a problem with the facturers email. 
In Your line

$manufacturer_mail=Db::getInstance()->getValue('SELECT `name` FROM `nnw5_manufacturer` WHERE `id_manufacturer` = '.(int)$id_manufacturer); 

I get an error: 

 

Table 'myDBTable.nnw5_manufacturer' doesn't exist.

 

Did they change where the Email is stored or did I do anything wrong ?

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

First of all i want to thank you guys!

 

But there is a problem with the facturers email. 

In Your line

$manufacturer_mail=Db::getInstance()->getValue('SELECT `name` FROM `nnw5_manufacturer` WHERE `id_manufacturer` = '.(int)$id_manufacturer); 

I get an error: 

 

Table 'myDBTable.nnw5_manufacturer' doesn't exist.

 

Did they change where the Email is stored or did I do anything wrong ?

 

"nw5" is just a random prefix that prestashop choose during installation. Just enter in phpmyadmin and look wich one is yours.

  • Like 1
Link to comment
Share on other sites

You can add instances for products, manufacturer and address variables

$productObject = new Product((int)$product['product_id'], (int)$id_lang);
$manufacturerObject = new Manufacturer((int)$product->id_manufacturer, (int)$id_lang);
$manufacturerAddress = new Address((int)$manufacturerObject->id_address);
$manufacturerEmail = trim($manufacturerAddress->other);

There is no specific field for manufacturer´s email you can use the other field 

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...