Reading list Switch to dark mode

    How to use customer api in Opencart

    Updated 8 May 2023

    Today, we will learn about, how the customer api is used in opencart . By default, opencart uses this api while creating the order from the back end .

    To create the order from the back end, you have to login with your admin end and then go to sales->orders and click add button.

    addorder

    After clicking the add button, fill the customer details or select the existing customer from the customer field .

    addorder1

    Searching for an experienced
    Opencart Company ?
    Find out More

    After filling all the details, press the continue button.

    addorder2

    When you will press the continue button then customer api will be called to add the customer details for generating the new order. The code for the customer api is given below :

    /**
     * Webkul Software.
     *
     * @category Webkul
     * @package api
     * @author Webkul
     * @copyright Webkul Software Private Limited (https://webkul.com)
     * @license https://store.webkul.com/license.html
     */
    <?php
    class ControllerApiCustomer extends Controller
    {
    	public function index()
    	{
    		$this->load->language('api/customer');
    
    		// Delete past customer in case there is an error
    		unset($this->session->data['customer']);
    
    		$json = array();
    
    		if (!isset($this->session->data['api_id'])) {
    			$json['error']['warning'] = $this->language->get('error_permission');
    		} else {
    			// Add keys for missing post vars
    			$keys = array(
    				'customer_id',
    				'customer_group_id',
    				'firstname',
    				'lastname',
    				'email',
    				'telephone',
    			);
    
    			foreach ($keys as $key) {
    				if (!isset($this->request->post[$key])) {
    					$this->request->post[$key] = '';
    				}
    			}
    
    			// Customer
    			if ($this->request->post['customer_id']) {
    				$this->load->model('account/customer');
    
    				$customer_info = $this->model_account_customer->getCustomer($this->request->post['customer_id']);
    
    				if (!$customer_info || !$this->customer->login($customer_info['email'], '', true)) {
    					$json['error']['warning'] = $this->language->get('error_customer');
    				}
    			}
    
    			if ((utf8_strlen(trim($this->request->post['firstname'])) < 1) || (utf8_strlen(trim($this->request->post['firstname'])) > 32)) {
    				$json['error']['firstname'] = $this->language->get('error_firstname');
    			}
    
    			if ((utf8_strlen(trim($this->request->post['lastname'])) < 1) || (utf8_strlen(trim($this->request->post['lastname'])) > 32)) {
    				$json['error']['lastname'] = $this->language->get('error_lastname');
    			}
    
    			if ((utf8_strlen($this->request->post['email']) > 96) || (!filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL))) {
    				$json['error']['email'] = $this->language->get('error_email');
    			}
    
    			if ((utf8_strlen($this->request->post['telephone']) < 3) || (utf8_strlen($this->request->post['telephone']) > 32)) {
    				$json['error']['telephone'] = $this->language->get('error_telephone');
    			}
    
    			// Customer Group
    			if (is_array($this->config->get('config_customer_group_display')) && in_array($this->request->post['customer_group_id'], $this->config->get('config_customer_group_display'))) {
    				$customer_group_id = $this->request->post['customer_group_id'];
    			} else {
    				$customer_group_id = $this->config->get('config_customer_group_id');
    			}
    
    			// Custom field validation
    			$this->load->model('account/custom_field');
    
    			$custom_fields = $this->model_account_custom_field->getCustomFields($customer_group_id);
    
    			foreach ($custom_fields as $custom_field) {
    				if ($custom_field['location'] == 'account') {
    					if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
    						$json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
    					} elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) {
    						$json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
    					}
    				}
    			}
    
    			if (!$json) {
    				$this->session->data['customer'] = array(
    					'customer_id' => $this->request->post['customer_id'],
    					'customer_group_id' => $customer_group_id,
    					'firstname' => $this->request->post['firstname'],
    					'lastname' => $this->request->post['lastname'],
    					'email' => $this->request->post['email'],
    					'telephone' => $this->request->post['telephone'],
    					'custom_field' => isset($this->request->post['custom_field']) ? $this->request->post['custom_field'] : array()
    				);
    
    				$json['success'] = $this->language->get('text_success');
    			}
    		}
    
    		$this->response->addHeader('Content-Type: application/json');
    		$this->response->setOutput(json_encode($json));
    	}
    }

    When the customer details will be successfully added to the session, then the api will return the success message.

    ordersuccess1

    If you need custom Opencart Development services then feel free to reach us and also explore our exclusive range of Opencart Extensions.

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home