Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 28e9cb6

Browse files
committed
Apply CP on variant and change the rule name
1 parent a1378c9 commit 28e9cb6

18 files changed

Lines changed: 72 additions & 103 deletions

features/promotion/applying_catalog_promotions/applying_catalog_promotions_for_taxons.feature renamed to features/promotion/applying_catalog_promotions/applying_catalog_promotions_for_variant.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@applying_catalog_promotions
2-
Feature: Applying catalog promotions for taxons
2+
Feature: Applying catalog promotions for variants
33
In order to be attracted to products
44
As a Customer
55
I want to see discounted products in the catalog

src/Sylius/Behat/Context/Api/Admin/ManagingCatalogPromotionsContext.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public function iBrowseCatalogPromotions(): void
193193
public function iAddTheRuleConfiguredWithProductAnd(ProductVariantInterface $firstVariant, ProductVariantInterface $secondVariant): void
194194
{
195195
$rules = [[
196-
'type' => CatalogPromotionRuleInterface::TYPE_CONTAINS_VARIANTS,
196+
'type' => CatalogPromotionRuleInterface::TYPE_FOR_VARIANTS,
197197
'configuration' => [
198198
$this->iriConverter->getIriFromItem($firstVariant),
199199
$this->iriConverter->getIriFromItem($secondVariant),
@@ -210,7 +210,7 @@ public function iWantCatalogPromotionToBeAppliedOn(CatalogPromotionInterface $ca
210210
{
211211
$this->client->buildUpdateRequest($catalogPromotion->getCode());
212212
$rules = [[
213-
'type' => CatalogPromotionRuleInterface::TYPE_CONTAINS_VARIANTS,
213+
'type' => CatalogPromotionRuleInterface::TYPE_FOR_VARIANTS,
214214
'configuration' => [
215215
$this->iriConverter->getIriFromItem($productVariant),
216216
],

src/Sylius/Behat/Context/Api/Shop/CartContext.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ public function iShouldHaveEmptyCart(string $tokenValue): void
603603
public function iShouldBeUnableToAddItToTheCart(): void
604604
{
605605
/** @var ProductVariantInterface $productVariant */
606-
$productVariant = $this->sharedStorage->get('productVariant');
606+
$productVariant = $this->sharedStorage->get('product_variant');
607607

608608
$tokenValue = $this->pickupCart();
609609
$this->putProductVariantToCart($productVariant, $tokenValue);

src/Sylius/Behat/Context/Api/Shop/ProductContext.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function iOpenProductPage(ProductInterface $product): void
6060
{
6161
$this->client->show($product->getCode());
6262
$this->sharedStorage->set('product', $product);
63-
$this->sharedStorage->set('productVariant', current($product->getVariants()->getValues()));
63+
$this->sharedStorage->set('product_variant', current($product->getVariants()->getValues()));
6464
}
6565

6666
/**
@@ -116,7 +116,7 @@ public function iShouldSeeTheProduct(string $name): void
116116
public function iShouldSeeItIsOutOfStock(): void
117117
{
118118
/** @var ProductVariantInterface $productVariant */
119-
$productVariant = $this->sharedStorage->get('productVariant');
119+
$productVariant = $this->sharedStorage->get('product_variant');
120120

121121
$variantResponse = $this->client->showByIri($this->iriConverter->getIriFromItem($productVariant));
122122

@@ -152,12 +152,12 @@ public function iShouldSeeTheProductPrice(int $price): void
152152
*/
153153
public function iShouldSeeTheProductOriginalPrice(int $originalPrice): void
154154
{
155-
/** @var ProductInterface $checkedProduct */
156-
$checkedProduct = $this->sharedStorage->get('productVariant');
157-
$product = $this->responseChecker->getResponseContent($this->client->getLastResponse());
155+
/** @var ProductVariantInterface $checkedVariant */
156+
$checkedVariant = $this->sharedStorage->get('product_variant');
157+
$variant = $this->responseChecker->getResponseContent($this->client->getLastResponse());
158158

159-
Assert::same($product['originalPrice'], $originalPrice);
160-
Assert::same($product['code'], $checkedProduct->getCode());
159+
Assert::same($variant['originalPrice'], $originalPrice);
160+
Assert::same($variant['code'], $checkedVariant->getCode());
161161
}
162162

163163
/**

src/Sylius/Behat/Context/Setup/CatalogPromotionContext.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function itWillBeAppliedOnVariant(CatalogPromotionInterface $catalogPromo
8181
{
8282
$catalogPromotionRule = new CatalogPromotionRule();
8383

84-
$catalogPromotionRule->setType(CatalogPromotionRule::TYPE_CONTAINS_VARIANTS);
84+
$catalogPromotionRule->setType(CatalogPromotionRule::TYPE_FOR_VARIANTS);
8585
$catalogPromotionRule->setConfiguration([$variant->getCode()]);
8686

8787
$catalogPromotion->addRule($catalogPromotionRule);

src/Sylius/Bundle/CoreBundle/Applicator/CatalogPromotionApplicator.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,11 @@
1414
namespace Sylius\Bundle\CoreBundle\Applicator;
1515

1616
use Sylius\Component\Core\Model\ChannelPricingInterface;
17-
use Sylius\Component\Core\Model\ProductInterface;
1817
use Sylius\Component\Core\Model\ProductVariantInterface;
1918

2019
final class CatalogPromotionApplicator implements CatalogPromotionApplicatorInterface
2120
{
22-
public function applyPercentageDiscount(ProductInterface $product, float $discount): void
23-
{
24-
/** @var ProductVariantInterface $variant */
25-
foreach ($product->getVariants() as $variant) {
26-
$this->lowerVariantPricesForAllChannels($variant, $discount);
27-
}
28-
}
29-
30-
private function lowerVariantPricesForAllChannels(ProductVariantInterface $variant, float $discount): void
21+
public function applyPercentageDiscount(ProductVariantInterface $variant, float $discount): void
3122
{
3223
/** @var ChannelPricingInterface $channelPricing */
3324
foreach ($variant->getChannelPricings() as $channelPricing) {

src/Sylius/Bundle/CoreBundle/Applicator/CatalogPromotionApplicatorInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
namespace Sylius\Bundle\CoreBundle\Applicator;
1515

16-
use Sylius\Component\Core\Model\ProductInterface;
16+
use Sylius\Component\Core\Model\ProductVariantInterface;
1717

1818
interface CatalogPromotionApplicatorInterface
1919
{
20-
public function applyPercentageDiscount(ProductInterface $product, float $discount): void;
20+
public function applyPercentageDiscount(ProductVariantInterface $variant, float $discount): void;
2121
}

src/Sylius/Bundle/CoreBundle/Processor/DummyCatalogPromotionProcessor.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,37 @@
1717
use Sylius\Bundle\CoreBundle\Applicator\CatalogPromotionApplicatorInterface;
1818
use Sylius\Component\Core\Model\CatalogPromotionInterface;
1919
use Sylius\Component\Core\Model\ProductInterface;
20-
use Sylius\Component\Core\Provider\CatalogPromotionProductsProviderInterface;
20+
use Sylius\Component\Core\Model\ProductVariantInterface;
21+
use Sylius\Component\Core\Provider\CatalogPromotionVariantsProviderInterface;
2122

2223
final class DummyCatalogPromotionProcessor implements CatalogPromotionProcessorInterface
2324
{
24-
private CatalogPromotionProductsProviderInterface $catalogPromotionProductsProvider;
25+
private CatalogPromotionVariantsProviderInterface $catalogPromotionVariantsProvider;
2526

2627
private CatalogPromotionApplicatorInterface $catalogPromotionApplicator;
2728

2829
private EntityManagerInterface $entityManager;
2930

3031
public function __construct(
31-
CatalogPromotionProductsProviderInterface $catalogPromotionProductsProvider,
32+
CatalogPromotionVariantsProviderInterface $catalogPromotionVariantsProvider,
3233
CatalogPromotionApplicatorInterface $catalogPromotionApplicator,
3334
EntityManagerInterface $entityManager
3435
) {
35-
$this->catalogPromotionProductsProvider = $catalogPromotionProductsProvider;
36+
$this->catalogPromotionVariantsProvider = $catalogPromotionVariantsProvider;
3637
$this->catalogPromotionApplicator = $catalogPromotionApplicator;
3738
$this->entityManager = $entityManager;
3839
}
3940

4041
public function process(CatalogPromotionInterface $catalogPromotion): void
4142
{
42-
$products = $this->catalogPromotionProductsProvider->provideEligibleProducts($catalogPromotion);
43-
if (empty($products)) {
43+
$variants = $this->catalogPromotionVariantsProvider->provideEligibleVariants($catalogPromotion);
44+
if (empty($variants)) {
4445
return;
4546
}
4647

47-
/** @var ProductInterface $product */
48-
foreach ($products as $product) {
49-
$this->catalogPromotionApplicator->applyPercentageDiscount($product, 0.5);
48+
/** @var ProductVariantInterface $variant */
49+
foreach ($variants as $variant) {
50+
$this->catalogPromotionApplicator->applyPercentageDiscount($variant, 0.5);
5051
}
5152

5253
$this->entityManager->flush();

src/Sylius/Bundle/CoreBundle/Provider/CatalogPromotionProductsProvider.php renamed to src/Sylius/Bundle/CoreBundle/Provider/CatalogPromotionVariantsProvider.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
use Sylius\Component\Core\Model\CatalogPromotionInterface;
1717
use Sylius\Component\Core\Model\ProductInterface;
1818
use Sylius\Component\Core\Model\ProductVariantInterface;
19-
use Sylius\Component\Core\Provider\CatalogPromotionProductsProviderInterface;
19+
use Sylius\Component\Core\Provider\CatalogPromotionVariantsProviderInterface;
2020
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
2121
use Sylius\Component\Promotion\Model\CatalogPromotionRuleInterface;
2222

23-
final class CatalogPromotionProductsProvider implements CatalogPromotionProductsProviderInterface
23+
final class CatalogPromotionVariantsProvider implements CatalogPromotionVariantsProviderInterface
2424
{
2525
private ProductVariantRepositoryInterface $productVariantRepository;
2626

@@ -29,22 +29,22 @@ public function __construct(ProductVariantRepositoryInterface $productVariantRep
2929
$this->productVariantRepository = $productVariantRepository;
3030
}
3131

32-
public function provideEligibleProducts(CatalogPromotionInterface $catalogPromotion): array
32+
public function provideEligibleVariants(CatalogPromotionInterface $catalogPromotion): array
3333
{
34-
$products = [];
34+
$variants = [];
3535

3636
/** @var CatalogPromotionRuleInterface $rule */
3737
foreach ($catalogPromotion->getRules() as $rule) {
3838
$configuration = $rule->getConfiguration();
3939

4040
/** We can do that for now, as we have only one rule */
41-
$products = $this->getVariantsProducts($configuration, $products);
41+
$variants = $this->getVariantsProducts($configuration, $variants);
4242
}
4343

44-
return $products;
44+
return $variants;
4545
}
4646

47-
private function getVariantsProducts(array $configuration, array $products): array
47+
private function getVariantsProducts(array $configuration, array $variants): array
4848
{
4949
/** @var string $variantCode */
5050
foreach ($configuration as $variantCode) {
@@ -54,13 +54,11 @@ private function getVariantsProducts(array $configuration, array $products): arr
5454
continue;
5555
}
5656

57-
/** @var ProductInterface $product */
58-
$product = $variant->getProduct();
59-
if (!in_array($product, $products)) {
60-
$products[] = $product;
57+
if (!in_array($variant, $variants)) {
58+
$variants[] = $variant;
6159
}
6260
}
6361

64-
return $products;
62+
return $variants;
6563
}
6664
}

src/Sylius/Bundle/CoreBundle/Resources/config/services.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@
254254
/>
255255

256256
<service
257-
id="Sylius\Component\Core\Provider\CatalogPromotionProductsProviderInterface"
258-
class="Sylius\Bundle\CoreBundle\Provider\CatalogPromotionProductsProvider"
257+
id="Sylius\Component\Core\Provider\CatalogPromotionVariantsProviderInterface"
258+
class="Sylius\Bundle\CoreBundle\Provider\CatalogPromotionVariantsProvider"
259259
>
260260
<argument type="service" id="sylius.repository.product_variant" />
261261
</service>
@@ -264,7 +264,7 @@
264264
id="Sylius\Bundle\CoreBundle\Processor\CatalogPromotionProcessorInterface"
265265
class="Sylius\Bundle\CoreBundle\Processor\DummyCatalogPromotionProcessor"
266266
>
267-
<argument type="service" id="Sylius\Component\Core\Provider\CatalogPromotionProductsProviderInterface" />
267+
<argument type="service" id="Sylius\Component\Core\Provider\CatalogPromotionVariantsProviderInterface" />
268268
<argument type="service" id="Sylius\Bundle\CoreBundle\Applicator\CatalogPromotionApplicatorInterface" />
269269
<argument type="service" id="sylius.manager.product" />
270270
</service>

0 commit comments

Comments
 (0)