Add calculating OrderItemsSubtotal to the Order model

This commit is contained in:
Jakub Tobiasz 2023-04-14 10:42:06 +02:00
parent b6c348ffa1
commit c68fd42d99
No known key found for this signature in database
GPG key ID: 6434250CB3525233
5 changed files with 47 additions and 6 deletions

View file

@ -51,3 +51,6 @@
1. Not passing an instance of `Symfony\Component\PropertyAccess\PropertyAccessorInterface`
to `Sylius\Bundle\CoreBundle\Validator\Constraints\HasEnabledEntityValidator`
as the second argument is deprecated.
1. Class `\Sylius\Bundle\ShopBundle\Calculator\OrderItemsSubtotalCalculator` has been deprecated. Order items subtotal calculation
is now available on the Order model `\Sylius\Component\Core\Model\Order::getItemsSubtotal`.

View file

@ -13,17 +13,23 @@ declare(strict_types=1);
namespace Sylius\Bundle\ShopBundle\Calculator;
use Sylius\Component\Core\Model\Order;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
trigger_deprecation(
'sylius/sylius',
'1.13',
sprintf(
'Class "%s" is deprecated and will be removed in Sylius 2.0. Items subtotal calculations is now available by using %s::getSubtotalItems method.',
OrderItemsSubtotalCalculator::class,
Order::class,
),
);
final class OrderItemsSubtotalCalculator implements OrderItemsSubtotalCalculatorInterface
{
public function getSubtotal(OrderInterface $order): int
{
return array_reduce(
$order->getItems()->toArray(),
static fn (int $subtotal, OrderItemInterface $item): int => $subtotal + $item->getSubtotal(),
0,
);
return $order->getItemsSubtotal();
}
}

View file

@ -335,6 +335,20 @@ class Order extends BaseOrder implements OrderInterface
return $this->getTotalQuantity();
}
public function getItemsSubtotal(): int
{
/** @var array<OrderItemInterface> $items */
$items = $this->getItems()->toArray();
return array_reduce(
$items,
static function (int $subtotal, OrderItemInterface $item): int {
return $subtotal + $item->getSubtotal();
},
0
);
}
public function getCurrencyCode(): ?string
{
return $this->currencyCode;

View file

@ -110,6 +110,8 @@ interface OrderInterface extends
public function getOrderPromotionTotal(): int;
public function getItemsSubtotal(): int;
public function getTokenValue(): ?string;
public function setTokenValue(?string $tokenValue): void;

View file

@ -415,6 +415,22 @@ final class OrderSpec extends ObjectBehavior
$this->getPromotionSubjectCount()->shouldReturn(7);
}
function it_counts_items_subtotal(OrderItemInterface $item1, OrderItemInterface $item2): void
{
$item1->getSubtotal()->willReturn(420);
$item1->getTotal()->willReturn(420);
$item1->setOrder($this)->will(function () {});
$item2->getSubtotal()->willReturn(666);
$item2->getTotal()->willReturn(666);
$item2->setOrder($this)->will(function () {});
$this->addItem($item1);
$this->addItem($item2);
$this->getItemsSubtotal()->shouldReturn(1086);
}
function it_adds_and_removes_promotions(PromotionInterface $promotion): void
{
$this->addPromotion($promotion);