
Newsletter is one of effective marketing tool for a web shop. I am going to cover subscribe and unsubscribe function in this article.
Download Page
Modifying right.php

We need to add ‘subscribe to’ and ‘unsubscribe from’ our newsletter link in the right column.
We add the following after login status div.
system/application/views/shop/right.php
// add this after login status
<div id="newsletter" class="signtop paddingright">
<b><?php
echo lang('subscribe_newsletter')."<br />\n";
echo anchor(base_url()."index.php/".lang('webshop_folder')."/subscribe/", lang('subscribe_subscribe'));
echo "<br />\n";
echo anchor(base_url()."index.php/".lang('webshop_folder')."/unsubscribe/", lang('subscribe_unsubscribe'));
?></b><br />
</div>
CSS
/* newsletter */
#newsletter{
background-color:#E4E0EF;
margin-top: 5px;
}
Language
Let’s add some texts to our language file.
modules/webshop/language/english/webshop_lang.php
// modules/webshop/controllers/subscribe function $lang['subscribe_newsletter'] = 'Newsletter'; $lang['subscribe_subscribe'] = 'Subscribe'; $lang['subscribe_unsubscribe'] = 'Unsubscribe'; $lang['subscribe_name'] = 'Name'; $lang['subscribe_registed_before'] = 'Your email is already in our list.'; $lang['subscribe_thank_for_subscription'] = 'Thanks for subscribing our newsletter!'; $lang['subscribe_you_been_subscribed'] = 'You have been unsubscribed!'; $lang['subscribe_need_login'] = 'You need to login first'; $lang['subscribe_you_been_unsubscribed'] = 'You have been unsubscribed from Newsletter';
subscribe method
In our subscribe method, we are going to use recaptcha as you see the above image. We set a recaptcha as we did in ‘Contact us’ and ‘Registration’ pages.
Setting rules and fields and validate. If validation is FALSE, then outputs errors.
If validation is ok, we use MSubscribers->checkSubscriber function to check if the email exists in our database. This model function returns TRUE or FALSE as you see below.
If it is TRUE, the email exists in our database, so we set a message to tell you are registered and redirect.
Otherwise we create a new subscriber with MSubscribers->createSubscriber function.
We set a message and redirect.
modules/subscribers/models/msubscribers.php
function checkSubscriber($email){
$numrow = 0;
$this->db->select('id');
$this->db->where('email',db_clean($email));
$this->db->limit(1);
$Q = $this->db->get('omc_subscribers');
if ($Q->num_rows() > 0){
$numrow = TRUE;
return $numrow;
}else{
$numrow = FALSE;
return $numrow;
}
}
modules/webshop/controllers/webshop.php
function subscribe(){
$data['title']=lang('webshop_shop_name')." | ".'Subscribe to our News letter';
$captcha_result = '';
$data['cap_img'] = $this->_generate_captcha();
if ($this->input->post('name')){
$rules['name'] = 'required';
$rules['email'] = 'required|valid_email';
$rules['recaptcha_response_field'] = 'trim|required|valid_captcha';
$this->validation->set_rules($rules);
$fields['email'] = lang('webshop_email');
$fields['name'] = lang('subscribe_name');
$fields['recaptcha_response_field'] = 'Recaptcha';
$this->validation->set_fields($fields);
if ($this->validation->run() == FALSE)
{
// if false outputs errors
$this->validation->output_errors();
}
else
{
$email = $this->input->post('email');
// otherwise check if the customer's email is in the database
$numrow = $this->MSubscribers->checkSubscriber($email);
if ($numrow == TRUE){
// you have registered before, set the message and redirect to login page.
flashMsg('info',lang('subscribe_registed_before'));
redirect( lang('webshop_folder').'/subscribe','refresh');
}
$this->MSubscribers->createSubscriber();
flashMsg('success',lang('subscribe_thank_for_subscription'));
redirect( lang('webshop_folder').'/subscribe','refresh');
}
}
$data['page'] = $this->config->item('backendpro_template_shop') . 'subscribe';
$data['module'] = lang('webshop_folder');
$this->load->view($this->_container,$data);
}
view subscribe
<?php print displayStatus();?>
<?php
if ($this->session->flashdata('subscribe_msg')){
echo "<div class='status_box'>";
echo $this->session->flashdata('subscribe_msg');
echo "</div>";
}
?>
<?php echo form_open($this->lang->line('webshop_folder')."/subscribe"); ?>
<h1>
<?php echo form_fieldset('Subscribe To Our Newsletter'); ?>
</h1>
<h5>*Name</h5>
<input type="text" name="name" id="name" value="<?php echo set_value('name'); ?>" size="40" />
<h5>*Email</h5>
<input type="text" name="email" id="email" value="<?php echo set_value('email'); ?>" size="40" />
<h5>*Are you human?</h5>
<?php echo "<p>$cap_img</p>" ;?>
<div><input type="submit" value="Subscribe" /></div>
<?php echo form_fieldset_close(); ?>
<?php echo form_close(); ?>
unsubscribe function

Unsubscribe page has ‘Email’ and Recapcha field to fill in.
If email field is not filled out, it must be the first visit to the page. Set a recapcha and other valiables.
Otherwise a customer must have filled out email field, so set rules and fields.
Run validation.
If the validation is FALSE, outputs errors and redirect to the same page.
If it is ok, remove the subscriber from out database and set a success message and redirect.
function unsubscribe($email=''){
if (!$this->input->post('email')){
$data['title']=lang('webshop_shop_name')." | ".'Unsubscribe our Newsletter';
$captcha_result = '';
$data['cap_img'] = $this->_generate_captcha();
$data['page'] = $this->config->item('backendpro_template_shop') . 'unsubscribe';
$data['module'] = lang('webshop_folder');
$this->load->view($this->_container,$data);
}else{
$rules['email'] = 'trim|required|max_length[254]|valid_email';
$rules['recaptcha_response_field'] = 'trim|required|valid_captcha';
$this->validation->set_rules($rules);
$fields['email'] = lang('webshop_email');
$fields['recaptcha_response_field'] = 'Recaptcha';
$this->validation->set_fields($fields);
if ($this->validation->run() == FALSE)
{
// if false outputs errors
$this->validation->output_errors();
redirect( lang('webshop_folder').'/unsubscribe','refresh');
}
else
{
$email = $this->input->post('email');
$this->MSubscribers->removeSubscriber($email);
flashMsg('success',lang('subscribe_you_been_unsubscribed'));
redirect( lang('webshop_folder').'/index','refresh');
}
}
}
view unsubscribe
<?php print displayStatus();?>
<?php
if ($this->session->flashdata('subscribe_msg')){
echo "<div class='status_box'>";
echo $this->session->flashdata('subscribe_msg');
echo "</div>";
}
?>
<?php echo form_open($this->lang->line('webshop_folder')."/unsubscribe"); ?>
<h1>
<?php echo form_fieldset('Unsubscribe Our Newsletter'); ?>
</h1>
<h5>*Email</h5>
<input type="text" name="email" id="email" value="<?php echo set_value('email'); ?>" size="40" />
<h5>*Are you human?</h5>
<?php echo "<p>$cap_img</p>" ;?>
<div><input type="submit" value="Subscribe" /></div>
<?php echo form_fieldset_close(); ?>
<?php echo form_close(); ?>






























[...] Codeigniter shopping cart v1.1 Part 17: Newsletter | Okada Design Blog [...]
this looks fantastic, im def checking out this system. Thanks!