Jump to content

Newsletter block in homehook


Recommended Posts

I do not know who will care of my forum post.

 

I have googled several times for hooking newsletter block in home. Most of the articles says to modify into core modules "Newsletter Block". I know this is not good idea to me because I am theme developer while we export our theme it does not export any core module of prestashop. So, we do duplicate of "Newsletter Block" . Ex: "DevilNewletter Block". in this module we made hook into home. However it hooks into home, but pragmatically it loose the functionality.

 

Tiny Code from devilnewsletter.pnp. we have modified on install method and hookHome only.

 

public function install()
 {
if (parent::install() == false  OR  $this->registerHook('foote') == false OR  $this->registerHook('header') == false)
 return false;
return Db::getInstance()->Execute('
 CREATE TABLE IF NOT EXISTS '._DB_PREFIX_.'newsletter (
  `id` int(6) NOT NULL AUTO_INCREMENT,
  `email` varchar(255) NOT NULL,
  `newsletter_date_add` rDATETIME NULL,
  `ip_registration_newsletter` varchar(15) NOT NULL,
  `http_referer` VARCHAR(255) NULL,
  PRIMARY KEY(`id`)
 ) ENGINE='._MYSQL_ENGINE_.' default CHARSET=utf8');
 }

function hookHome($params)
{
 return $this->hookLeftColumn($params);
}

 

When I see the stat of newsletter from back office it makes me to install "Newsletter Block" and then I can see the stats of newsletter subscriber but our front office "DevilNewsletter" always shows message "E-mail address already registered" even we have put news email but it fills data into database dramatically. This newsletter module is really killing me slowly . Currently I have activate both newsletter Newsletter Block(Prestashop Module) and DevilNewsletter(My Module). Please find our newsletter module from attachment.

 

 

That would be great if we have core module override concept from theme folder. However it only overide .tpl files only. But for hooking into different is not possible.

Link to comment
Share on other sites

  • 5 months later...
  • 1 year later...

Got the same problem :( It is a weird bug. Tried a few fixes but i don't seem to get it. Debugging modules is also a hell in Prestashop! (have disabled cache, but still need to press 5 times clear cache in backend before changes show up...)

 

The problem is situated in this function, i'll need more time to fully debug it... i personally think it has something to do with the $registered variable. But not sure about that ;) I'll keep you guys posted. First of all i need to finish some Laravel stuff for other projects :)

	/**
	 * Check if this mail is registered for newsletters
	 *
	 * @param unknown_type $customerEmail
	 * @return int -1 = not a customer and not registered
	 * 				0 = customer not registered
	 * 				1 = registered in block
	 * 				2 = registered in customer
	 */
	private function isNewsletterRegistered($customerEmail)
	{

		$sql = 'SELECT `email`
				FROM '._DB_PREFIX_.'newsletter
				WHERE `email` = \''.pSQL($customerEmail).'\'
				AND id_shop = '.$this->context->shop->id;
		if (Db::getInstance()->getRow($sql)){
			return 1;
		}

		$sql = 'SELECT `newsletter`
				FROM '._DB_PREFIX_.'customer
				WHERE `email` = \''.pSQL($customerEmail).'\'
				AND id_shop = '.$this->context->shop->id;
		if (!$registered = Db::getInstance()->getRow($sql)){
			
			return -1;
		}
		
		if ($registered['newsletter'] == '1'){
			return 2;
		}

		return 0;
	}
Link to comment
Share on other sites

I have found the problem to your bug. This is age old bug. Its amazing this bug escaped prestashop developers eyes. Anyways in programming terms equal to is == (double equal to) where = (is assignment).

 

So search for this line :

if (!$registered = Db::getInstance()->getRow($sql))
    return self::GUEST_NOT_REGISTERED;

replace the above code code with :

if (!$registered == Db::getInstance()->getRow($sql))
    return self::GUEST_NOT_REGISTERED;

notice the double equal to. This should fix your bug.

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

Hi Stephen,

 

Its not just in one place. There are about 7 places where this mistake is repeated.

 

I have written about this here : http://blog.prestastrap.com/block-newsletter-module-returns-this-email-address-is-already-registered-error/

 

You can take a look and use the file I uploaded.

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

Guys, I jumped the gun saying the prestashop developers had made the most basic mistake in programming. But its my mistake I failed to look into the code thoroughly. I did look into it and I found out that those assignment operators were intentional.


 


Ok so what was causing the problem. Quick answer : The Hooks. i.e. if the module is hooked to more than one place then this error will occur.


 


Long Answer :


Lets assume you have hooked block newsletter to Left Column and to Footer or Home. You hide the left column in the home page (note : you are only hiding the left column. This doesn’t mean the left column hook is not executed) and just display the newsletter block in the home page. When you submit the email. This is what happens.


*  First the left column hook is executed, which will take your email and save it to the database.


Next footer (or home) hook is executed. So what happens now while executing the footer hook (or home) is that the module tries to save the email again. Since we had already saved the email while executing the left column hook. We get “This email address is already registered” error.


 


What is the fix ?


We’ll have to make sure that the Email save process is called only when its corresponding hook is called and should do nothing when other hooks are executed. That is when you click on submit from the form in the footer, the save process should be called only in hookFooter and not during executing of other hooks.


I have implemented the fix here : blocknewsletter.zip


 


Once again, I am sorry for my carelessness.


 


Note : If your theme overrides blocknewsletter module then make sure you copy the blocknewsletter.tpl file to themes/<theme-name>/modules/blocknewsletter/


 


Please let me know if you have any problems.


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

×
×
  • Create New...