<a href="https://console.cloud.google.com/apis/credentials">Get Your Api Credentials Here</a> <a href="https://calendar.google.com/calendar">Get your Calender ID</a> <?php include_once("wp-load.php"); function get_calender_events() { $params = array(); /*Get current date*/ $current_date = date('Y-m-d H:i:s'); /*Convert it to google calendar's rfc_format */ $rfc_format = date("c", strtotime($current_date)); $params[] = 'orderBy=startTime'; $params[] ='maxResults=100'; $params[] = 'timeMin='.urlencode($rfc_format); $url_param = ''; foreach($params as $param) { $url_param.= '&'.$param; } $calender_id = "calender_id"; $client_key = "client_key"; $url = "https://www.googleapis.com/calendar/v3/calendars/".$calender_id."/events?key=".$client_key."&singleEvents=true".$url_param; $list_events = wp_remote_post($url, ...
How to Add customer by using soap.
1st create SOAP User and roles
<?php
$proxy = new SoapClient('http://phpserver:8090/magento-projects/demo/api/soap/?wsdl');
$sessionId = $proxy->login('samrat', 'samrat123');
// Create new customer
$newCustomer = array(
'firstname' => 'samrat',
'lastname' => 'parida',
'email' => 'samrat.ke.parida@gmail.com',
'password' => 'password123',
'store_id' => 1,
'website_id' => 1,
'group_id' => 3,
);
$newCustomerId = $proxy->call($sessionId, 'customer.create', array($newCustomer));
//Create new customer address
$newCustomerAddress = array(
'firstname' => 'Ajay',
'lastname' => 'Pogul',
'country_id' => 'USA',
'region_id' => '43',
'region' => 'New York',
'city' => 'New York',
'street' => array('street1','street2'),
'telephone' => '5555-555',
'postcode' => 10021,
'fax' => 45435354,
'is_default_billing' => true,
'is_default_shipping' => false
);
$newAddressId = $proxy->call($sessionId, 'customer_address.create', array($newCustomerId, $newCustomerAddress));
var_dump($proxy->call($sessionId, 'customer_address.list', $newCustomerId));
//Update customer address
//$proxy->call($sessionId, 'customer_address.update', array($newAddressId, array('firstname'=>'Changed Firstname')));
//var_dump($proxy->call($sessionId, 'customer_address.list', $newCustomerId));
// Delete customer address
/*
$proxy->call($sessionId, 'customer_address.delete', $newAddressId);
var_dump($proxy->call($sessionId, 'customer_address.list', $newCustomerId));
*/
?>
or by SAP get customers.
<?php
ini_set('default_socket_timeout', 300);
$conf = new ArrayObject(array(
'sapUrl' => 'http://54.253.117.216/EDI/customersservice.asmx?WSDL',
'sapNS' => 'http://www.hammersley.com.au/edi/customers',
'sapUsername' => 'EDI',
'sapPassword' => 'magenta',
'magUrl' => 'http://trinature-dev.rb.com.au/api/soap/?wsdl',
'magUsername' => 'ajayp',
'magPassword' => 'ajaypsap',
), ArrayObject::ARRAY_AS_PROPS);
$sync = new CustomerSync($conf);
$sync->run();
exit;
class CustomerSync
{
private $conf;
private $sap;
private $saplist;
private $mag;
private $msess; // magento session
private $maglist;
public function __construct($conf)
{
$this->conf = $conf;
}
function __destruct()
{
if ( is_object($this->mag) && isset($this->msess) )
{
$this->mag->endSession($this->msess);
unset($this->msess);
}
}
protected function sap()
{
if ( ! is_object($this->sap) )
{
$this->sap = new SoapClient($this->conf->sapUrl);
$headerBody = array(
'Username' => $this->conf->sapUsername,
'Password' => $this->conf->sapPassword,
);
$header = new SOAPHeader($this->conf->sapNS, 'Authentication', $headerBody);
$this->sap->__setSoapHeaders($header);
}
return $this->sap;
}
protected function mag()
{
if ( ! is_object($this->mag) )
{
$this->mag = new SoapClient($this->conf->magUrl);
$this->msess = $this->mag->login($this->conf->magUsername, $this->conf->magPassword);
}
return $this->mag;
}
public function run()
{
$out = $this->mag()->call($this->msess, 'customer.list');
//$this->dump($out);
if ( ! is_array($out) ) return $this->error('get all magento customers failed');
$this->maglist = $out;
$out = $this->sap()->__call('GetAll', array());
// $this->dump($out);
if ( ! is_array($out->Customers) ) return $this->error('get all sap customers failed');
$this->saplist = $out->Customers;
$found = array();
foreach ( $this->saplist as $obj )
{
$customer = get_object_vars($obj);
$mag_customer_id = $this->findmag($customer['EmailAddress']);
if ( $mag_customer_id > 0 )
{
$found[$mag_customer_id] = 1;
}
else
{
$this->add2mag($customer);
// break; # test
}
}
foreach ( $this->maglist as $customer )
{
if ( $found[$customer['customer_id']] != 1 )
{
$this->add2sap($customer);
// die('test');
}
}
}
protected function findmag($email)
{
foreach ( $this->maglist as $i => $customer )
{
if ( $customer['email'] == $email ) return $customer['customer_id'];
}
return 0;
}
protected function add2mag($customer)
{
$this->deb('sap customer "'.$customer['Name'].'" ('.$customer['EmailAddress'].') not found in magento');
//$this->dump($customer, 'sap customer data');
if ( $customer['EmailAddress'] == '' ) return;
list($firstname, $lastname) = split(' ', $customer['Name'], 2);
if ( $lastname == '' ) $lastname = $firstname;
$params = array(
'email' => $customer['EmailAddress'],
'firstname' => $firstname,
'lastname' => $lastname,
'password' => 'trinature2014',
'website_id' => 1,
'store_id' => 1,
'group_id' => $customer['PriceList'],
);
// $this->dump($params, 'create new magento customer params');
$result = $this->mag()->call($this->msess, 'customer.create', array($params));
//$this->dump($result, 'create new magento customer result');
$customer_id = $result;
$params = array(
'firstname' => $firstname,
'lastname' => $lastname,
'street' => array($customer['BillTo_Address']),
'city' => $customer['BillTo_City'],
'street' => $customer['BillTo_Street'],
'country_id' => $customer['BillTo_Country'],
'region' => $customer['BillTo_State'],
'region_id' => 0,
'postcode' => $customer['BillTo_ZipCode'],
'telephone' => ($customer['Phone1'] != '' ? $customer['Phone1'] : ($customer['Phone2'] != '' ? $customer['Phone2'] : '0')),
'fax' => $customer['Fax'],
'is_default_billing' => true,
'is_default_shipping' => false
);
$this->dump($result, 'create new magento customer bill address params');
$result = $this->mag()->call($this->msess, 'customer_address.create', array('customerId' => $customer_id, 'addressdata' => $params));
$this->dump($result, 'create new magento customer bill address result');
$params = array(
'firstname' => $firstname,
'lastname' => $lastname,
'street' => array($customer['ShipTo_Address']),
'city' => $customer['ShipTo_City'],
'street' => $customer['ShipTo_Street'],
'country_id' => $customer['ShipTo_Country'],
'region' => $customer['ShipTo_State'],
'region_id' => 0,
'postcode' => $customer['ShipTo_ZipCode'],
'telephone' => ($customer['Phone2'] != '' ? $customer['Phone2'] : ($customer['Phone1'] != '' ? $customer['Phone1'] : '0')),
'fax' => $customer['Fax'],
'is_default_billing' => false,
'is_default_shipping' => true
);
$this->dump($params, 'create new magento customer ship address params');
$result = $this->mag()->call($this->msess, 'customer_address.create', array('customerId' => $customer_id, 'addressdata' => $params));
$this->dump($result, 'create new magento customer ship address result');
}
protected function magaddr($customer_id)
{
$out = $this->mag()->call($this->msess, 'customer_address.list', $customer_id);
// $this->dump($out, 'magento customer id='.$customer_id.' address list');
return $out;
}
protected function add2sap($customer)
{
$this->deb('magento customer "'.$customer['firstname'].'_'.$customer['lastname'].'" ('.$customer['email'].') not found in sap');
//$this->dump($customer, 'magento customer data');
$addrlist = $this->magaddr($customer['customer_id']);
$phone1;
$phone2;
$fax;
$bill_addr = array(
'BillTo_Block' => '',
'BillTo_Address' => '',
'BillTo_ZipCode' => '',
'BillTo_City' => '',
'BillTo_County' => '',
'BillTo_Country' => '',
);
$ship_addr = array(
'ShipTo_Block' => '',
'ShipTo_Address' => '',
'ShipTo_ZipCode' => '',
'ShipTo_City' => '',
'ShipTo_County' => '',
'ShipTo_Country' => '',
);
foreach ($addrlist as $addr)
{
if ( $addr['is_default_billing'] == true )
{
$bill_addr = array(
'BillTo_Block' => '',
'BillTo_Address' => $addr['street'],
'BillTo_ZipCode' => $addr['postcode'],
'BillTo_City' => $addr['city'],
'BillTo_County' => $addr['region'],
'BillTo_Country' => $addr['country_id'],
);
if ( $fax == '' ) $fax = $addr['fax'];
if ( $phone1 == '' )
{
$phone1 = $addr['telephone'];
}
elseif ( $phone2 == '' )
{
$phone2 = $addr['telephone'];
}
}
if ( $addr['is_default_shipping'] == true )
{
$ship_addr = array(
'ShipTo_Block' => '',
'ShipTo_Address' => $addr['street'],
'ShipTo_ZipCode' => $addr['postcode'],
'ShipTo_City' => $addr['city'],
'ShipTo_County' => $addr['region'],
'ShipTo_Country' => $addr['country_id'],
);
if ( $fax == '' ) $fax = $addr['fax'];
if ( $phone1 == '' )
{
$phone1 = $addr['telephone'];
}
elseif ( $phone2 == '' )
{
$phone2 = $addr['telephone'];
}
}
}
$this->dump($bill_addr, 'create sap customer bill addr');
$this->dump($ship_addr, 'create sap customer ship addr');
$params = array_merge(array(
'Code' => 'M'.$customer['customer_id'],
'Name' => $customer['firstname'].'_'.$customer['lastname'],
'CreditLimit' => '0.0',
'Balance' => '0.0',
'Active' => true,
'PriceList' => '4',
'RefCode' => $customer['customer_id'],
'Phone1' => $phone1,
'Phone2' => $phone2,
'Fax' => $fax,
'ContactPerson' => '',
'EmailAddress' => $customer['email'],
), $bill_addr, $ship_addr);
$this->dump($params, 'create sap customer params');
$result = $this->sap()->__call('Save', array('Save' => array('Customer' => $params)));
$this->dump($result, 'create sap customer result');
}
protected function deb($msg)
{
print $msg."\n";
}
protected function dump($mix, $msg = '')
{
if ( $msg != '' ) print $msg.': ';
var_dump($mix);
}
protected function error($msg)
{
print $msg."\n";
return false;
}
}
?>
Comments
Post a Comment