Created
May 25, 2023 17:22
-
-
Save kulterryan/b59c0fa54af783284dde9c03637c2923 to your computer and use it in GitHub Desktop.
Connect your KylasCRM with WooCommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Kylas Integration | |
function kylasIntegration( $orderdata,$leadtype,$groupid) { | |
//echo "=============";echo "<pre>";print_r($orderdata);echo "</pre>";die; | |
if($leadtype=='abandoned'){ | |
$leadtype=(int)1072133; | |
} | |
if($leadtype=='enrolled'){ | |
$leadtype=(int)1072132; | |
} | |
$groupdetails=get_post($groupid); | |
//echo $groupid."=============";echo "<pre>";print_r($groupdetails);echo "</pre>";die; | |
$kylasapi_data=array( | |
"firstName"=>$orderdata['billing']['first_name'], | |
"lastName"=>$orderdata['billing']['last_name'], | |
'phoneNumbers'=>array(array( | |
"type"=>"MOBILE", | |
"code"=>'IN', | |
"value"=>$orderdata['billing']['phone'], | |
"dialCode"=>'91', | |
"primary"=>true | |
)), | |
'emails'=>array(array( | |
"type"=>"OFFICE", | |
"value"=>$orderdata['billing']['email'], | |
"primary"=>true | |
)) | |
); | |
$kylasapi_data['source']=(int)1072774; | |
if($leadtype){ | |
$kylasapi_data['customFieldValues']['cfLeadType']=$leadtype; | |
} | |
if($groupdetails->post_title){ | |
$kylasapi_data['customFieldValues']['cfBatchName']=$groupdetails->post_title; | |
} | |
//echo "=============";echo "<pre>";print_r($kylasapi_data);echo "</pre>"; | |
$curl = curl_init(); | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => 'https://api.kylas.io/v1/leads/', | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => '', | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 0, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => 'POST', | |
CURLOPT_POSTFIELDS => json_encode($kylasapi_data), | |
CURLOPT_HTTPHEADER => array( | |
'api-key: [API KEY HERE]', // API KEY Here | |
'Content-Type: application/json', | |
'Accept: application/json' | |
), | |
)); | |
$response = curl_exec($curl); | |
curl_close($curl); | |
//echo $response;die; | |
$realname=$orderdata['billing']['first_name']." ".$orderdata['billing']['last_name']; | |
$filemsg = date("Y-m-d h:i:sa"). ', '. $realname . ', ' . $orderdata['billing']['email'] . ', ' . $orderdata['billing']['phone'] . ', ' . $leadtype .', ' . $groupdetails->post_title .', '.json_encode($kylasapi_data).', ' . $response ."\n"; | |
$filemsg .="\n=====================================================================================================================================================================\n"; | |
$file = fopen("kylasleads.txt","a"); | |
fwrite($file, $filemsg); | |
fclose($file); | |
} | |
function getOrderData($order_id){ | |
$order = new WC_Order( $order_id ); | |
$order_data = $order->get_data(); | |
$product=$order->get_items(); | |
$product_id=''; | |
foreach ( $product as $item ) { | |
$product_id = $item['product_id']; | |
} | |
$group=get_post_meta($product_id,'_related_group',true); | |
return array('orderdata'=>$order_data,'group'=>$group[0]); | |
} | |
function order_abandoned($order_id) { | |
$orderdata=getOrderData($order_id); | |
kylasIntegration( $orderdata['orderdata'],'abandoned',$orderdata['group']); | |
} | |
function order_enrolled($order_id) { | |
$orderdata=getOrderData($order_id); | |
kylasIntegration( $orderdata['orderdata'],'enrolled',$orderdata['group']); | |
} | |
add_action( 'woocommerce_order_status_cancelled', 'order_abandoned', 10, 1); | |
add_action( 'woocommerce_order_status_failed', 'order_abandoned', 10, 1); | |
add_action( 'woocommerce_order_status_pending', 'order_abandoned', 10, 1); | |
add_action( 'woocommerce_order_status_on-hold', 'order_abandoned', 10, 1); | |
// Note that it's woocommerce_order_status_on-hold, and NOT on_hold. | |
add_action( 'woocommerce_order_status_completed', 'order_enrolled', 10, 1); | |
add_action( 'woocommerce_order_status_processing', 'order_enrolled', 10, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment