Thursday, August 30, 2018

How to get Currency symbol in Magento2




How to get currency symbol in Magento2?

It is very simple to retrieve currency symbol from Magento2

In your Helper add a constructor like below
    protected $storeManager;
    protected $_localeCurrency;

public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storemanager,
        \Magento\Framework\Locale\CurrencyInterface $localeCurrency
       ) {
        $this->_storeManager       = $storemanager;
        $this->_localeCurrency     = $localeCurrency;
         }


/**
     * [getCurrencySymbol get currency symbol]
     * @return string currency symbol
     */
    public function getCurrencySymbol()
    {
        $code     = $this->_storeManager->getStore()->getCurrentCurrencyCode();
        $currency = $this->_localeCurrency->getCurrency($code)->getSymbol();
        return $currency;
    }

Signup in Codeigniter



Today i am going to discuss about the sign-up functionality in codeigniter, those who are not aware of codeigniter I will give a short explanation about it.

Codeigniter is a popular Php based MVC frame work, which is light weight and fast, lot of inbuilt libraries included and we can include our own libraries etc, very user friendly easy to build a website with in a short span of time.

How we can create a signup functionality in the Codeigniter framework? It is very simple and also very powerful.

First create a controller file called Signup.php in the application controllers folder and past this below code, also don't for get to connect your database in the config/database.php file
<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Signup extends CI_Controller
{

public function __construct()
{
parent::__construct();

$this->load->helper('form');
$this->load->library('form_validation');
$this->load->library('session');
$this->load->library('email');
$this->load->helper('url');
$this->load->database();
$this->load->model('User_Model');
}

/**
* [index loading singup form]
*/
public function index()
{
$this->load->view('templates/header');
$this->load->view('templates/signup_form');
}

/**
* [form_submit form submission]
* @return [type] [description]
*/
public function form_submit()
{
$this->form_validation->set_rules('name', 'name', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required|matches[password]');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');

if ($this->form_validation->run() == false) {
$this->load->view('templates/header');
$this->load->view('templates/signup_form');
} else {
$key = sha1(mt_rand(10000, 99999) . time() . $this->input->post('email'));
$data = array(
'name' => $this->input->post('name'),
'password' => md5($this->input->post('password')),
'email' => $this->input->post('email'),
'ip_address' => $this->input->post('ip'),
'activation_key' => $key,
'active_status' => '0',
);
$email = $this->input->post('email');
$user = $this->checkUserexists($email);
if (!$user) {
$this->User_Model->insertUserDetails($data);
$this->sendActivationLinktoCustomer($email, $key);
$this->session->set_flashdata('msg', '<div id="alert-success" class="alert alert-success text-center">Successfully registered. Please confirm the mail that has been sent to your email. </div>');
} else {
$this->session->set_flashdata('msg', '<div id="alert-danger" class="alert alert-danger text-center">This email address is already exists in our database. </div>');
}

$this->load->view('templates/header');
$this->load->view('templates/signup_form');
}
}

/**
* [checkUserexists checking the currect signup form submitted user is exists or not in our db]
* @param [string] $email email address of the current signup form submited user
* @return [string] user detials
*/
public function checkUserexists($email)
{
$user = $this->User_Model->checkUser($email);
return $user;
}

/**
* [sendActivationLinktoCustomer description]
* @param [string] $email [email address of a user]
* @param [string] $key activation key
* @return void
*/
public function sendActivationLinktoCustomer($email, $key)
{
$data = array(
'email' => $email,
'key' => $key,
'activation_url' => site_url() . '/signup/activate?email=' . $email . '&key=' . $key,
);
$this->load->library('email');
$config['protocol'] = 'ssmtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user'] = 'naveen.bos@gmail.com';
$config['smtp_pass'] = '';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['mailtype'] = 'text';
$config['validation'] = true;
$this->email->initialize($config);
$this->email->from('naveen.bos@gmail.com', 'Naveen');
$this->email->to($email);
$this->email->subject("Activation Link");
$message = $this->load->view('email/activation', $data, true);
$this->email->message($message);
if ($this->email->send($message)) {
return true;
} else {
$this->session->set_flashdata('msg', '<div id="alert-danger" class="alert alert-danger text-center">Email not send. </div>');
redirect('signup', 'refresh');
}
}

/**
* [activate function is used for activating users using the signup link]
* getting query strings from url
* @return
*/
public function activate()
{
$params = $this->input->server('QUERY_STRING');
$email = $this->input->get('email', true);
$key = $this->input->get('key', true);
$space_check = strrpos($email, " ");
if ($space_check) {
$email = str_replace(" ", "+", $email); //This line is added for replacing space to plus sign, when user add email id with plus for signup
}
$this->User_Model->activateUser($email, $key);
$this->session->set_flashdata('msg', '<div id="alert-success" class="alert alert-success text-center">Successfully Activated. Please login. </div>');
redirect('signup', 'refresh');
}
}

Then we can create a Model file called User_Model.php inside application models folder and paste below code 

<?
defined('BASEPATH') or exit('No direct script access allowed');

class User_Model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library('session');
}

/**
* [insertUserDetails insert details of a user in db]
* @param [string] $data details from the signup form
* @return [string] id of user
*/
public function insertUserDetails($data)
{
return $this->db->insert('user', $data);
}

/**
* [checkUser check user exists or not]
* @param string $email email id of the user
* @return string user data
*/
public function checkUser($email)
{
$this->db->like('email', $email);
$query = $this->db->get('user');
return $query->result();
}

/**
* [activateUser activate user after clicking url from email]
* @param [string] $email email address of a user
* @param [activation key] $key activation key of that user
* @return void
*/
public function activateUser($email, $key)
{
$this->db->select('user_id');
$this->db->from('user');
$this->db->where('email', $email);
$this->db->where('activation_key', $key);
$query = $this->db->get();
$user_id = $query->row('user_id');
if ($user_id != "") {
$data = array(
'active_status' => '1',
);
$this->db->where('user_id', $user_id);
$this->db->update('user', $data);
return true;
} else {
return false;
}
}

}

 Then we can create a view file inside application/views/templates/signup_form.php

<?
defined('BASEPATH') or exit('No direct script access allowed');
?>
<br/><br/><br/>
<?php echo $this->session->flashdata('msg'); ?>
<?php echo form_open('signup/form_submit'); ?>
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="form-group">
<h2>Signup Form</h2>
<h5>Name</h5>
<input id="name" type="text" class="form-control" name="name" value="<?php echo set_value('name'); ?>" size="50" />
<?php if (form_error('name')) {?>
<div class="alert alert-danger"><?php echo form_error('name'); ?></div>
<?php }?>
</div>
<div class="form-group">
<h5>Password</h5>
<input id="pswd" type="text" class="form-control" name="password" value="<?php echo set_value('password'); ?>" size="50" />
<?php if (form_error('password')) {?>
<div class="alert alert-danger"><?php echo form_error('password'); ?></div>
<?php }?>
</div>
<div class="form-group">
<h5>Confirm Password</h5>
<input id="pswdconf" type="text" class="form-control" name="passconf" value="<?php echo set_value('passconf'); ?>" size="50" />
<?php if (form_error('passconf')) {?>
<div class="alert alert-danger"><?php echo form_error('passconf'); ?></div>
<?php }?>
</div>
<div class="form-group">
<h5>Email Address</h5>
<input id="email_id" type="text" class="form-control" name="email" value="<?php echo set_value('email'); ?>" size="50" />
<?php if (form_error('email')) {?>
<div class="alert alert-danger"><?php echo form_error('email'); ?></div>
<?php }?>
</div>
<div class="form-group">
<h5>Email Address</h5>
<input id="ip" type="hidden" class="form-control" name="ip" value='<?php echo $_SERVER['REMOTE_ADDR']?>' size="50" />
</div>
<div><input type="submit" class="btn btn-default" value="Submit" /></div>
</div>
</div>
</div>
</body>
</html>

 For more clarification please check my git account from here