Skip to content

Commit

Permalink
bug #11820 [ChannelPricing] Ability to remove price on channel pricin…
Browse files Browse the repository at this point in the history
…g when channel is disabled (AdamKasp, Tomanhez)

This PR was merged into the 1.8 branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | 1.8
| Bug fix?        | yes
| New feature?    | yes
| BC breaks?      | no
| Deprecations?   | no
| License         | MIT



Commits
-------

fb08419 [Behat] removing product price for disabled channel
dc4bcef Fix channel pricing type
b116c7c Fixes for tests and FormType
  • Loading branch information
GSadee committed Sep 9, 2020
2 parents d7b7054 + b116c7c commit 54a5840
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@managing_product_variants
Feature: Removing a product variant's price from disabled channel
In order to have only valid prices on enabled channels
As an Administrator
I want to be able to remove price from disabled channels

Background:
Given the store operates on a channel named "Web-US" in "USD" currency
And the store operates on another channel named "Web-GB" in "USD" currency
And the store has a "PHP Mug" configurable product
And this product has "Medium PHP Mug" variant priced at "$20" in "Web-US" channel
And "Medium PHP Mug" variant priced at "$25" in "Web-GB" channel
And the channel "Web-GB" has been disabled
And this product is disabled in "Web-GB" channel
And I am logged in as an administrator

@ui
Scenario: Removing a product variant 's price from disabled channel
When I want to modify the "Medium PHP Mug" product variant
And I remove its price for "Web-GB" channel
And I save my changes
Then I should not have configured price for "Web-GB" channel
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@managing_products
Feature: Removing a product's price from disabled channel
In order to have only valid prices on enabled channels
As an Administrator
I want to be able to remove price from disabled channels

Background:
Given the store operates on a channel named "Web-US" in "USD" currency
And the store operates on another channel named "Web-GB" in "USD" currency
And the store has a product "Dice Brewing" priced at "$10.00" in "Web-US" channel
And this product is also priced at "$5.00" in "Web-GB" channel
And the channel "Web-GB" has been disabled
And this product is disabled in "Web-GB" channel
And I am logged in as an administrator

@ui
Scenario: Removing a product's price from disabled channel
When I want to modify the "Dice Brewing" product
And I remove its price for "Web-GB" channel
And I save my changes
Then I should not have configured price for "Web-GB" channel
23 changes: 23 additions & 0 deletions src/Sylius/Behat/Context/Setup/ProductContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,19 @@ public function theProductHasVariantPricedAt(
);
}

/**
* @Given /^("[^"]+" variant) priced at ("[^"]+") in ("[^"]+" channel)$/
*/
public function variantPricedAtInChannel(
ProductVariantInterface $productVariant,
int $price,
ChannelInterface $channel
): void {
$productVariant->addChannelPricing($this->createChannelPricingForChannel($price, $channel));

$this->sharedStorage->set('variant', $productVariant);
}

/**
* @Given /^the (product "[^"]+") has(?:| a| an) "([^"]+)" variant$/
* @Given /^(this product) has(?:| a| an) "([^"]+)" variant$/
Expand Down Expand Up @@ -1009,6 +1022,16 @@ public function allVariantsOfThisProductAreDisabled(ProductInterface $product):
$this->objectManager->flush();
}

/**
* @Given /^(this product) is disabled in ("[^"]+" channel)$/
*/
public function thisProductIsDisabledInChannel(ProductInterface $product, ChannelInterface $channel): void
{
$product->removeChannel($channel);

$this->objectManager->flush();
}

private function getPriceFromString(string $price): int
{
return (int) round((float) str_replace(['€', '£', '$'], '', $price) * 100, 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ public function iSetItsPriceTo(?string $price = null, ?ChannelInterface $channel
$this->createPage->specifyPrice($price ?? '', $channel ?? $this->sharedStorage->get('channel'));
}

/**
* @When I remove its price for :channel channel
*/
public function iRemoveItsPriceForChannel(ChannelInterface $channel): void
{
$this->iSetItsPriceTo('', $channel);
}

/**
* @When /^I set its original price to "(?:€|£|\$)([^"]+)" for ("([^"]+)" channel)$/
*/
Expand Down Expand Up @@ -543,6 +551,19 @@ public function iShouldBeNotifiedThatVariantsCannotBeGeneratedFromOptionsWithout
$this->notificationChecker->checkNotification('Cannot generate variants for a product without options values', NotificationType::failure());
}

/**
* @Then I should not have configured price for :channel channel
*/
public function iShouldNotHaveConfiguredPriceForChannel(ChannelInterface $channel): void
{
/** @var ProductVariantInterface $product */
$productVariant = $this->sharedStorage->get('variant');

$this->updatePage->open(['productId' => $productVariant->getProduct()->getId(), 'id' => $productVariant->getId()]);

Assert::same($this->updatePage->getPriceForChannel($channel), '');
}

/**
* @Then I should see the :optionName option as :valueName
*/
Expand Down
27 changes: 27 additions & 0 deletions src/Sylius/Behat/Context/Ui/Admin/ManagingProductsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,17 @@ public function theProductShouldNotHaveAnAssociationWithProduct(
Assert::false($this->updateSimpleProductPage->hasAssociatedProduct($productName, $productAssociationType));
}

/**
* @Then I should be notified that original price can not be defined without price
*/
public function iShouldBeNotifiedThatOriginalPriceCanNotBeDefinedWithoutPrice(): void
{
Assert::same(
$this->createSimpleProductPage->getChannelPricingValidationMessage(),
'Original price can not be defined without price'
);
}

/**
* @Then I should be notified that simple product code has to be unique
*/
Expand Down Expand Up @@ -914,6 +925,14 @@ public function iSetThePositionOfTo(string $productName, string $position): void
$this->indexPerTaxonPage->setPositionOfProduct($productName, $position);
}

/**
* @When /^I remove its price for ("[^"]+" channel)$/
*/
public function iRemoveItsPriceForChannel(ChannelInterface $channel): void
{
$this->iSetItsPriceTo('', $channel);
}

/**
* @Then this product should( still) have slug :value in :language
*/
Expand Down Expand Up @@ -1109,6 +1128,14 @@ public function thisProductShouldBeEnabledAlongWithItsVariant(ProductInterface $
Assert::true($this->variantUpdatePage->isEnabled());
}

/**
* @Then I should not have configured price for :channel channel
*/
public function iShouldNotHaveConfiguredPriceForChannel(ChannelInterface $channel): void
{
Assert::same($this->updateSimpleProductPage->getPriceForChannel($channel), '');
}

/**
* @param string $element
* @param string $value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,29 @@
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ChannelPricingInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class ChannelPricingType extends AbstractResourceType
{
/** @var RepositoryInterface */
private $channelPricingRepository;

public function __construct(
string $dataClass,
array $validationGroups,
?RepositoryInterface $channelPricingRepository = null
) {
parent::__construct($dataClass, $validationGroups);

$this->channelPricingRepository = $channelPricingRepository;
}

public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
Expand All @@ -48,6 +63,19 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
return;
}

/** @var ChannelInterface $channel */
$channel = $options['channel'];

if (!$channel->isEnabled() && $channelPricing->getPrice() === null) {
$event->setData(null);

if ($channelPricing->getId() !== null) {
$this->channelPricingRepository->remove($channelPricing);
}

return;
}

$channelPricing->setChannelCode($options['channel']->getCode());
$channelPricing->setProductVariant($options['product_variant']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@
<service id="sylius.form.type.channel_pricing" class="Sylius\Bundle\CoreBundle\Form\Type\Product\ChannelPricingType">
<argument>%sylius.model.channel_pricing.class%</argument>
<argument>%sylius.form.type.channel_pricing.validation_groups%</argument>
<argument type="service" id="sylius.repository.channel_pricing" />
<tag name="form.type" />
</service>
<service id="sylius.form.type.channels_collection" class="Sylius\Bundle\CoreBundle\Form\Type\ChannelCollectionType">
Expand Down

0 comments on commit 54a5840

Please sign in to comment.