/** * {@inheritdoc} */ public function create(array $options = []) { $options = $this->optionsResolver->resolve($options); /** @var TaxonInterface $taxon */ $taxon = $this->taxonRepository->findOneBy(['code' => $options['code']]); if (null === $taxon) { $taxon = $this->taxonFactory->createNew(); } $taxon->setCode($options['code']); foreach ($this->getLocales() as $localeCode) { $taxon->setCurrentLocale($localeCode); $taxon->setFallbackLocale($localeCode); $taxon->setName($options['name']); $taxon->setDescription($options['description']); } foreach ($options['children'] as $childOptions) { $taxon->addChild($this->create($childOptions)); } return $taxon; }
/** * {@inheritdoc} */ public function isEligible(PromotionSubjectInterface $subject, array $configuration) { if (!$subject instanceof OrderInterface) { throw new UnexpectedTypeException($subject, OrderInterface::class); } if (!isset($configuration['taxon']) || !isset($configuration['count'])) { return false; } $targetTaxon = $this->taxonRepository->findOneBy(['code' => $configuration['taxon']]); if (null === $targetTaxon) { return false; } $validProducts = 0; foreach ($subject->getItems() as $item) { if (!$item->getProduct()->hasTaxon($targetTaxon)) { continue; } $validProducts += $item->getQuantity(); } return $validProducts >= $configuration['count']; }
/** * {@inheritdoc} */ public function isEligible(PromotionSubjectInterface $subject, array $configuration) { if (!$subject instanceof OrderInterface) { throw new UnexpectedTypeException($subject, OrderInterface::class); } if (!isset($configuration['taxon']) || !isset($configuration['amount'])) { return false; } $targetTaxon = $this->taxonRepository->findOneBy(['code' => $configuration['taxon']]); if (null === $targetTaxon) { return false; } $itemsWithTaxonTotal = 0; /** @var OrderItemInterface $item */ foreach ($subject->getItems() as $item) { if ($item->getProduct()->hasTaxon($targetTaxon)) { $itemsWithTaxonTotal += $item->getTotal(); } } return $itemsWithTaxonTotal >= $configuration['amount']; }
/** * {@inheritdoc} */ public function isEligible(PromotionSubjectInterface $subject, array $configuration) { Assert::isInstanceOf($subject, OrderInterface::class); $channelCode = $subject->getChannel()->getCode(); if (!isset($configuration[$channelCode])) { return false; } $configuration = $configuration[$channelCode]; if (!isset($configuration['taxon']) || !isset($configuration['amount'])) { return false; } $targetTaxon = $this->taxonRepository->findOneBy(['code' => $configuration['taxon']]); if (null === $targetTaxon) { return false; } $itemsWithTaxonTotal = 0; /** @var OrderItemInterface $item */ foreach ($subject->getItems() as $item) { if (!$item->getProduct()->filterProductTaxonsByTaxon($targetTaxon)->isEmpty()) { $itemsWithTaxonTotal += $item->getTotal(); } } return $itemsWithTaxonTotal >= $configuration['amount']; }
function it_returns_false_if_taxon_with_configured_code_does_not_exist(OrderInterface $order, TaxonRepositoryInterface $taxonRepository) { $taxonRepository->findOneBy(['code' => 'bows'])->willReturn(null); $this->isEligible($order, ['taxon' => 'bows', 'count' => 10])->shouldReturn(false); }
function it_returns_false_if_taxon_with_configured_code_cannot_be_found(TaxonRepositoryInterface $taxonRepository, OrderInterface $order) { $taxonRepository->findOneBy(['code' => 'sniper_rifles'])->willReturn(null); $this->isEligible($order, ['taxon' => 'sniper_rifles', 'amount' => 1000])->shouldReturn(false); }
function it_returns_false_if_taxon_with_configured_code_cannot_be_found(ChannelInterface $channel, OrderInterface $order, TaxonRepositoryInterface $taxonRepository) { $order->getChannel()->willReturn($channel); $channel->getCode()->willReturn('WEB_US'); $taxonRepository->findOneBy(['code' => 'sniper_rifles'])->willReturn(null); $this->isEligible($order, ['WEB_US' => ['taxon' => 'sniper_rifles', 'amount' => 1000]])->shouldReturn(false); }
/** * @Transform /^taxon with "([^"]+)" code$/ */ public function getTaxonByCode($code) { $taxon = $this->taxonRepository->findOneBy(['code' => $code]); Assert::notNull($taxon, sprintf('Taxon with code "%s" does not exist.', $code)); return $taxon; }