Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow defining custom amounts for gift cards #154

Merged
merged 12 commits into from
Sep 15, 2021
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ $bundles = [

You will find the templates you need to override in the [test application](https://github.com/Setono/SyliusGiftCardPlugin/tree/master/tests/Application/templates).

### Extend `Product`, `Order`, `OrderRepository`, and `CustomerRepository`
### Extend entities

**Extend `Product`**
```php
Expand Down Expand Up @@ -150,6 +150,31 @@ class Order extends BaseOrder implements SetonoSyliusGiftCardPluginOrderInterfac
}
```

**Extend `OrderItem`**

```php
<?php

# src/Entity/Order/OrderItem.php

declare(strict_types=1);

namespace App\Entity\Order;

use Doctrine\ORM\Mapping as ORM;
use Setono\SyliusGiftCardPlugin\Model\OrderItemTrait as SetonoSyliusGiftCardOrderItemTrait;
use Sylius\Component\Core\Model\OrderItem as BaseOrderItem;

/**
* @ORM\Entity
* @ORM\Table(name="sylius_order_item_unit")
*/
class OrderItemUnit extends BaseOrderItem
{
use SetonoSyliusGiftCardOrderItemTrait;
}
```

**Extend `OrderItemUnit`**

```php
Expand Down Expand Up @@ -234,6 +259,9 @@ sylius_order:
classes:
model: App\Entity\Order\Order
repository: App\Doctrine\ORM\OrderRepository
order_item:
classes:
model: App\Entity\Order\OrderItem
order_item_unit:
classes:
model: App\Entity\Order\OrderItemUnit
Expand Down
25 changes: 25 additions & 0 deletions UPGRADE-0.12.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# UPGRADE FROM `0.11` TO `0.12`

## Gift cards are now configurable

From now on, Gift cards can have a custom amount given by the customer, and a custom message that can later be used to
send emails for example

1. Added an override of `Sylius\Bundle\OrderBundle\Controller\AddToCardCommand` so if your application was referencing
this class instead of the interface, you should change it. Please also note that a new parameter
`setono_sylius_gift_card.order.model.add_to_cart_command.class` has been introduced to allow an easier override

1. Added an override of `Sylius\Component\Core\Model\OrderItem` with a new Trait:
`Setono\SyliusGiftCardPlugin\Model\OrderItemTrait`. See [Installation](/README.md#Extend entities) for procedure

1. Added property `giftCardAmountConfigurable` to `Product`. Override the template `@SyliusAdmin/Product/Tab/_details.html.twig`
to add
```twig
{{ form_row(form.giftCardAmountConfigurable) }}
```

1. Removed method `create(OrderInterface $order): void` from `Setono\SyliusGiftCardPlugin\Operator\OrderGiftCardOperatorInterface`
and its associate SM callback on payment

1. Added method `associateToCustomer(OrderInterface $order): void` to `Setono\SyliusGiftCardPlugin\Operator\OrderGiftCardOperatorInterface`
along with a sm callback on checkout complete
6 changes: 6 additions & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"Sylius\\Bundle\\ApiBundle\\Context\\UserContextInterface",
"Sylius\\Bundle\\ApiBundle\\DataTransformer\\CommandDataTransformerInterface",
"Sylius\\Bundle\\CoreBundle\\Application\\SyliusPluginTrait",
"Sylius\\Bundle\\CoreBundle\\Form\\Type\\Order\\AddToCartType",
"Sylius\\Bundle\\ChannelBundle\\Form\\Type\\ChannelChoiceType",
"Sylius\\Bundle\\CoreBundle\\Application\\SyliusPluginTrait",
"Sylius\\Bundle\\CoreBundle\\Fixture\\AbstractResourceFixture",
Expand All @@ -28,6 +29,9 @@
"Sylius\\Bundle\\CoreBundle\\Fixture\\OptionsResolver\\LazyOption",
"Sylius\\Bundle\\CoreBundle\\Form\\Type\\ImageType",
"Sylius\\Bundle\\LocaleBundle\\Form\\Type\\LocaleChoiceType",
"Sylius\\Bundle\\MoneyBundle\\Form\\Type\\MoneyType",
"Sylius\\Bundle\\OrderBundle\\Controller\\AddToCartCommandInterface",
"Sylius\\Bundle\\OrderBundle\\Factory\\AddToCartCommandFactoryInterface",
"Sylius\\Bundle\\ProductBundle\\Form\\Type\\ProductType",
"Sylius\\Bundle\\UiBundle\\Menu\\Event\\MenuBuilderEvent",
"Sylius\\Component\\Channel\\Context\\ChannelContextInterface",
Expand All @@ -42,6 +46,7 @@
"Sylius\\Component\\Core\\Model\\ImageInterface",
"Sylius\\Component\\Core\\Model\\ImagesAwareInterface",
"Sylius\\Component\\Core\\Model\\OrderInterface",
"Sylius\\Component\\Core\\Model\\OrderItemInterface",
"Sylius\\Component\\Core\\Model\\OrderItemUnitInterface",
"Sylius\\Component\\Core\\Model\\ProductInterface",
"Sylius\\Component\\Core\\Model\\ShopUserInterface",
Expand All @@ -60,6 +65,7 @@
"Sylius\\Component\\Order\\Context\\CartContextInterface",
"Sylius\\Component\\Order\\Factory\\AdjustmentFactoryInterface",
"Sylius\\Component\\Order\\Model\\OrderInterface",
"Sylius\\Component\\Order\\Model\\OrderItemInterface",
"Sylius\\Component\\Order\\Processor\\OrderProcessorInterface",
"Sylius\\Component\\Promotion\\Checker\\Rule\\RuleCheckerInterface",
"Sylius\\Component\\Promotion\\Model\\PromotionSubjectInterface",
Expand Down
139 changes: 0 additions & 139 deletions spec/Operator/OrderGiftCardOperatorSpec.php

This file was deleted.

27 changes: 16 additions & 11 deletions src/Factory/GiftCardFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,32 @@ public function createFromOrderItemUnit(OrderItemUnitInterface $orderItemUnit):
{
/** @var OrderInterface|null $order */
$order = $orderItemUnit->getOrderItem()->getOrder();

Assert::isInstanceOf($order, OrderInterface::class);

/** @var ChannelInterface|null $channel */
$channel = $order->getChannel();

Assert::isInstanceOf($channel, ChannelInterface::class);

$currencyCode = $order->getCurrencyCode();
Assert::notNull($currencyCode);

/** @var CustomerInterface|null $customer */
$customer = $order->getCustomer();
Assert::isInstanceOf($customer, CustomerInterface::class);

$giftCard = $this->createNew();
$giftCard = $this->createFromOrderItemUnitAndCart($orderItemUnit, $order);
$giftCard->setCustomer($customer);

return $giftCard;
}

public function createFromOrderItemUnitAndCart(
OrderItemUnitInterface $orderItemUnit,
OrderInterface $cart
): GiftCardInterface {
$channel = $cart->getChannel();
Assert::isInstanceOf($channel, ChannelInterface::class);
$currencyCode = $cart->getCurrencyCode();
Assert::notNull($currencyCode);

$giftCard = $this->createNew();
$giftCard->setOrderItemUnit($orderItemUnit);
$giftCard->setChannel($channel);
$giftCard->setAmount($orderItemUnit->getTotal());
$giftCard->setCurrencyCode($currencyCode);
$giftCard->setChannel($channel);
$giftCard->disable();

return $giftCard;
Expand Down
6 changes: 6 additions & 0 deletions src/Factory/GiftCardFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Setono\SyliusGiftCardPlugin\Model\GiftCardInterface;
use Setono\SyliusGiftCardPlugin\Model\OrderItemUnitInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

interface GiftCardFactoryInterface extends FactoryInterface
Expand All @@ -16,4 +17,9 @@ public function createNew(): GiftCardInterface;
public function createForChannel(ChannelInterface $channel): GiftCardInterface;

public function createFromOrderItemUnit(OrderItemUnitInterface $orderItemUnit): GiftCardInterface;

public function createFromOrderItemUnitAndCart(
OrderItemUnitInterface $orderItemUnit,
OrderInterface $cart
): GiftCardInterface;
}
Loading