/** * @Transform /^"([^"]+)" variant of product "([^"]+)"$/ */ public function getProductVariantByNameAndProduct($variantName, $productName) { $products = $this->productRepository->findByName($productName, 'en_US'); Assert::eq(1, count($products), sprintf('%d products has been found with name "%s".', count($products), $productName)); $productVariants = $this->productVariantRepository->findByNameAndProduct($variantName, 'en_US', $products[0]); Assert::notEmpty($productVariants, sprintf('Product variant with name "%s" of product "%s" does not exist', $variantName, $productName)); return $productVariants[0]; }
/** * @Transform /^product "([^"]+)"$/ * @Transform /^"([^"]+)" product$/ * @Transform /^"([^"]+)" products$/ * @Transform /^(?:a|an) "([^"]+)"$/ * @Transform :product */ public function getProductByName($productName) { $product = $this->productRepository->findOneByName($productName); if (null === $product) { throw new \InvalidArgumentException(sprintf('Product with name "%s" does not exist', $productName)); } return $product; }
/** * @Transform /^"([^"]+)" variant of product "([^"]+)"$/ */ public function getProductVariantByNameAndProduct($variantName, $productName) { $products = $this->productRepository->findByName($productName, 'en_US'); Assert::eq(1, count($products), sprintf('%d products has been found with name "%s".', count($products), $productName)); $productVariant = $this->productVariantRepository->findOneBy(['name' => $variantName, 'product' => $products[0]]); if (null === $productVariant) { throw new \InvalidArgumentException(sprintf('Product variant with name "%s" of product "%s" does not exist', $variantName, $productName)); } return $productVariant; }
/** * {@inheritdoc} */ public function transform($value) { if (!is_array($value) && !is_null($value)) { throw new UnexpectedTypeException($value, 'array'); } if (empty($value)) { return new ArrayCollection(); } return new ArrayCollection($this->productRepository->findBy(['code' => $value])); }
/** * @Transform /^"([^"]+)" variant of product "([^"]+)"$/ */ public function getProductVariantByNameAndProduct($variantName, $productName) { $product = $this->productRepository->findOneByName($productName); if (null === $product) { throw new \InvalidArgumentException(sprintf('Product with name "%s" does not exist', $productName)); } $productVariant = $this->productVariantRepository->findOneBy(['name' => $variantName, 'object' => $product]); if (null === $productVariant) { throw new \InvalidArgumentException(sprintf('Product variant with name "%s" of product "%s" does not exist', $variantName, $productName)); } return $productVariant; }
/** * @Given /^there is product "([^"]+)" available in ((?:this|that|"[^"]+") channel)$/ */ public function thereIsProductAvailableInGivenChannel($productName, ChannelInterface $channel) { /** @var ProductInterface $product */ $product = $this->productFactory->createNew(); $product->setName($productName); $product->setPrice(0); $product->setDescription('Awesome ' . $productName); $product->addChannel($channel); $this->productRepository->add($product); }
/** * @param ProductInterface $owner * * @return string[] */ private function getAssociatedProductsAsArray(ProductInterface $owner) { $products = $this->productRepository->findBy(['mainTaxon' => $owner->getMainTaxon()]); $products = $this->faker->randomElements($products, 3); $associatedProducts = []; /** @var ProductInterface $product */ foreach ($products as $product) { $associatedProducts[] = $product->getCode(); } return $associatedProducts; }
/** * {@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; }
/** * @param ProductInterface $product */ private function saveProduct(ProductInterface $product) { $this->productRepository->add($product); $this->sharedStorage->set('product', $product); }
/** * @Transform /^product(?:|s) "([^"]+)"$/ * @Transform /^"([^"]+)" product(?:|s)$/ * @Transform /^(?:a|an) "([^"]+)"$/ * @Transform :product * @Transform /^"[^"]+" product$/ */ public function getProductByName($productName) { $products = $this->productRepository->findByName($productName, 'en_US'); Assert::eq(1, count($products), sprintf('%d products has been found with name "%s".', count($products), $productName)); return $products[0]; }
function it_throws_element_not_found_exception_if_product_variant_has_not_been_found(ProductRepositoryInterface $productRepository, ProductVariantRepositoryInterface $productVariantRepository, ProductInterface $product) { $productRepository->findOneByName('T-Shirt')->willReturn($product); $productVariantRepository->findOneBy(['presentation' => 'Han Solo T-Shirt', 'object' => $product])->willReturn(null); $this->shouldThrow(\InvalidArgumentException::class)->during('getProductVariantByNameAndProduct', ['Han Solo T-Shirt', 'T-Shirt']); }
function it_transforms_only_existing_products(ProductRepositoryInterface $productRepository, ProductInterface $bow) { $productRepository->findBy(['code' => ['bow', 'sword']])->willReturn([$bow]); $products = new ArrayCollection([$bow->getWrappedObject()]); $this->transform(['bow', 'sword'])->shouldBeCollection($products); }
/** * @Transform /^product(?:|s) "([^"]+)"$/ * @Transform /^"([^"]+)" product(?:|s)$/ * @Transform /^(?:a|an) "([^"]+)"$/ * @Transform :product */ public function getProductByName($productName) { $product = $this->productRepository->findOneByName($productName); Assert::notNull($product, sprintf('Product with name "%s" does not exist', $productName)); return $product; }
function it_throws_element_not_found_exception_if_product_has_not_been_found(ProductRepositoryInterface $productRepository) { $productRepository->findOneByName('T-Shirt')->willReturn(null); $this->shouldThrow(\InvalidArgumentException::class)->during('getProductByName', ['T-Shirt']); }