1212 */
1313
1414use Extcode \Cart \Domain \Finisher \Form \AddToCartFinisherInterface ;
15- use Extcode \Cart \Domain \Model \Cart \BeVariant ;
1615use Extcode \Cart \Domain \Model \Cart \Cart ;
1716use Extcode \Cart \Domain \Model \Cart \FeVariant ;
18- use Extcode \Cart \Domain \Model \Cart \Product ;
19- use Extcode \CartEvents \Domain \Model \EventDate ;
20- use Extcode \CartEvents \Domain \Model \PriceCategory ;
21- use Extcode \CartEvents \Domain \Repository \EventDateRepository ;
22- use Extcode \CartEvents \Domain \Repository \PriceCategoryRepository ;
17+ use Extcode \CartEvents \Domain \Model \Cart \ProductFactoryInterface ;
2318use TYPO3 \CMS \Core \Configuration \ExtensionConfiguration ;
2419use TYPO3 \CMS \Core \Utility \GeneralUtility ;
2520
2621class AddToCartFinisher implements AddToCartFinisherInterface
2722{
2823 protected Cart $ cart ;
2924
30- protected EventDate $ eventDate ;
31-
32- protected PriceCategory $ priceCategory ;
33-
3425 public function __construct (
35- private readonly EventDateRepository $ eventDateRepository ,
36- private readonly PriceCategoryRepository $ priceCategoryRepository ,
26+ private readonly ProductFactoryInterface $ productFactory ,
3727 ) {}
3828
3929 public function getProductFromForm (
@@ -45,107 +35,37 @@ public function getProductFromForm(
4535 if ($ formValues ['productType ' ] !== 'CartEvents ' ) {
4636 return [$ errors , []];
4737 }
48-
49- $ eventDateId = $ formValues ['eventDateId ' ];
50- $ priceCategoryId = (int )$ formValues ['priceCategoryId ' ];
51-
5238 unset($ formValues ['productType ' ]);
39+
40+ $ requestArguments = [
41+ 'eventDate ' => $ formValues ['eventDateId ' ],
42+ 'priceCategory ' => $ formValues ['priceCategoryId ' ],
43+ 'quantity ' => $ formValues ['quantity ' ] ?? 1 ,
44+ ];
5345 unset($ formValues ['eventDateId ' ]);
5446 unset($ formValues ['priceCategoryId ' ]);
47+ unset($ formValues ['quantity ' ]);
5548
56- $ this ->eventDate = $ this ->eventDateRepository ->findByUid ((int )$ eventDateId );
57- $ quantity = 1 ;
58-
59- if ($ priceCategoryId ) {
60- $ this ->priceCategory = $ this ->priceCategoryRepository ->findByUid ((int )$ priceCategoryId );
49+ if (!empty ($ formValues )) {
50+ $ requestArguments ['feVariant ' ] = $ this ->getFeVariant ($ formValues );
6151 }
62-
63- $ newProduct = $ this ->getProductFromEventDate ($ quantity , $ cart ->getTaxClasses (), $ formValues );
52+ $ newProduct = $ this ->productFactory ->createProductFromRequestArguments (
53+ $ requestArguments ,
54+ $ cart ->getTaxClasses (),
55+ (bool )GeneralUtility::makeInstance (ExtensionConfiguration::class)->get ('cart_events ' , 'inputIsNetPrice ' )
56+ );
6457
6558 $ newProduct ->setMaxNumberInCart (1 );
6659 $ newProduct ->setMinNumberInCart (1 );
6760
6861 return [$ errors , [$ newProduct ]];
6962 }
7063
71- protected function getProductFromEventDate (
72- int $ quantity ,
73- array $ taxClasses ,
74- array $ feVariants = []
75- ): Product {
76- $ event = $ this ->eventDate ->getEvent ();
77- $ title = implode (' - ' , [$ event ->getTitle (), $ this ->eventDate ->getTitle ()]);
78- $ sku = implode (' - ' , [$ event ->getSku (), $ this ->eventDate ->getSku ()]);
79-
80- $ price = $ this ->eventDate ->getBestPrice ();
81- if ($ this ->priceCategory ) {
82- $ price = $ this ->priceCategory ->getBestPrice ();
83- }
84-
85- $ inputIsNetPrice = (bool )GeneralUtility::makeInstance (ExtensionConfiguration::class)->get ('cart_events ' , 'inputIsNetPrice ' );
86-
87- $ product = new Product (
88- 'CartEvents ' ,
89- $ this ->eventDate ->getUid (),
90- $ sku ,
91- $ title ,
92- $ price ,
93- $ taxClasses [$ event ->getTaxClassId ()],
94- $ quantity ,
95- $ inputIsNetPrice ,
96- $ this ->getFeVariant ($ feVariants )
97- );
98- $ product ->setIsVirtualProduct ($ event ->isVirtualProduct ());
99-
100- if ($ this ->priceCategory ) {
101- $ product ->addBeVariant ($ this ->getProductBackendVariant ($ product , $ quantity ));
102- }
103-
104- if ($ GLOBALS ['TYPO3_CONF_VARS ' ]['EXTCONF ' ]['cart_events ' ]['getProductFromEventDate ' ]) {
105- foreach ($ GLOBALS ['TYPO3_CONF_VARS ' ]['EXTCONF ' ]['cart_events ' ]['getProductFromEventDate ' ] ?? [] as $ className ) {
106- $ params = [
107- 'cart ' => $ this ->cart ,
108- 'eventDate ' => $ this ->eventDate ,
109- ];
110-
111- $ _procObj = GeneralUtility::makeInstance ($ className );
112- $ _procObj ->changeProductFromEventDate ($ product , $ params );
113- }
114- }
115-
116- return $ product ;
117- }
118-
119- protected function getProductBackendVariant (
120- Product $ product ,
121- int $ quantity
122- ): BeVariant {
123- $ cartBackendVariant = GeneralUtility::makeInstance (
124- BeVariant::class,
125- PriceCategory::class . '- ' . $ this ->priceCategory ->getUid (),
126- $ product ,
127- $ this ->priceCategory ->getTitle (),
128- $ this ->priceCategory ->getSku (),
129- 1 ,
130- $ this ->priceCategory ->getBestPrice (),
131- $ quantity
132- );
133-
134- /*
135- TODO
136- if ($bestSpecialPrice) {
137- $cartBackendVariant->setSpecialPrice($bestSpecialPrice->getPrice());
138- }
139- */
140-
141- return $ cartBackendVariant ;
142- }
143-
14464 protected function getFeVariant (array $ data ): ?FeVariant
14565 {
14666 $ feVariant = null ;
14767
148- if (!empty ($ data ) && is_array ( $ data ) ) {
68+ if (!empty ($ data )) {
14969 $ feVariants = [];
15070 foreach ($ data as $ dataKey => $ dataValue ) {
15171 if (!empty ($ dataKey ) && !empty ($ dataValue )) {
0 commit comments