hello I need to Add simple product with custom options To cart throw Rest API I succeeded t add the simple product , price is correct and every thing is ok but unfortunately I didn't succeed to add custom options here is my code I'm using Curl:
<?php
//****************** get the token ********************//
$magentoURL = "https://--------.----------.org/";
$station = "fr";
$data = array("username" => "*******", "password" => "*********");
$data_string = json_encode($data);
$ch = curl_init($magentoURL . "index.php/rest/" . $station . "/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Length: " . strlen($data_string)));
$token = curl_exec($ch);
$token = json_decode($token);
$headers = array("Authorization: Bearer " . $token);
//************************* find the customer ***********************************//
$customeremail = '[email protected]';
$ch = curl_init($magentoURL . '/rest/' . $station . '/V1/customers/search?searchCriteria[filter_groups][0][filters][0][field]=email&searchCriteria[filter_groups][0][filters][0][value]=%25' . 
 $customeremail . '%25&searchCriteria[filter_groups][0][filters][0][condition_type]=like');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . $token));
$result = curl_exec($ch);
$customer = json_decode($result, true);
//************************* if customer exist ***********************************//
if ($customer['total_count'] != 0) {
 $id = $customer['items'][0]['id'];
 $ch = curl_init($magentoURL . "rest/" . $station . "/V1/carts/mine");
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
 $body = '{
 "quote": {
 "items": [{
 "sku": "mysku-sku",
 "qty": 1,
 "name": "Reservation 2020",
 "price": 320,
 "product_type": "simple",
 "product_option": {
 "extension_attributes": {
 "custom_options": [{
 "option_id": "8227",
 "option_value": "5"
 }
 ]
 }
 }
 }],
 "customer": {
 "id": '. $id.',
 "email": "[email protected]",
 "firstname": "firstname",
 "lastname": "lastname"
 }
 }
';
// set body
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . $token));
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
// send the request and save response to $response
 $response = curl_exec($ch);
 
// stop if fails
 if (!$response) {
 die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
 }
 echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
 echo 'Response Body: ' . $response . PHP_EOL;
// close curl resource to free up system resources
 curl_close($ch);
}
the response :
HTTP Status Code: 200 Response Body: []
but nothing in my cart
1 Answer 1
Using this code I succeed add product to cart but without Custom options type : Date and field
<?php
$magentoURL = "*************************";
$station = "fr";
$data = array("username" => "**********", "password" => "**********");
$data_string = json_encode($data);
$ch = curl_init($magentoURL . "index.php/rest/" . $station . "/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Length: " . strlen($data_string)));
$token = curl_exec($ch);
$token = json_decode($token);
$headers = array("Authorization: Bearer " . $token);
//************************* find customer ***********************************//
$customeremail = '**********************';
$ch = curl_init($magentoURL . '/rest/' . $station . '/V1/customers/search?searchCriteria[filter_groups][0][filters][0][field]=email&searchCriteria[filter_groups][0][filters][0][value]=%25'
 . $customeremail . '%25&searchCriteria[filter_groups][0][filters][0][condition_type]=like');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . $token));
$result = curl_exec($ch);
$customer = json_decode($result, true);
//echo '<pre>';print_r($customer);
if ($customer['total_count'] != 0) {
 $id = $customer['items'][0]['id'];
 $customerData = [
 'customer_id' => $id
 ];
 $ch = curl_init($magentoURL . "rest/" . $station . "/V1/carts/mine");
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . $token));
 $result = curl_exec($ch);
 $quote_id = json_decode($result, true);
 echo '<pre>';print_r($quote_id);
 $productData = [
 'product' => [
 'sku' => 'sku',
 'name' => 'name',
 "price" => '40'
 ]
 ];
 $ch = curl_init($magentoURL . "rest/" . $station . "/V1/products?sku=sku");
 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($productData));
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . $token));
 $result = curl_exec($ch);
 $result = json_decode($result, 1);
 echo '<pre>';print_r($result);
 $productData = [
 'cart_item' => [
 'quote_id' => $quote_id,
 'sku' => 'sku',
 'qty' => 1
 ]];
 $ch = curl_init($magentoURL . "rest/" . $station . "/V1/carts/mine/items");
 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($productData));
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . $token));
 $result = curl_exec($ch);
 if (!$result) {
 die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
 }
 echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
 echo 'Response Body: ' . $result . PHP_EOL;
 
} else {
 echo 'customer doesn\'t exist';
}
Explore related questions
See similar questions with these tags.