function it_recalculates_prices_without_adding_anything_to_the_context_if_its_not_needed(DelegatingCalculatorInterface $priceCalculator, OrderInterface $order, OrderItemInterface $item, PriceableInterface $variant)
 {
     $order->getCustomer()->willReturn(null);
     $order->getChannel()->willReturn(null);
     $order->getItems()->willReturn([$item]);
     $item->isImmutable()->willReturn(false);
     $item->getQuantity()->willReturn(5);
     $item->getVariant()->willReturn($variant);
     $priceCalculator->calculate($variant, ['quantity' => 5])->willReturn(10);
     $item->setUnitPrice(10)->shouldBeCalled();
     $this->process($order);
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public function recalculate(OrderInterface $order)
 {
     $context = [];
     if (null !== ($customer = $order->getCustomer())) {
         $context['customer'] = $customer;
         $context['groups'] = $customer->getGroups()->toArray();
     }
     if (null !== $order->getChannel()) {
         $context['channel'] = [$order->getChannel()];
     }
     foreach ($order->getItems() as $item) {
         if ($item->isImmutable()) {
             continue;
         }
         $context['quantity'] = $item->getQuantity();
         $item->setUnitPrice($this->priceCalculator->calculate($item->getVariant(), $context));
     }
 }
Esempio n. 3
0
 /**
  * Recalculate the order unit prices.
  *
  * @param GenericEvent $event
  *
  * @throws UnexpectedTypeException
  */
 public function recalculatePrices(GenericEvent $event)
 {
     $order = $event->getSubject();
     if (!$order instanceof OrderInterface) {
         throw new UnexpectedTypeException($order, 'Sylius\\Component\\Order\\Model\\OrderInterface');
     }
     $context = array();
     if (null !== ($user = $order->getUser())) {
         $context['user'] = $user;
         $context['groups'] = $user->getGroups()->toArray();
     }
     foreach ($order->getItems() as $item) {
         $priceable = $item->getVariant();
         $context['quantity'] = $item->getQuantity();
         $item->setUnitPrice($this->priceCalculator->calculate($priceable, $context));
     }
     $order->calculateTotal();
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function process(BaseOrderInterface $order)
 {
     /** @var OrderInterface $order */
     Assert::isInstanceOf($order, OrderInterface::class);
     $context = [];
     if (null !== ($customer = $order->getCustomer())) {
         $context['customer'] = $customer;
         $context['groups'] = [$customer->getGroup()];
     }
     if (null !== $order->getChannel()) {
         $context['channel'] = [$order->getChannel()];
     }
     foreach ($order->getItems() as $item) {
         if ($item->isImmutable()) {
             continue;
         }
         $context['quantity'] = $item->getQuantity();
         $item->setUnitPrice($this->priceCalculator->calculate($item->getVariant(), $context));
     }
 }
Esempio n. 5
0
 /**
  * @param GenericEvent $event
  *
  * @throws UnexpectedTypeException
  */
 public function recalculatePrices(GenericEvent $event)
 {
     $order = $event->getSubject();
     if (!$order instanceof OrderInterface) {
         throw new UnexpectedTypeException($order, OrderInterface::class);
     }
     $context = [];
     if (null !== ($customer = $order->getCustomer())) {
         $context['customer'] = $customer;
         $context['groups'] = $customer->getGroups()->toArray();
     }
     if (null !== $order->getChannel()) {
         $context['channel'] = [$order->getChannel()];
     }
     foreach ($order->getItems() as $item) {
         if ($item->isImmutable()) {
             continue;
         }
         $context['quantity'] = $item->getQuantity();
         $item->setUnitPrice($this->priceCalculator->calculate($item->getVariant(), $context));
     }
 }
Esempio n. 6
0
 function it_recalculates_prices_adding_all_context(GenericEvent $event, OrderInterface $order, OrderItemInterface $item, DelegatingCalculatorInterface $priceCalculator, GroupableInterface $customer, ArrayCollection $groups, ChannelInterface $channel, PriceableInterface $variant)
 {
     $event->getSubject()->shouldBeCalled()->willReturn($order);
     $order->getCustomer()->shouldBeCalled()->willReturn($customer);
     $order->getChannel()->shouldBeCalled()->willReturn($channel);
     $order->getItems()->shouldBeCalled()->willReturn([$item]);
     $customer->getGroups()->shouldBeCalled()->willReturn($groups);
     $groups->toArray()->shouldBeCalled()->willReturn(['group1', 'group2']);
     $item->isImmutable()->shouldBeCalled()->willReturn(false);
     $item->getQuantity()->shouldBeCalled()->willReturn(5);
     $item->setUnitPrice(10)->shouldBeCalled();
     $item->getVariant()->shouldBeCalled()->willReturn($variant);
     $priceCalculator->calculate($variant, ['customer' => $customer, 'groups' => ['group1', 'group2'], 'channel' => [$channel], 'quantity' => 5])->shouldBeCalled()->willReturn(10);
     $this->recalculatePrices($event);
 }
Esempio n. 7
0
 /**
  * {@inheritdoc}
  */
 public function resolve(CartItemInterface $item, $data)
 {
     $id = $this->resolveItemIdentifier($data);
     $channel = $this->channelContext->getChannel();
     if (!($product = $this->productRepository->findOneBy(['id' => $id, 'channels' => $channel]))) {
         throw new ItemResolvingException('Requested product was not found.');
     }
     if ($this->restrictedZoneChecker->isRestricted($product)) {
         throw new ItemResolvingException('Selected item is not available in your country.');
     }
     // We use forms to easily set the quantity and pick variant but you can do here whatever is required to create the item.
     $form = $this->formFactory->create('sylius_cart_item', $item, ['product' => $product]);
     $form->submit($data);
     // If our product has no variants, we simply set the master variant of it.
     if (null === $item->getVariant() && !$product->hasVariants()) {
         $item->setVariant($product->getMasterVariant());
     }
     if (null === $item->getVariant() && $product->hasVariants()) {
         throw new ItemResolvingException('Please select variant');
     }
     $variant = $item->getVariant();
     // If all is ok with form, quantity and other stuff, simply return the item.
     if (!$form->isValid() || null === $variant) {
         throw new ItemResolvingException('Submitted form is invalid.');
     }
     $cart = $this->cartProvider->getCart();
     $quantity = $item->getQuantity();
     $context = ['quantity' => $quantity];
     if (null !== ($customer = $cart->getCustomer())) {
         $context['groups'] = $customer->getGroups()->toArray();
     }
     $item->setUnitPrice($this->priceCalculator->calculate($variant, $context));
     foreach ($cart->getItems() as $cartItem) {
         if ($cartItem->equals($item)) {
             $quantity += $cartItem->getQuantity();
             break;
         }
     }
     if (!$this->availabilityChecker->isStockSufficient($variant, $quantity)) {
         throw new ItemResolvingException('Selected item is out of stock.');
     }
     return $item;
 }
Esempio n. 8
0
 /**
  * @param PriceableInterface $priceable
  * @param array $context
  *
  * @return int
  */
 public function calculatePrice(PriceableInterface $priceable, array $context = [])
 {
     return $this->priceCalculator->calculate($priceable, $context);
 }
Esempio n. 9
0
 function it_calculates_price(DelegatingCalculatorInterface $calculator, PriceableInterface $priceable)
 {
     $calculator->calculate($priceable, [])->willReturn(10);
     $this->calculatePrice($priceable)->shouldReturn(10);
 }