public static function ofCart(Cart $cart)
 {
     $entity = new static();
     $entity->shippingAddress = !is_null($cart->getShippingAddress()) ? $cart->getShippingAddress()->toArray() : [];
     $entity->billingAddress = !is_null($cart->getBillingAddress()) ? $cart->getBillingAddress()->toArray() : [];
     return $entity;
 }
 public function createOrderFromCart($locale, Cart $cart)
 {
     $client = $this->getClient();
     $request = OrderCreateFromCartRequest::ofCartIdAndVersion($cart->getId(), $cart->getVersion());
     $request->setOrderNumber($this->createOrderNumber());
     $response = $request->executeWithClient($client);
     $order = $request->mapFromResponse($response, $this->getMapper($locale));
     $this->session->remove(CartRepository::CART_ID);
     $this->session->remove(CartRepository::CART_ITEM_COUNT);
     return $order;
 }
 public function getCart($locale, $cartId = null, $customerId = null)
 {
     $cart = null;
     $client = $this->getClient();
     if ($cartId) {
         $cartRequest = CartQueryRequest::of();
         $predicate = 'id = "' . $cartId . '" and cartState = "' . CartState::ACTIVE . '"';
         if (!is_null($customerId)) {
             $predicate .= ' and customerId="' . $customerId . '"';
         }
         $cartRequest->where($predicate)->limit(1);
         $cartResponse = $cartRequest->executeWithClient($client);
         $carts = $cartRequest->mapFromResponse($cartResponse, $this->getMapper($locale));
         if (!is_null($carts)) {
             $cart = $carts->current();
             if ($cart->getCustomerId() !== $customerId) {
                 throw new \InvalidArgumentException();
             }
         }
     }
     if (is_null($cart)) {
         $cart = Cart::of($client->getConfig()->getContext());
         $this->session->remove(self::CART_ID);
         $this->session->remove(self::CART_ITEM_COUNT);
     } else {
         $this->session->set(self::CART_ID, $cart->getId());
         $this->session->set(self::CART_ITEM_COUNT, $cart->getLineItemCount());
     }
     return $cart;
 }
 public function getCart($cartId = null)
 {
     $cart = null;
     if ($cartId) {
         $cartRequest = CartByIdGetRequest::ofId($cartId);
         $this->profiler->enter($profile = new Profile('getCart'));
         $cartResponse = $cartRequest->executeWithClient($this->client);
         $this->profiler->leave($profile);
         $cart = $cartRequest->mapResponse($cartResponse);
     }
     if (is_null($cart)) {
         $cart = Cart::of($this->client->getConfig()->getContext());
     }
     return $cart;
 }
 protected function getItemCount(Cart $cart)
 {
     $count = 0;
     if ($cart->getLineItems()) {
         foreach ($cart->getLineItems() as $lineItem) {
             $count += $lineItem->getQuantity();
         }
     }
     return $count;
 }
 protected function getCartLineItems(Cart $cart)
 {
     $cartItems = new ViewData();
     $cartItems->list = new ViewDataCollection();
     $lineItems = $cart->getLineItems();
     if (!is_null($lineItems)) {
         foreach ($lineItems as $lineItem) {
             $variant = $lineItem->getVariant();
             $cartLineItem = new ViewData();
             $cartLineItem->productId = $lineItem->getProductId();
             $cartLineItem->variantId = $variant->getId();
             $cartLineItem->lineItemId = $lineItem->getId();
             $cartLineItem->quantity = $lineItem->getQuantity();
             $lineItemVariant = new ViewData();
             $lineItemVariant->url = (string) $this->generateUrl('pdp-master', ['slug' => (string) $lineItem->getProductSlug()]);
             $lineItemVariant->name = (string) $lineItem->getName();
             $lineItemVariant->image = (string) $variant->getImages()->current()->getUrl();
             $price = $lineItem->getPrice();
             if (!is_null($price->getDiscounted())) {
                 $lineItemVariant->price = (string) $price->getDiscounted()->getValue();
                 $lineItemVariant->priceOld = (string) $price->getValue();
             } else {
                 $lineItemVariant->price = (string) $price->getValue();
             }
             $cartLineItem->variant = $lineItemVariant;
             $cartLineItem->sku = $variant->getSku();
             $cartLineItem->totalPrice = $lineItem->getTotalPrice();
             $cartLineItem->attributes = new ViewDataCollection();
             $cartAttributes = $this->config['sunrise.cart.attributes'];
             foreach ($cartAttributes as $attributeName) {
                 $attribute = $variant->getAttributes()->getByName($attributeName);
                 if ($attribute) {
                     $lineItemAttribute = new ViewData();
                     $lineItemAttribute->label = $attributeName;
                     $lineItemAttribute->key = $attributeName;
                     $lineItemAttribute->value = (string) $attribute->getValue();
                     $cartLineItem->attributes->add($lineItemAttribute);
                 }
             }
             $cartItems->list->add($cartLineItem);
         }
     }
     return $cartItems;
 }