Jump to content

[SOLVED] Add Newsletter Opt-in Checkbox on Checkout


Arkadia

Recommended Posts

Hi everyone,

 

I was wondering if anyone could help or give me a push in the right direction...

 

I need to add a "Receive our newsletters" checkbox (opt in for newsletters) somewhere in the 5 step checkout process. It should be unchecked by default...and then if the user checks it, it should subscribe them to the newsletter as they checkout.

 

I have no idea where to begin with this yet.

 

Any ideas?

 

Thank you! :)

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

Okay I resolved this!

 

For anyone else that may be interested...

 

Edit controllers/OrderController.php

 

Find the following function under /*payment step*/ and add this code...

 

/* Payment step */


protected function _assignPayment()
{
 //Start php to add a newsletter opt-in
 $emailkd = self::$cookie->email;  

 if (isset($_POST['newsletterkd'])){
  $newssub = 'checked';
 } else {
  $newssub = 'not checked';
 }

 if ($newssub == 'checked'){

  if (Db::getInstance()->getRow('SELECT `email` FROM '._DB_PREFIX_.'newsletter WHERE `email` = \''.pSQL($emailkd).'\'')){
  //user is in the ps_newletter db - so do nothing...
  $checkdb = 'in database';
 } else {
  //add customer to ps_newletter
  $checkdb = 'not in db';
if (!Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'newsletter (email, newsletter_date_add, ip_registration_newsletter, http_referer) VALUES (\''.$emailkd.'\', NOW(), \''.pSQL(Tools::getRemoteAddr()).'\', NULL )')){
 return $this->error = $this->l('Error during subscription');
}	
 }

 }  
 //END newsletter optin edit

 global $orderTotal;
 // Redirect instead of displaying payment modules if any module are grefted on
 Hook::backBeforePayment('order.php?step=3');
 /* We may need to display an order summary */
 self::$smarty->assign(self::$cart->getSummaryDetails());
 self::$smarty->assign(array(
  'total_price' => (float)($orderTotal),  
  'taxes_enabled' => (int)(Configuration::get('PS_TAX'))
 ));
 self::$cookie->checkedTOS = '1';
 parent::_assignPayment();
}

 

Then in your tpl file where you'd like to show the checkbox in the checkout area...add the following code:

 

(In my case I chose to use theme/order-carrier.tpl)

 

<!-- Added this to add Newsletter Subscription checkbox in checkout-->
<br />
<h3 class="carrier_title">{l s='Would you like to sign up for our Newsletter?'}</h3>
<p class="checkbox">
 <input type="checkbox" name="newsletterkd" id="newsletterkd" value="1" />
 <label for="newsletterkd">{l s='Yes, sign me up!'}</label>
</p>
<!--END Newsletter Subscription checkbox-->

 

Note: I am using prestashop 1.4.5.1 with Newsletter block v1.4 by PrestaShop

 

This should work in similar way in the newer prestashop versions but I've not tested it with any other prestashop version...so adjust accordingly...it's simply writing the customers email address and other details required (namely: the date stamp, ip address, and http referrer) to the ps_newsletter table in the database. It also checks if the user was already registered or not so you don't get double entries.

 

Feel free to improve my code! :rolleyes:

 

Well hope that's useful to someone out there. :)

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

  • 10 months later...

Okay I resolved this!

 

For anyone else that may be interested...

 

Edit controllers/OrderController.php

 

Find the following function under /*payment step*/ and add this code...

 

/* Payment step */


protected function _assignPayment()
{
  //Start php to add a newsletter opt-in
  $emailkd = self::$cookie->email;  

  if (isset($_POST['newsletterkd'])){
   $newssub = 'checked';
  } else {
   $newssub = 'not checked';
  }

  if ($newssub == 'checked'){
  
   if (Db::getInstance()->getRow('SELECT `email` FROM '._DB_PREFIX_.'newsletter WHERE `email` = \''.pSQL($emailkd).'\'')){
	  //user is in the ps_newletter db - so do nothing...
	  $checkdb = 'in database';
	 } else {
	  //add customer to ps_newletter
	  $checkdb = 'not in db';
	if (!Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'newsletter (email, newsletter_date_add, ip_registration_newsletter, http_referer) VALUES (\''.$emailkd.'\', NOW(), \''.pSQL(Tools::getRemoteAddr()).'\', NULL )')){
	 return $this->error = $this->l('Error during subscription');
	}	
	 }
  
  }  
  //END newsletter optin edit
		
  global $orderTotal;
  // Redirect instead of displaying payment modules if any module are grefted on
  Hook::backBeforePayment('order.php?step=3');
  /* We may need to display an order summary */
  self::$smarty->assign(self::$cart->getSummaryDetails());
  self::$smarty->assign(array(
   'total_price' => (float)($orderTotal),  
   'taxes_enabled' => (int)(Configuration::get('PS_TAX'))
  ));
  self::$cookie->checkedTOS = '1';
  parent::_assignPayment();
}
Then in your tpl file where you'd like to show the checkbox in the checkout area...add the following code:

 

(In my case I chose to use theme/order-carrier.tpl)

 

<!-- Added this to add Newsletter Subscription checkbox in checkout-->
<br />
<h3 class="carrier_title">{l s='Would you like to sign up for our Newsletter?'}</h3>
<p class="checkbox">
  <input type="checkbox" name="newsletterkd" id="newsletterkd" value="1" />
  <label for="newsletterkd">{l s='Yes, sign me up!'}</label>
</p>
<!--END Newsletter Subscription checkbox-->
Note: I am using prestashop 1.4.5.1 with Newsletter block v1.4 by PrestaShop

 

This should work in similar way in the newer prestashop versions but I've not tested it with any other prestashop version...so adjust accordingly...it's simply writing the customers email address and other details required (namely: the date stamp, ip address, and http referrer) to the ps_newsletter table in the database. It also checks if the user was already registered or not so you don't get double entries.

 

Feel free to improve my code! :rolleyes:

 

Well hope that's useful to someone out there. :)

 

 

Does it work for 1.5 version?

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