Пример #1
0
 /**
  * @param GenericEvent $event
  */
 public function handleRestrictedZone(GenericEvent $event)
 {
     $cart = $event->getSubject();
     if (!$cart instanceof CartInterface) {
         $cart = $this->cartProvider->getCart();
     }
     $removed = false;
     foreach ($cart->getItems() as $item) {
         if ($this->restrictedZoneChecker->isRestricted($product = $item->getProduct(), $cart->getShippingAddress())) {
             $cart->removeItem($item);
             $removed = true;
             $this->session->getBag('flashes')->add('error', $this->translator->trans('sylius.cart.restricted_zone_removal', ['%product%' => $product->getName()], 'flashes'));
         }
     }
     if ($removed) {
         $this->cartManager->persist($cart);
         $this->cartManager->flush();
     }
 }
Пример #2
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;
 }
Пример #3
0
 /**
  * @param ProductInterface $product
  *
  * @return Boolean
  */
 public function isRestricted(ProductInterface $product)
 {
     return $this->restrictedZoneChecker->isRestricted($product);
 }