Here is the full file sending info to the deals endpoint:
$api_token = ‘********************************’;
// Pipedrive company domain
// Extract the JSON payload from the incoming request
$payload = file_get_contents(‘php://input’);
$order_data = json_decode($payload, true);
$company_domain = ‘fioritaflowers’;
$order_id = $order_data[‘id’];
$customer_name = $order_data[‘shipping’][‘first_name’] . ’ ’ . $order_data[‘shipping’][‘last_name’];
$customer_phone = $order_data[‘shipping’][‘phone’];
$customer_address = $order_data[‘shipping’][‘address_1’] . ’ ’ . $order_data[‘shipping’][‘address_2’];
$customer_postcode = $order_data[‘shipping’][‘postcode’];
$sender_name = $order_data[‘billing’][‘first_name’]. ’ ’ . $order_data[‘billing’][‘last_name’];
$sender_phone = $order_data[‘billing’][‘phone’];
$sender_email = $order_data[‘billing’][‘email’];
$order_total = $order_data[‘total’];
$order_date = $order_data[‘date_created’];
// Initialize a counter for the occurrence of “_wc_eco_fields_value”
$value_counter = 0;
// Check if the “fee_lines” key exists in the data
// if (isset($order_data[‘fee_lines’]) && is_array($order_data[‘fee_lines’])) {
// // Loop through the fee lines to find occurrences of “_wc_eco_fields_value”
// foreach ($order_data[‘fee_lines’] as $fee_line) {
// // Check if the fee line has the “_wc_eco_fields_value” key
// if (isset($fee_line[‘meta_data’]) && is_array($fee_line[‘meta_data’])) {
// foreach ($fee_line[‘meta_data’] as $meta) {
// if (isset($meta[‘key’]) && $meta[‘key’] === ‘_wc_eco_fields_value’) {
// $value_counter++;
// // Check if this is the third occurrence
// if ($value_counter === 2) {
// $delivery_date_string = is_array($meta[‘value’]) ? reset($meta[‘value’]) : ‘’;
// // Output the value of the third occurrence
// break 2; // Exit both loops
// }
// }
// }
// }
// }
// }
// Convert the date string to a valid date format (YYYYMMDD)
// $parts = explode(‘/’, $delivery_date_string);
// if (count($parts) === 3) {
// $day = str_pad($parts[0], 2, ‘0’, STR_PAD_LEFT);
// $month = str_pad($parts[1], 2, ‘0’, STR_PAD_LEFT);
// $year = substr($parts[2], -2); // Get the last two digits of the year
// $year_full = $parts[2];
// // $delivery_date = $year . $month . $day;
// // $exp_close_day = $day . ‘.’ . $month . ‘.’ . $year_full;
// } else {
// echo “Invalid date format: $delivery_date_string”;
// }
$deal_title = $delivery_date . ’ ’ . $order_id . ’ ’ . $customer_name;
// Initialize an empty array to store product data
$products_bought = array();
$line_items = $order_data[‘line_items’];
foreach ($line_items as $item) {
$product_name = $item[‘name’];
$product_quantity = $item[‘quantity’];
$product_price = $item[‘price’];
$product_image_url = isset($item[‘image’][‘src’]) ? $item[‘image’][‘src’] : ‘’;
// Create an array to represent the product
$product = array(
'name' => $product_name,
'quantity' => $product_quantity,
'price' => $product_price,
'image_url' => $product_image_url, // Include the product image URL here
);
// Add the product to the list of products bought
$products_bought[] = $product;
}
$products_bought_plain = “”;
foreach ($products_bought as $product) {
// Format each product entry
$productText = “{$product[‘name’]} (Quantity: {$product[‘quantity’]}, Price: {$product[‘price’]})”;
// Add the product image URL as a link
$productText .= "\nImage URL: {$product['image_url']}\n";
// Append this product to the plain text list
$products_bought_plain .= $productText . "\n";
}
// Output the plain text list
echo $products_bought_plain;
// Deal title and Organization ID
$deal = array(
‘title’ => $deal_title,
‘value’ => $order_total,
‘pipeline_id’ => 2,
‘expected_close_date’ => $order_date,
‘31d16193cdf2f40ad0d079ba6ce125cca6d5318f’ => $sender_name,
‘dbf4a6d8283c506241d4c3e9f42b08717b28c7bf’ => $customer_name,
‘person_id’ => $customer_name,
‘ca34cc84eb3345bec0ca84ea385f5fe3a8c51242’ => $customer_phone,
‘85721ee018ee6e54097d5e7ca53ad39757e74ee7’ => $customer_address,
‘b708db8c32a00e67c354eda18d2d3f287b6cca1b’ => $customer_postcode,
// ‘8e2203728f8c4e21c0770146c63339ae0313563c’ => $exp_close_day,
‘535052f880ae4f50a5180b171cc62561c7f2b2e0’ => $sender_phone,
‘a04d63d7fb0aa8ed9f49068f7e056b1c19d6114a’ => $sender_email,
‘353ae4a6aa4f7bbe86bb846c65107ea255539e76’ => $products_bought_plain,
// ‘products_bought’ => $products_bought, // Include the products bought here
);
$url = ‘https://’ . $company_domain . ‘.pipedrive.com/api/v1/deals?api_token=’ . $api_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $deal);
echo ‘Sending request…’ . PHP_EOL;
$output = curl_exec($ch);
curl_close($ch);
// Create an array from the data that is sent back from the API
// As the original content from server is in JSON format, you need to convert it to PHP array
$result = json_decode($output, true);
// Check if an ID came back, if did print it out
if (!empty($result[‘data’][‘id’])) {
echo ‘Deal was added successfully!’ . PHP_EOL;
}
foreach ($order_data as $key => $value) {
echo $key . PHP_EOL;
}
foreach ($deal as $key => $value) {
echo $key . PHP_EOL;
}
I commented out some code that might be debatable although it worked a week ago, but still no progress