Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 12 additions & 29 deletions src/controllers/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ class CartController extends Controller

public function actionRestore(): Response
{
if (! Craft::$app->plugins->isPluginEnabled('commerce')) {
throw new HttpException(400, 'Craft Commerce needs to be installed and enabled to restore carts.');
}

$number = Craft::$app->getRequest()->getParam('number');

if (! $number) {
Expand All @@ -37,17 +33,15 @@ public function actionRestore(): Response
throw new HttpException(400, 'Cannot restore a completed order');
}

if (! $order->hasLineItems()) {
throw new HttpException(400, 'Cart is empty');
}

// Get current user
$currentUser = Craft::$app->getUser()->getIdentity();
$currentUserId = $currentUser ? $currentUser->id : null;

// Check if cart belongs to a user account
// If user is not logged in, or logged in as different user
if ($order->customerId && (! $currentUserId || $order->customerId !== $currentUserId)) {
// Check if cart belongs to a CREDENTIALED user account (matches Commerce's behavior)
$cartCustomer = $order->getCustomer();

// If no one is logged in, or wrong user is logged in
if ($cartCustomer && $cartCustomer->getIsCredentialed() && (! $currentUserId || $order->customerId !== $currentUserId)) {
// Show message page using CP template mode
$loginPath = Craft::$app->getConfig()->getGeneral()->getLoginPath();
$loginUrl = \craft\helpers\UrlHelper::url($loginPath, [
Expand All @@ -64,36 +58,25 @@ public function actionRestore(): Response
return $this->asRaw($html);
}

// At this point, either:
// - Cart has no customer (guest cart), OR
// - Current user owns the cart
// At this point, one of these is true:
// - Cart has no customer (guest cart)
// - Cart has non-credentialed customer (guest with email only)
// - Current user owns the cart (credentialed customer)

// Clear current cart
// Clear current cart and restore using Commerce 5's API
$cartsService = $commerce->getCarts();
$cartsService->forgetCart();
$cartsService->setSessionCartNumber($order->number);

// Set session
$session = Craft::$app->getSession();
$session->set('commerce_cart', $order->number);

// Use reflection to set the cart
try {
$reflection = new \ReflectionClass($cartsService);
$cartProperty = $reflection->getProperty('_cart');
$cartProperty->setAccessible(true);
$cartProperty->setValue($cartsService, $order);
} catch (\Exception $exception) {
Craft::error('Reflection failed: ' . $exception->getMessage(), 'klaviyo-connect-plus');
}

$session->setNotice(Craft::t('klaviyo-connect-plus', 'Your cart has been restored.'));

/** @var Settings $settings */
$settings = Plugin::getInstance()->getSettings();
$cartUrl = $settings->cartUrl;

if ((string) $cartUrl === '') {
throw new HttpException(400, 'Cart URL is required.');
throw new HttpException(400, 'Cart URL is not configured. Please set it in Settings → Klaviyo Connect Plus → Cart URL.');
}

return $this->redirect($cartUrl);
Expand Down