Hi, today i’m gonna show you how to add ‘Autocomplete Search for Magento Customer by API Call‘. I assume you already read my first post about calling Magento API ‘Play with Magento API SOAP V1‘. This method using jQuery Autocomplete, here is the link.
As i mention in ‘Play with Magento API SOAP V1‘, first of all you gonna need is to make connection between our application and Magento API. Since post before use indonesian language i’ll repeat again here to make sure you follow correct instruction.
Connection
Setting SOAP Account
We need SOAP account to integrate with system and getting the roles we want.
- Login to backend magento
- System -> Web Services -> SOAP/XML-RPC – Roles
- Add New Role
- Resource Access -> All -> Save Role
- System -> Web Services -> SOAP/XML-RPC – Users
- Add New User
- On ‘User Role’ Tab, choose role we’ve made before
- Save User
Login with SOAP Account
In your host, create php file (ex. magehost.php) and put script below to login using SOAP account you’ve created.
$config=array();
$config["hostname"] = "namahost.com"; //ini adalah host magento anda
$config["login"] = "apiuser"; //ini adalah user API anda
$config["password"] = "apipassword"; //ini adalah password magento anda
$proxy = new SoapClient('http://'.$config["hostname"].'/index.php/api/soap/?wsdl', array('trace'=>1));
$sessionId = $proxy->login($config["login"], $config["password"]);
Hold it here, then we’re gonna make one file to handle autocomplete processing.
jQuery Autocomplete and API Call
jQuery Autocomplete
After we’ve done download jQuery UI, we need to include it on file processing. On this article, i’ll use “Default Functionality” method to request customer name and ID via API SOAP. You can see the full source example method here.
Remember to put jQuery file before jQuery and add css file.
API Call
To call customer data on magento we use ‘customer.list‘. This will allow you to retrieve all customer list on Magento.
Let’s create one file on host (ex. autocomplete.php) and put the code below.
<?php
require_once('magehost.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Autocomplete Customer Magento Call by API</title>
<link href="your_style.css" rel="stylesheet" type="text/css">
<link href="jquery-ui-1.10.4.custom.min.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="jquery-1.10.2.js"></script>
<script type="text/javascript" src="jquery-ui-1.10.4.custom.min.js"></script>
</head>
<body>
<input type="text" class="input-text" id="fName" name="customerName" />
<script type="text/javascript">
$(document).ready(function() {
var customerName = [
<?php
try {
$action = "customer.list"; // action for calling customer data
$customers = $proxy->call($sessionId, $action);
foreach($customers as $customer) {
$lastname = str_replace("'", "", $customer['lastname']);
echo "'".$customer['firstname'].' '.$lastname.' '.$customer['customer_id']."',";
}
}
catch (Exception $e) { //while an error has occured
echo "==> Error: ".$e->getMessage(); //we print this
exit();
}
?>
];
$( "#fName" ).autocomplete({
source: customerName
});
});
</script>
</body>
</html>
Explanation
In the code above , at first we will include our host which is magehost.php => require_once(‘magehost.php’).
Then we make textbox with id fName, the ID will use as identifier for jQuery Autocomplete. And we retrieve all customer list using customer.list and make loop for data. Please remember, the acceptable array format for ‘default fuctionality’ autocomplete is like this [“ActionScript”, “AppleScript”, “Asp”]. Other than that your script will show error. Use console log to debug the script. And to handled error from API call we use Exception something like $e->getMessage(). And if you succes all Magento Customer will retrieve on textbox as autocomplete.
Magento Customer Autocomplete
Noted : This will make your website slower if you have thousands customer list. Use filter to manipulated it. For example you can use :
$filters = array( array('firstname' => array('like'=>'your_input_text%')));
$customers = $proxy->call($sessionId, $action, $filters);
Hope this help 😀