Skip to content

Commit

Permalink
Merge pull request #618 from extcode/615-allow-fevariant-to-have-a-price
Browse files Browse the repository at this point in the history
[TASK] Allow FeVariant to have a price
  • Loading branch information
extcode authored Jan 8, 2025
2 parents f47d638 + 12bdd73 commit 320b351
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Classes/Domain/Model/Cart/FeVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* LICENSE file that was distributed with this source code.
*/

class FeVariant
class FeVariant implements FeVariantInterface
{
protected ?Product $product = null;

Expand Down
16 changes: 16 additions & 0 deletions Classes/Domain/Model/Cart/FeVariantInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Extcode\Cart\Domain\Model\Cart;

interface FeVariantInterface
{
public function getId(): string;

public function getVariantData(): array;

public function getTitle(): string;

public function getSku(): string;

public function getValue(): string;
}
8 changes: 8 additions & 0 deletions Classes/Domain/Model/Cart/FeVariantWithPriceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Extcode\Cart\Domain\Model\Cart;

interface FeVariantWithPriceInterface extends FeVariantInterface
{
public function getPrice(): float;
}
24 changes: 19 additions & 5 deletions Classes/Domain/Model/Cart/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Product

protected array $beVariants = [];

protected ?FeVariant $feVariant = null;
protected ?FeVariantInterface $feVariant = null;

protected array $additional = [];

Expand Down Expand Up @@ -69,7 +69,7 @@ public function __construct(
protected TaxClass $taxClass,
protected int $quantity,
protected bool $isNetPrice = false,
?FeVariant $feVariant = null
?FeVariantInterface $feVariant = null
) {
if ($feVariant) {
$this->feVariant = $feVariant;
Expand Down Expand Up @@ -153,7 +153,7 @@ public function changeVariantsQuantity(array $variantQuantity): void
}
}

public function getFeVariant(): ?FeVariant
public function getFeVariant(): ?FeVariantInterface
{
return $this->feVariant;
}
Expand Down Expand Up @@ -554,9 +554,16 @@ protected function calcGross(): void
foreach ($this->beVariants as $variant) {
$sum += $variant->getGross();
}
if ($this->feVariant instanceof FeVariantWithPriceInterface) {
$sum += $this->feVariant->getPrice();
}
$this->gross = $sum;
} else {
$this->gross = $this->getBestPrice() * $this->quantity;
$sum = $this->getBestPrice();
if ($this->feVariant instanceof FeVariantWithPriceInterface) {
$sum += $this->feVariant->getPrice();
}
$this->gross = $sum * $this->quantity;
}
} else {
$this->calcNet();
Expand All @@ -582,9 +589,16 @@ protected function calcNet(): void
foreach ($this->beVariants as $variant) {
$sum += $variant->getNet();
}
if ($this->feVariant instanceof FeVariantWithPriceInterface) {
$sum += $this->feVariant->getPrice();
}
$this->net = $sum;
} else {
$this->net = $this->getBestPrice() * $this->quantity;
$sum = $this->getBestPrice();
if ($this->feVariant instanceof FeVariantWithPriceInterface) {
$sum += $this->feVariant->getPrice();
}
$this->net = $sum * $this->quantity;
}
} else {
$this->calcGross();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Before this the dates in the backend were always in the format `d.m.Y`, now it

Migration
=========

To get the old formatting you need to overwrite the above shown TypoScript
constants:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
.. include:: ../../Includes.rst.txt

===============================================
Feature: #615 - Allow FeVariant to have a price
===============================================

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

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

The default `FeVariant` implementation cannot have a price. In order to be able
to process own `FeVariant`s with a price, the method arguments should use a
`FeVariantInterface` instead.

The `FeVariantWithPriceInterface` extends the `FeVariantInterface` with a
`getPrice()` method.
The price calculation of a product then takes into account whether the
`FeVariant` implements the `FeVariantWithPriceInterface` to add this price as
well.

To create an own `FeVariant` for a `Product`, you have to replace the
`CreateCartFrontendVariants` EventListener.

.. code-block:: yaml
:caption: EXT:my_extension/Configuration/Services.yaml
Extcode\CartProducts\EventListener\Create\CreateCartFrontendVariants: null
MyVendor\MyExtension\EventListener\Create\CreateCartFrontendVariants:
tags:
- name: event.listener
identifier: 'cart-products--create--create-cart-frontend-variants'
event: Extcode\CartProducts\Event\RetrieveProductsFromRequestEvent
after: 'cart-products--create--load-product'
This is very flexible. You can implement `FeVariant`s as checkboxes and add some
surcharge for some options. Another idea is to calculate the price markup
depending on the number of characters entered.How you can use this is up to you.
However, you should test carefully whether the price calculation for the product
price then fits, especially if you use more than one front-end variant with a
price for a product.

Impact
======

No Impact.

.. index:: Frontend, API
20 changes: 20 additions & 0 deletions Documentation/Changelog/9.4/Index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.. include:: ../../Includes.rst.txt

9.4 Changes
===========

**Table of contents**

.. contents::
:local:
:depth: 1

Features
--------

.. toctree::
:maxdepth: 1
:titlesonly:
:glob:

Feature-*
1 change: 1 addition & 0 deletions Documentation/Changelog/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ChangeLog
:maxdepth: 5
:titlesonly:

9.4/Index
9.3/Index
9.1/Index
9.0/Index
Expand Down
4 changes: 2 additions & 2 deletions Documentation/guides.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
interlink-shortcode="extcode/cart"
/>
<project title="Cart"
release="9.3.0"
version="9.3"
release="9.4.0"
version="9.4"
copyright="2018 - 2024"
/>
<inventory id="t3tsref" url="https://docs.typo3.org/typo3cms/TyposcriptReference/"/>
Expand Down
2 changes: 1 addition & 1 deletion ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'title' => 'Cart',
'description' => 'Shopping Cart(s) for TYPO3',
'category' => 'plugin',
'version' => '9.3.0',
'version' => '9.4.0',
'state' => 'stable',
'author' => 'Daniel Gohlke',
'author_email' => '[email protected]',
Expand Down

0 comments on commit 320b351

Please sign in to comment.