Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Classes/Domain/Model/Order/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ class Product extends AbstractEntity

protected string $additionalData = '';

protected bool $shippable = true;

protected bool $cancelable = true;

protected bool $returnable = true;

/**
* @var ObjectStorage<ProductAdditional>
*/
Expand Down Expand Up @@ -199,6 +205,45 @@ public function setTitle(string $title): void
$this->title = $title;
}

public function isShippable(): bool
{
return $this->shippable;
}

/**
* Set once when the order is persisted, see BeforePersistOrderProductEvent.
*/
public function setShippable(bool $shippable): void
{
$this->shippable = $shippable;
}

public function isCancelable(): bool
{
return $this->cancelable;
}

/**
* Set once when the order is persisted, see BeforePersistOrderProductEvent.
*/
public function setCancelable(bool $cancelable): void
{
$this->cancelable = $cancelable;
}

public function isReturnable(): bool
{
return $this->returnable;
}

/**
* Set once when the order is persisted, see BeforePersistOrderProductEvent.
*/
public function setReturnable(bool $returnable): void
{
$this->returnable = $returnable;
}

public function addProductAdditional(ProductAdditional $productAdditional): void
{
$this->productAdditional->attach($productAdditional);
Expand Down
114 changes: 114 additions & 0 deletions Classes/Event/Order/BeforePersistOrderProductEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

declare(strict_types=1);

namespace Extcode\Cart\Event\Order;

/*
* This file is part of the package extcode/cart.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

use Extcode\Cart\Domain\Model\Cart\BeVariantInterface;
use Extcode\Cart\Domain\Model\Cart\ProductInterface;
use Extcode\Cart\Domain\Model\Order\Item;
use Extcode\Cart\Domain\Model\Order\Product;

/**
* Dispatched while the order is persisted, once per ordered product (or
* backend variant) before it is stored.
*
* Listeners of product extensions decide whether the ordered product can be
* shipped, canceled and returned. The values are a snapshot at order time,
* they are not changed afterwards when the product changes.
*
* The order product and the cart product are passed for information only,
* change the properties with the setters of this event.
*/
final class BeforePersistOrderProductEvent
{
public function __construct(
private readonly Item $orderItem,
private readonly Product $orderProduct,
private readonly ProductInterface $cartProduct,
private readonly ?BeVariantInterface $beVariant,
private bool $shippable,
private bool $cancelable,
private bool $returnable,
) {
}

public function getOrderItem(): Item
{
return $this->orderItem;
}

public function getOrderProduct(): Product
{
return $this->orderProduct;
}

/**
* The product of the cart, the product of the backend variant if the
* ordered product is a backend variant.
*/
public function getCartProduct(): ProductInterface
{
return $this->cartProduct;
}

/**
* The backend variant of the ordered product, null for products without
* backend variants.
*/
public function getBeVariant(): ?BeVariantInterface
{
return $this->beVariant;
}

public function isShippable(): bool
{
return $this->shippable;
}

/**
* Defaults to true unless the cart product is a virtual product.
*/
public function setShippable(bool $shippable): void
{
$this->shippable = $shippable;
}

public function isCancelable(): bool
{
return $this->cancelable;
}

/**
* Whether the ordered product may be canceled before it is shipped or
* before the service is provided, defaults to true.
*
* Rules depending on the time of the cancellation (e.g. until 14 days
* before an event) do not belong here.
*/
public function setCancelable(bool $cancelable): void
{
$this->cancelable = $cancelable;
}

public function isReturnable(): bool
{
return $this->returnable;
}

/**
* Whether the ordered product may be returned after it was shipped,
* defaults to true.
*/
public function setReturnable(bool $returnable): void
{
$this->returnable = $returnable;
}
}
39 changes: 38 additions & 1 deletion Classes/EventListener/Order/Create/PersistOrder/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
use Extcode\Cart\Domain\Model\Order\ProductAdditional;
use Extcode\Cart\Domain\Repository\Order\ProductAdditionalRepository;
use Extcode\Cart\Domain\Repository\Order\ProductRepository;
use Extcode\Cart\Event\Order\BeforePersistOrderProductEvent;
use Extcode\Cart\Event\Order\PersistOrderEvent;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;

Expand All @@ -32,11 +34,21 @@ class Products

private int $storagePid;

private readonly EventDispatcherInterface $eventDispatcher;

/**
* The event dispatcher is optional, so subclasses calling the constructor
* with the former three arguments keep working.
*
* TODO: 13.0.0 Make the event dispatcher required.
*/
public function __construct(
private readonly PersistenceManager $persistenceManager,
private readonly ProductRepository $productRepository,
private readonly ProductAdditionalRepository $productAdditionalRepository
private readonly ProductAdditionalRepository $productAdditionalRepository,
?EventDispatcherInterface $eventDispatcher = null
) {
$this->eventDispatcher = $eventDispatcher ?? GeneralUtility::makeInstance(EventDispatcherInterface::class);
}

public function __invoke(PersistOrderEvent $event): void
Expand Down Expand Up @@ -78,6 +90,8 @@ protected function addProduct(ProductInterface $cartProduct): void
$orderProduct->setAdditional($cartProduct->getAdditionals());
}

$this->setFulfillmentOptions($orderProduct, $cartProduct);

$orderProduct->setPid($this->storagePid);

$this->productRepository->add($orderProduct);
Expand Down Expand Up @@ -166,6 +180,8 @@ protected function addBeVariant(BeVariantInterface $variant, int $level): void
$orderProduct->setAdditional($cartProduct->getAdditionals());
}

$this->setFulfillmentOptions($orderProduct, $cartProduct, $variant);

if (!$orderProduct->_isDirty()) {
$this->productRepository->add($orderProduct);
}
Expand Down Expand Up @@ -198,4 +214,25 @@ protected function addBeVariant(BeVariantInterface $variant, int $level): void

$this->orderItem->addProduct($orderProduct);
}

private function setFulfillmentOptions(
Product $orderProduct,
ProductInterface $cartProduct,
?BeVariantInterface $beVariant = null
): void {
$event = new BeforePersistOrderProductEvent(
$this->orderItem,
$orderProduct,
$cartProduct,
$beVariant,
!$cartProduct->isVirtualProduct(),
true,
true
);
$this->eventDispatcher->dispatch($event);

$orderProduct->setShippable($event->isShippable());
$orderProduct->setCancelable($event->isCancelable());
$orderProduct->setReturnable($event->isReturnable());
}
}
32 changes: 31 additions & 1 deletion Configuration/TCA/tx_cart_domain_model_order_product.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
'hideTable' => 1,
'types' => [
'1' => [
'showitem' => '--palette--;;product_type_and_id, sku, title, count, --palette--;' . $_LLL . ':tx_cart_domain_model_order_product.price.group;price, product_additional, additional, additional_data',
'showitem' => '--palette--;;product_type_and_id, sku, title, count, --palette--;' . $_LLL . ':tx_cart_domain_model_order_product.price.group;price, --palette--;' . $_LLL . ':tx_cart_domain_model_order_product.properties.group;properties, product_additional, additional, additional_data',
],
],
'palettes' => [
Expand All @@ -38,6 +38,9 @@
'product_type_and_id' => [
'showitem' => 'product_type, product_id',
],
'properties' => [
'showitem' => 'shippable, cancelable, returnable',
],
],
'columns' => [
'product_id' => [
Expand Down Expand Up @@ -170,6 +173,33 @@
],
],
],
'shippable' => [
'exclude' => 0,
'label' => $_LLL . ':tx_cart_domain_model_order_product.shippable',
'config' => [
'type' => 'check',
'readOnly' => 1,
'default' => 1,
],
],
'cancelable' => [
'exclude' => 0,
'label' => $_LLL . ':tx_cart_domain_model_order_product.cancelable',
'config' => [
'type' => 'check',
'readOnly' => 1,
'default' => 1,
],
],
'returnable' => [
'exclude' => 0,
'label' => $_LLL . ':tx_cart_domain_model_order_product.returnable',
'config' => [
'type' => 'check',
'readOnly' => 1,
'default' => 1,
],
],
'additional' => [
'exclude' => 0,
'label' => $_LLL . ':tx_cart_domain_model_order_product.additional',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.. include:: ../../Includes.rst.txt

=====================================================================
Feature: #895 - Shippable, cancelable and returnable ordered products
=====================================================================

See `Issue 895 <https://github.com/extcode/cart/issues/895>`__

Description
===========

An ordered product (``tx_cart_domain_model_order_product``) stores whether it
is shippable, cancelable and returnable in the new fields ``shippable``,
``cancelable`` and ``returnable``. The values are set once when the order is
persisted and are not changed afterwards, even if the product changes later.
The model ``\Extcode\Cart\Domain\Model\Order\Product`` provides them with
``isShippable()``, ``isCancelable()`` and ``isReturnable()``.

Defaults:

* ``shippable``: ``true`` unless the product in the cart is a virtual product
(``ProductInterface::isVirtualProduct()``).
* ``cancelable`` and ``returnable``: ``true``.

The new event ``\Extcode\Cart\Event\Order\BeforePersistOrderProductEvent``
is dispatched once per ordered product (or backend variant) before it is
stored. Product extensions can change the values with ``setShippable()``,
``setCancelable()`` and ``setReturnable()``, e.g. for personalized products
that cannot be returned.

The order details in the backend module mark ordered products that are not
shippable, not cancelable or not returnable.

The fields are the base for partial deliveries, cancellations and returns per
ordered product. Rules that depend on the time (e.g. a ticket can be canceled
until 14 days before the event) do not belong into these fields.

Impact
======

The database schema has to be updated. Products of existing orders are
shippable, cancelable and returnable (default of the database fields).

.. index:: Backend, Database, PHP-API
1 change: 1 addition & 0 deletions Documentation/Changelog/12.8/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Features
~~~~~~~~

* :doc:`#895 - Shippable, cancelable and returnable ordered products <Feature-895-OrderProductProperties>`
* :doc:`#896 - Taxes per tax class of the discounts of an order <Feature-896-OrderDiscountTaxesPerTaxClass>`

Deprecations
Expand Down
Loading
Loading