コード例 #1
0
 /**
  * @param $cartId
  * @param $currency
  * @param $country
  * @return Cart|null
  */
 public function createCart($currency, $country, LineItemDraftCollection $lineItems)
 {
     $shippingMethodResponse = $this->shippingMethodRepository->getByCountryAndCurrency($country, $currency);
     $cartDraft = CartDraft::ofCurrency($currency)->setCountry($country)->setShippingAddress(Address::of()->setCountry($country))->setLineItems($lineItems);
     if (!$shippingMethodResponse->isError()) {
         /**
          * @var ShippingMethodCollection $shippingMethods
          */
         $shippingMethods = $shippingMethodResponse->toObject();
         $cartDraft->setShippingMethod($shippingMethods->current()->getReference());
     }
     $cartCreateRequest = CartCreateRequest::ofDraft($cartDraft);
     $this->profiler->enter($profile = new Profile('createCart'));
     $cartResponse = $cartCreateRequest->executeWithClient($this->client);
     $this->profiler->leave($profile);
     $cart = $cartCreateRequest->mapResponse($cartResponse);
     return $cart;
 }
コード例 #2
0
 /**
  * @param $cartId
  * @param $currency
  * @param $country
  * @return Cart|null
  */
 public function createCart($locale, $currency, $country, LineItemDraftCollection $lineItems, $customerId = null)
 {
     $client = $this->getClient();
     $shippingMethodResponse = $this->shippingMethodRepository->getByCountryAndCurrency($locale, $country, $currency);
     $cartDraft = CartDraft::ofCurrency($currency)->setCountry($country)->setShippingAddress(Address::of()->setCountry($country))->setLineItems($lineItems);
     if (!is_null($customerId)) {
         $cartDraft->setCustomerId($customerId);
     }
     if (!$shippingMethodResponse->isError()) {
         /**
          * @var ShippingMethodCollection $shippingMethods
          */
         $shippingMethods = $shippingMethodResponse->toObject();
         $cartDraft->setShippingMethod($shippingMethods->current()->getReference());
     }
     $cartCreateRequest = CartCreateRequest::ofDraft($cartDraft);
     $cartResponse = $cartCreateRequest->executeWithClient($client);
     $cart = $cartCreateRequest->mapFromResponse($cartResponse, $this->getMapper($locale));
     $this->session->set(self::CART_ID, $cart->getId());
     $this->session->set(self::CART_ITEM_COUNT, $cart->getLineItemCount());
     return $cart;
 }