Search code examples
phpmagentosalesforce

How to fetch and update products from/to salesforce using php?


<?php
define("USERNAME", "abc@demo.com");
define("PASSWORD", "abc");
define("SECURITY_TOKEN", "aDyy0oukYSCsQ7qua7lgG85Jd");

require_once ('includes/soapclient/SforcePartnerClient.php');




 $client = new SoapClient('http://url/index.php/api/soap/?wsdl');

 $session = $client->login('ab', 'ab');

$result = $client->call($session, 'order.list');

echo "\n\n";

$mySforceConnection = new SforcePartnerClient();
$mySforceConnection->createConnection("includes/soapclient/partner.wsdl.xml");
$mySforceConnection->login(USERNAME, PASSWORD.SECURITY_TOKEN);

echo "<br><br>";

foreach($result as $res)
{
$records = array();
//print_r($result);

$records[0] = new stdClass();
$records[0]->fields = array(
    'FirstName' => $res['firstname'] ,
    'LastName' => $res['lastname'],
    'Email' => $res['customer_email'] 
);
$records[0]->type = 'Contact';
$response = $mySforceConnection->create($records);
/*
print_r($response);
//echo "Full Record=". $res . "<br/>";
echo "<br/> Customer Details:<br/>";
echo "FirstName=". $res['firstname'] . "<br/>";
echo "LastName=". $res['lastname'] . "<br/>";
echo "Email=". $res['customer_email'] . "<br/>";
*/
}



$ids = array();
foreach ($response as $i => $result) {
   /* echo $records[$i]->fields["FirstName"] . " "
            . $records[$i]->fields["LastName"] . " "
            . $result->id . "<br/>\n";*/
    array_push($ids, $result->id);

}


 ?>

This code works fine for inserting data into salesforce and I am also able to get the contacts to my website. But I was not able to figure out that how to fetch products in my website and update it in salesforce as well.

Please Help ...

Thanks


Solution

  • Products are stored in Product2 table and the best place to check which fields are there would be your WSDL or a describe call.

    Online explanation "what is what" is available only for standard fields (obviously, if you've created some custom ones they won't be in the docs): http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_product2.htm

    To fetch them you'd have to use query() call, something like this should get you started:

    $queryString = 'SELECT Id, Name, ProductCode FROM Product2 ORDER BY Name LIMIT 10';
    $products= $mySforceConnection -> query($queryString);
    print_r($products);
    

    Read further in the docs from the last link about update statements and you should be able to connect all the dots :)