I am building a onepagecheckout for craft commerce 3. I created a custom controller to submit cart and payment info in one form submit. I'll call the default updateCart action with a curl request and send the formdata with it. This works but create a new cart eachtime instead of update the current cart in the current session. How i'll update the current cart instead of create a new one?
this is my custom action
<?php
namespace modules\modulename\controllers;
use Craft;
use craft\web\Controller;
use craft\helpers\UrlHelper;
class CheckoutController extends Controller
{
protected $allowAnonymous = true;
public function actionCheckout(){
// Require POST request
$this->requirePostRequest();
// Get params
$data = $this->request->post();
try {
$url = UrlHelper::siteUrl() . "actions/commerce/cart/update-cart";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json'));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
$resp = curl_exec($curl);
$jsonResponse = \json_decode($resp);
// Show error if fails
if(!empty($jsonResponse)) {
if($jsonResponse->success){
Craft::$app->session->setError($jsonResponse->message);
} else {
Craft::$app->session->setError($jsonResponse->error);
}
} else{
Craft::$app->session->setError('mis');
}
curl_close($curl);
} catch (\Throwable $th) {
throw $th;
}
$redirect = UrlHelper::siteUrl() . "webshop/checkout/onepagecheckout";
$this->redirect($redirect);
Craft::$app->end();
}
}