function it_creates_a_cart_item_and_assigns_a_product_variant(FactoryInterface $decoratedFactory, VariantResolverInterface $variantResolver, OrderItemInterface $cartItem, ProductInterface $product, ProductVariantInterface $productVariant) { $decoratedFactory->createNew()->willReturn($cartItem); $variantResolver->getVariant($product)->willReturn($productVariant); $cartItem->setVariant($productVariant)->shouldBeCalled(); $this->createForProduct($product)->shouldReturn($cartItem); }
/** * @param GenericEvent $event */ public function uploadProductImage(GenericEvent $event) { $subject = $event->getSubject(); Assert::isInstanceOf($subject, ProductInterface::class); if ($subject->isSimple()) { $variant = $this->variantResolver->getVariant($subject); $this->uploadProductVariantImages($variant); } }
/** * @param FormEvent $event */ public function preSetData(FormEvent $event) { /** @var ProductInterface $product */ $product = $event->getData(); Assert::isInstanceOf($product, ProductInterface::class); $form = $event->getForm(); /** Options should be disabled for configurable product if it has at least one defined variant */ $disableOptions = null !== $this->variantResolver->getVariant($product) && false === $product->hasVariants(); $form->add('options', 'sylius_product_option_choice', ['required' => false, 'disabled' => $disableOptions, 'multiple' => true, 'label' => 'sylius.form.product.options']); }
function it_uses_image_uploader_to_upload_images_for_simple_product(GenericEvent $event, ProductInterface $product, ProductVariantInterface $variant, ImageInterface $image, ImageUploaderInterface $uploader, VariantResolverInterface $variantResolver) { $event->getSubject()->willReturn($product); $product->isSimple()->willReturn(true); $variantResolver->getVariant($product)->willReturn($variant); $variant->getImages()->willReturn([$image]); $image->hasFile()->willReturn(true); $image->getPath()->willReturn('some_path'); $uploader->upload($image)->shouldBeCalled(); $this->uploadProductImage($event); }
/** * @param string $productName * @param int $price * * @return ProductInterface */ private function createProduct($productName, $price = 0) { /** @var ProductInterface $product */ $product = $this->productFactory->createWithVariant(); $product->setName($productName); $product->setCode($this->convertToCode($productName)); $variant = $this->defaultVariantResolver->getVariant($product); $variant->setPrice($price); $variant->setCode($product->getCode()); return $product; }
/** * {@inheritdoc} */ public function resolve(CartItemInterface $item, $data) { $id = $this->resolveItemIdentifier($data); $channel = $this->channelContext->getChannel(); if (!($product = $this->productRepository->findOneByIdAndChannel($id, $channel))) { throw new ItemResolvingException('Requested product was not found.'); } // 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() && 1 === $product->getVariants()->count()) { $item->setVariant($this->variantResolver->getVariant($product)); } if (null === $item->getVariant() && 1 > $product->getVariants()->count()) { 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->cartContext->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; }
/** * @Given the customer bought a single :product using :coupon coupon */ public function theCustomerBoughtSingleUsing(ProductInterface $product, CouponInterface $coupon) { $order = $this->addProductVariantToOrder($this->variantResolver->getVariant($product), $product->getPrice()); $order->setPromotionCoupon($coupon); $this->objectManager->flush(); }
/** * {@inheritdoc} */ public function createForProduct(ProductInterface $product) { $cartItem = $this->createNew(); $cartItem->setVariant($this->variantResolver->getVariant($product)); return $cartItem; }
/** * @Then the order item with product :product should not exist */ public function orderItemShouldNotExistInTheRegistry(ProductInterface $product) { $orderItems = $this->orderItemRepository->findBy(['variant' => $this->variantResolver->getVariant($product)]); Assert::same($orderItems, []); }