Magento – How to find an a customer based on phone number through API

customercustomer-addresssoap

Code Overview

When a user types in a search term php determines whether it's a last name, phone number, or an email. Based on this it will try to find a customer using magento soap api.

what i'm having trouble with is, it doesn't seem like magento soap api has an option to search for a user based on phone number.

Any suggestions for a workaround? I tried programming it by accessing MySQL directly but I feel like there must be a more straightforward solution.

<?php

    $mysql_host =   "localhost";
    $mysql_user =   "root";
    $mysql_pass =   "root";
    $mysql_db  =    "magento";


    $soapUser = "admin";
    $soapPass = "thepassword";
    $soapUrl = 'http://localhost/magento/api/v2_soap/?wsdl=1';

    session_start();

    // regex patterns
    $name_pattern = '/^[a-zA-Z]*$/';
    $phone_pattern = '/^((([0-9]{1})*[- .(]*([0-9]{3})[- .)]*[0-9]{3}[- .]*[0-9]{4})+)*$/';
    $email_pattern = '^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$^';


    if ('POST' === $_SERVER['REQUEST_METHOD']) {

        $search_term = $_POST['search_term'];



        // determine what kind of search term it is

        if (preg_match($name_pattern, $search_term)) {
            $client = new SoapClient($soapUrl);
            $session = $client->login($soapUser, $soapPass);

            $complexFilter = array(
                'complex_filter' => array(
                    array(
                        'key' => 'lastname',
                        'value' => array('key' => 'in', 'value' => $search_term)
                    )
                )
            );

            $result = $client->customerCustomerList($session, $complexFilter);

            var_dump ($result); // for now
        }


        elseif (preg_match($phone_pattern, $search_term)) {
        // no soap method - use direct mysql




        }


        elseif (preg_match($email_pattern, $search_term)) {
            $client = new SoapClient($soapUrl);
            $session = $client->login($soapUser, $soapPass);

            $complexFilter = array(
                'complex_filter' => array(
                    array(
                        'key' => 'email',
                        'value' => array('key' => 'in', 'value' => $search_term)
                    )
                )
            );

            $result = $client->customerCustomerList($session, $complexFilter);

            var_dump ($result); // for now
        } 


        else {
            echo "you typed in something weird";
        }


    }


?>
<html>
    <head>
        <title>Find Customer</title>
    </head>

    <body>
        <form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
            <label for="search_term">Search Term:</label>
            <input type="text" name="search_term">
            <input type="submit" value="search">
        </form>

        <?
            /* if something is found it will go here */
        ?>
    </body>
</html>

Best Answer

Looking at the API information you can obtain telephone number from customer_address However to do this you need to request on customer_id http://www.magentocommerce.com/api/soap/customer/customerAddress/customer_address.info.html

I cant find a public method that you would use to search all customers only by telephone number as customer_address requires the customer_id be set.

Related Topic