Skip to content

Commit

Permalink
Merge pull request #181 from Setono/default-value-configurable-gc
Browse files Browse the repository at this point in the history
Add default value for configurable GiftCard
  • Loading branch information
Roshyo authored Jan 17, 2022
2 parents 598716d + 06a1234 commit 92cbf10
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
2 changes: 2 additions & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"Sylius\\Component\\Core\\Repository\\OrderRepositoryInterface",
"Sylius\\Component\\Core\\Repository\\ProductVariantRepositoryInterface",
"Sylius\\Component\\Core\\Uploader\\ImageUploaderInterface",
"Sylius\\Component\\Currency\\Context\\CurrencyContextInterface",
"Sylius\\Component\\Currency\\Model\\CurrencyInterface",
"Sylius\\Component\\Customer\\Model\\CustomerInterface",
"Sylius\\Component\\Grid\\Definition\\Field",
Expand All @@ -72,6 +73,7 @@
"Sylius\\Component\\Order\\Modifier\\OrderItemQuantityModifierInterface",
"Sylius\\Component\\Order\\Modifier\\OrderModifierInterface",
"Sylius\\Component\\Order\\Processor\\OrderProcessorInterface",
"Sylius\\Component\\Product\\Resolver\\ProductVariantResolverInterface",
"Sylius\\Component\\Promotion\\Checker\\Rule\\RuleCheckerInterface",
"Sylius\\Component\\Promotion\\Model\\PromotionSubjectInterface",
"Sylius\\Component\\User\\Model\\UserInterface"
Expand Down
39 changes: 37 additions & 2 deletions src/Form/Type/AddToCartGiftCardInformationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,43 @@

use Setono\SyliusGiftCardPlugin\Model\ProductInterface;
use Sylius\Bundle\MoneyBundle\Form\Type\MoneyType;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Currency\Context\CurrencyContextInterface;
use Sylius\Component\Product\Resolver\ProductVariantResolverInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Webmozart\Assert\Assert;

final class AddToCartGiftCardInformationType extends AbstractType
{
private string $dataClass;

private array $validationGroups;

public function __construct(string $dataClass, array $validationGroups)
{
private CurrencyContextInterface $currencyContext;

private ProductVariantResolverInterface $productVariantResolver;

private ChannelContextInterface $channelContext;

public function __construct(
string $dataClass,
array $validationGroups,
CurrencyContextInterface $currencyContext,
ProductVariantResolverInterface $productVariantResolver,
ChannelContextInterface $channelContext
) {
$this->dataClass = $dataClass;
$this->validationGroups = $validationGroups;
$this->currencyContext = $currencyContext;
$this->productVariantResolver = $productVariantResolver;
$this->channelContext = $channelContext;
}

public function buildForm(FormBuilderInterface $builder, array $options): void
Expand All @@ -35,8 +55,23 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
return;
}

/** @var ProductVariantInterface|null $variant */
$variant = $this->productVariantResolver->getVariant($product);
$defaultAmount = 0;
if (null !== $variant) {
/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();
Assert::isInstanceOf($channel, ChannelInterface::class);
$channelPricing = $variant->getChannelPricingForChannel($channel);
if (null !== $channelPricing) {
$defaultAmount = $channelPricing->getPrice();
}
}

$form->add('amount', MoneyType::class, [
'label' => 'setono_sylius_gift_card.form.add_to_cart.gift_card_information.amount',
'currency' => $this->currencyContext->getCurrencyCode(),
'data' => $defaultAmount,
]);
});

Expand Down
3 changes: 3 additions & 0 deletions src/Resources/config/services/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
class="Setono\SyliusGiftCardPlugin\Form\Type\AddToCartGiftCardInformationType">
<argument>%setono_sylius_gift_card.order.model.gift_card_information.class%</argument>
<argument>%setono_sylius_gift_card.form.type.add_to_cart_gift_card_information.validation_groups%</argument>
<argument type="service" id="sylius.context.currency"/>
<argument type="service" id="sylius.product_variant_resolver.default"/>
<argument type="service" id="sylius.context.channel"/>
<tag name="form.type"/>
</service>

Expand Down

0 comments on commit 92cbf10

Please sign in to comment.