Exemple #1
0
 /**
  * Return cart from session
  *
  * If session has a cart, retrieves it and checks if exists
  * If exists, returns it
  * Otherwise, creates new one
  * If session has not a cart, creates a new one and returns it
  *
  * @return CartInterface|null Cart
  */
 public function getCartFromSession()
 {
     $cartIdInSession = $this->cartSessionManager->get();
     if (!$cartIdInSession) {
         return null;
     }
     return $this->cartRepository->findOneBy(['id' => $cartIdInSession, 'ordered' => false]);
 }
 /**
  * Get cart from session
  *
  * @return CartInterface|null Cart loaded from session
  */
 protected function loadCartFromSession()
 {
     $cartIdInSession = $this->cartSessionManager->get();
     if (!$cartIdInSession) {
         return null;
     }
     $cart = $this->cartRepository->findOneBy(['id' => $cartIdInSession, 'ordered' => false]);
     return $cart instanceof CartInterface ? $cart : null;
 }
 /**
  * Updates all the carts with the cloned address
  *
  * @param AddressOnCloneEvent $event Event
  */
 public function updateCarts(AddressOnCloneEvent $event)
 {
     $originalAddress = $event->getOriginalAddress();
     $clonedAddress = $event->getClonedAddress();
     $carts = $this->cartRepository->findAllCartsWithAddress($originalAddress);
     foreach ($carts as $cart) {
         /**
          * @var CartInterface $cart
          */
         $deliveryAddress = $cart->getDeliveryAddress();
         $billingAddress = $cart->getBillingAddress();
         if ($deliveryAddress instanceof AddressInterface && $deliveryAddress->getId() == $originalAddress->getId()) {
             $cart->setDeliveryAddress($clonedAddress);
         }
         if ($billingAddress instanceof AddressInterface && $billingAddress->getId() == $originalAddress->getId()) {
             $cart->setBillingAddress($clonedAddress);
         }
         $this->cartObjectManager->flush($cart);
     }
 }