From 5a4f7db45513d23442957ce9cfce43cf4d5b292b Mon Sep 17 00:00:00 2001 From: tjeujansen Date: Fri, 27 Feb 2026 15:15:54 +0100 Subject: [PATCH 1/6] feat(TW-104): Added add to cart event backend functionality --- src/Api/Data/EventInterface.php | 15 ++++ .../Event/CheckoutSessionServiceInterface.php | 25 ++++++ src/Api/Event/PriceFormatServiceInterface.php | 14 ++++ src/Event/AddToCart.php | 80 +++++++++++++++++++ src/Observer/Event/TriggerAddToCartEvent.php | 45 +++++++++++ .../Cart/AddEventDataToCartSection.php | 32 ++++++++ src/Service/Event/CheckoutSessionService.php | 52 ++++++++++++ src/Service/Event/PriceFormatService.php | 19 +++++ src/etc/frontend/di.xml | 9 +++ src/etc/frontend/events.xml | 4 + 10 files changed, 295 insertions(+) create mode 100644 src/Api/Data/EventInterface.php create mode 100644 src/Api/Event/CheckoutSessionServiceInterface.php create mode 100644 src/Api/Event/PriceFormatServiceInterface.php create mode 100644 src/Event/AddToCart.php create mode 100644 src/Observer/Event/TriggerAddToCartEvent.php create mode 100644 src/Plugin/Event/CustomerData/Cart/AddEventDataToCartSection.php create mode 100644 src/Service/Event/CheckoutSessionService.php create mode 100644 src/Service/Event/PriceFormatService.php diff --git a/src/Api/Data/EventInterface.php b/src/Api/Data/EventInterface.php new file mode 100644 index 0000000..f2ece37 --- /dev/null +++ b/src/Api/Data/EventInterface.php @@ -0,0 +1,15 @@ + 'addtocart', + 'data' => [ + 'productKey' => $this->dataHelper->getTweakwiseId((int)$this->product->getId()), + 'quantity' => $this->qty, + 'totalAmount' => $this->getTotalAmount() + ] + ]; + } + + /** + * @param Product $product + * @return AddToCart + */ + public function setProduct(Product $product): AddToCart + { + $this->product = $product; + return $this; + } + + /** + * @param int $qty + * @return $this + */ + public function setQty(int $qty): AddToCart + { + $this->qty = $qty; + return $this; + } + + /** + * @return float + */ + private function getTotalAmount(): float + { + $price = (float)$this->product->getPriceInfo()->getPrice(FinalPrice::PRICE_CODE)->getValue(); + return $this->priceFormatService->format($price * $this->qty); + } +} diff --git a/src/Observer/Event/TriggerAddToCartEvent.php b/src/Observer/Event/TriggerAddToCartEvent.php new file mode 100644 index 0000000..24377cd --- /dev/null +++ b/src/Observer/Event/TriggerAddToCartEvent.php @@ -0,0 +1,45 @@ +getData('product'); + $qty = (int)$observer->getData('request')->getParam('qty'); + if ($qty === 0) { + $qty = 1; + } + + $this->checkoutSessionService->add( + 'add_to_cart_event', + $this->addToCartEvent->setProduct($product)->setQty($qty)->get() + ); + } +} diff --git a/src/Plugin/Event/CustomerData/Cart/AddEventDataToCartSection.php b/src/Plugin/Event/CustomerData/Cart/AddEventDataToCartSection.php new file mode 100644 index 0000000..9b5f2f3 --- /dev/null +++ b/src/Plugin/Event/CustomerData/Cart/AddEventDataToCartSection.php @@ -0,0 +1,32 @@ +checkoutSessionService->get(); + $this->checkoutSessionService->clear(); + + return array_merge($result, ['tweakwise_events' => $events]); + } +} diff --git a/src/Service/Event/CheckoutSessionService.php b/src/Service/Event/CheckoutSessionService.php new file mode 100644 index 0000000..da2e834 --- /dev/null +++ b/src/Service/Event/CheckoutSessionService.php @@ -0,0 +1,52 @@ +get(); + $eventData[$identifier] = $data; + $this->checkoutSession->setTweakwiseEventData($eventData); + } + + /** + * @return array + */ + public function get(): array + { + $eventData = $this->checkoutSession->getTweakwiseEventData(); + if (is_array($eventData)) { + return $eventData; + } + + return []; + } + + /** + * @return void + */ + public function clear(): void + { + $this->checkoutSession->setTweakwiseEventData([]); + } +} diff --git a/src/Service/Event/PriceFormatService.php b/src/Service/Event/PriceFormatService.php new file mode 100644 index 0000000..add8fa7 --- /dev/null +++ b/src/Service/Event/PriceFormatService.php @@ -0,0 +1,19 @@ + + + + + + + diff --git a/src/etc/frontend/events.xml b/src/etc/frontend/events.xml index 8fc4a45..fe1704c 100644 --- a/src/etc/frontend/events.xml +++ b/src/etc/frontend/events.xml @@ -9,4 +9,8 @@ + + + From 0970cb930bdce233971c1c32e3dfc945dea2edc7 Mon Sep 17 00:00:00 2001 From: Pascal Wientjes Date: Wed, 4 Mar 2026 16:03:57 +0100 Subject: [PATCH 2/6] feat(TW-104): added generic frontend tweakwiseLayerPush function --- src/Observer/Event/TriggerAddToCartEvent.php | 2 +- src/view/frontend/layout/default.xml | 13 +++++++++++++ src/view/frontend/layout/default_hyva.xml | 6 ++++++ .../templates/js/attribute-landing.phtml | 1 + .../templates/js/category/hyva/add-to.phtml | 1 + .../templates/js/category/listing.phtml | 1 + src/view/frontend/templates/js/default.phtml | 1 + .../frontend/templates/js/event/default.phtml | 1 + .../templates/js/event/event-push.phtml | 18 ++++++++++++++++++ .../js/event/hyva/init-event-push.phtml | 8 ++++++++ .../js/event/luma/init-event-push.phtml | 14 ++++++++++++++ .../templates/js/event/purchase-event.phtml | 1 + .../templates/js/event/search-event.phtml | 1 + .../js/event/view-product-event.phtml | 1 + src/view/frontend/templates/js/search.phtml | 4 ++++ 15 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 src/view/frontend/templates/js/event/event-push.phtml create mode 100644 src/view/frontend/templates/js/event/hyva/init-event-push.phtml create mode 100644 src/view/frontend/templates/js/event/luma/init-event-push.phtml diff --git a/src/Observer/Event/TriggerAddToCartEvent.php b/src/Observer/Event/TriggerAddToCartEvent.php index 24377cd..47f8833 100644 --- a/src/Observer/Event/TriggerAddToCartEvent.php +++ b/src/Observer/Event/TriggerAddToCartEvent.php @@ -38,7 +38,7 @@ public function execute(Observer $observer) } $this->checkoutSessionService->add( - 'add_to_cart_event', + 'AddToCart', $this->addToCartEvent->setProduct($product)->setQty($qty)->get() ); } diff --git a/src/view/frontend/layout/default.xml b/src/view/frontend/layout/default.xml index fb70c4b..376e67b 100644 --- a/src/view/frontend/layout/default.xml +++ b/src/view/frontend/layout/default.xml @@ -42,5 +42,18 @@ + + + + + + + diff --git a/src/view/frontend/layout/default_hyva.xml b/src/view/frontend/layout/default_hyva.xml index 81e8aec..20432ea 100644 --- a/src/view/frontend/layout/default_hyva.xml +++ b/src/view/frontend/layout/default_hyva.xml @@ -14,5 +14,11 @@ + + + + Tweakwise_TweakwiseJs::js/event/hyva/init-event-push.phtml + + diff --git a/src/view/frontend/templates/js/attribute-landing.phtml b/src/view/frontend/templates/js/attribute-landing.phtml index b2a8d8c..10aacbd 100644 --- a/src/view/frontend/templates/js/attribute-landing.phtml +++ b/src/view/frontend/templates/js/attribute-landing.phtml @@ -24,3 +24,4 @@ $landingPage = $block->getLandingPage(); window['twn-starter-config'].navigation.sortingTemplateId = getTweakwiseSortTemplate()?>; window['twn-starter-config'].navigation.builderTemplateId = getTweakwiseBuilderTemplate()?>; +registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/category/hyva/add-to.phtml b/src/view/frontend/templates/js/category/hyva/add-to.phtml index 5ec3d53..bf3116a 100644 --- a/src/view/frontend/templates/js/category/hyva/add-to.phtml +++ b/src/view/frontend/templates/js/category/hyva/add-to.phtml @@ -196,3 +196,4 @@ $storeId = $viewModel->getStoreId(); timeout); } +registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/category/listing.phtml b/src/view/frontend/templates/js/category/listing.phtml index dd11605..9f54c87 100644 --- a/src/view/frontend/templates/js/category/listing.phtml +++ b/src/view/frontend/templates/js/category/listing.phtml @@ -13,4 +13,5 @@ $viewModel = $block->getViewModel(); +registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/default.phtml b/src/view/frontend/templates/js/default.phtml index 36bcaa1..8966cbc 100644 --- a/src/view/frontend/templates/js/default.phtml +++ b/src/view/frontend/templates/js/default.phtml @@ -16,4 +16,5 @@ use Magento\Framework\View\Element\Template; window['twn-starter-config'] = window['twn-starter-config'] || {}; window['twn-starter-config'].on = window['twn-starter-config'].on || {}; +registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/event/default.phtml b/src/view/frontend/templates/js/event/default.phtml index 7d5fd2d..ce1adaa 100644 --- a/src/view/frontend/templates/js/event/default.phtml +++ b/src/view/frontend/templates/js/event/default.phtml @@ -104,3 +104,4 @@ $eventsCookieName = $eventViewModel->getEventsCookieName(); document.cookie = `${cookieName}=${encodeURIComponent(profileKey)}; expires=${expirationDate.toGMTString()}; path=/; secure; SameSite=lax` } +registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/event/event-push.phtml b/src/view/frontend/templates/js/event/event-push.phtml new file mode 100644 index 0000000..8e037eb --- /dev/null +++ b/src/view/frontend/templates/js/event/event-push.phtml @@ -0,0 +1,18 @@ + +registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/event/hyva/init-event-push.phtml b/src/view/frontend/templates/js/event/hyva/init-event-push.phtml new file mode 100644 index 0000000..6e2c140 --- /dev/null +++ b/src/view/frontend/templates/js/event/hyva/init-event-push.phtml @@ -0,0 +1,8 @@ + +registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/event/luma/init-event-push.phtml b/src/view/frontend/templates/js/event/luma/init-event-push.phtml new file mode 100644 index 0000000..e5fd2e6 --- /dev/null +++ b/src/view/frontend/templates/js/event/luma/init-event-push.phtml @@ -0,0 +1,14 @@ + diff --git a/src/view/frontend/templates/js/event/purchase-event.phtml b/src/view/frontend/templates/js/event/purchase-event.phtml index 001d9fc..fea3137 100644 --- a/src/view/frontend/templates/js/event/purchase-event.phtml +++ b/src/view/frontend/templates/js/event/purchase-event.phtml @@ -20,3 +20,4 @@ $eventViewModel = $block->getViewModel(); } }); +registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/event/search-event.phtml b/src/view/frontend/templates/js/event/search-event.phtml index c917c70..8db5dd7 100644 --- a/src/view/frontend/templates/js/event/search-event.phtml +++ b/src/view/frontend/templates/js/event/search-event.phtml @@ -36,3 +36,4 @@ use Magento\Catalog\Block\Product\View; return ''; } +registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/event/view-product-event.phtml b/src/view/frontend/templates/js/event/view-product-event.phtml index a450ace..c2f6588 100644 --- a/src/view/frontend/templates/js/event/view-product-event.phtml +++ b/src/view/frontend/templates/js/event/view-product-event.phtml @@ -19,3 +19,4 @@ $eventViewModel = $block->getViewModel(); } }); +registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/search.phtml b/src/view/frontend/templates/js/search.phtml index f94fb45..e6ff564 100644 --- a/src/view/frontend/templates/js/search.phtml +++ b/src/view/frontend/templates/js/search.phtml @@ -63,6 +63,7 @@ $viewModel = $block->getViewModel(); }; }); + registerInlineScript(); ?> getSearchType()->value === SearchType::INSTANT_SEARCH->value): ?> + registerInlineScript(); ?> + registerInlineScript(); ?> isSearchResultsPage()): ?> + registerInlineScript(); ?> From 78b2404c83ca1fc4fa6b474665dd1d62827be736 Mon Sep 17 00:00:00 2001 From: tjeujansen Date: Thu, 5 Mar 2026 08:12:59 +0100 Subject: [PATCH 3/6] feat(TW-104): Added add to cart event frontend functionality --- src/view/frontend/layout/default.xml | 13 +++++++++++++ src/view/frontend/layout/default_hyva.xml | 6 ++++++ .../frontend/templates/js/attribute-landing.phtml | 1 + .../templates/js/category/hyva/add-to.phtml | 1 + .../frontend/templates/js/category/listing.phtml | 1 + src/view/frontend/templates/js/default.phtml | 1 + .../frontend/templates/js/event/default.phtml | 1 + .../frontend/templates/js/event/event-push.phtml | 15 +++++++++++++++ .../templates/js/event/hyva/init-event-push.phtml | 8 ++++++++ .../templates/js/event/luma/init-event-push.phtml | 14 ++++++++++++++ .../templates/js/event/purchase-event.phtml | 1 + .../templates/js/event/search-event.phtml | 1 + .../templates/js/event/view-product-event.phtml | 1 + src/view/frontend/templates/js/search.phtml | 4 ++++ 14 files changed, 68 insertions(+) create mode 100644 src/view/frontend/templates/js/event/event-push.phtml create mode 100644 src/view/frontend/templates/js/event/hyva/init-event-push.phtml create mode 100644 src/view/frontend/templates/js/event/luma/init-event-push.phtml diff --git a/src/view/frontend/layout/default.xml b/src/view/frontend/layout/default.xml index fb70c4b..376e67b 100644 --- a/src/view/frontend/layout/default.xml +++ b/src/view/frontend/layout/default.xml @@ -42,5 +42,18 @@ + + + + + + + diff --git a/src/view/frontend/layout/default_hyva.xml b/src/view/frontend/layout/default_hyva.xml index 81e8aec..20432ea 100644 --- a/src/view/frontend/layout/default_hyva.xml +++ b/src/view/frontend/layout/default_hyva.xml @@ -14,5 +14,11 @@ + + + + Tweakwise_TweakwiseJs::js/event/hyva/init-event-push.phtml + + diff --git a/src/view/frontend/templates/js/attribute-landing.phtml b/src/view/frontend/templates/js/attribute-landing.phtml index b2a8d8c..10aacbd 100644 --- a/src/view/frontend/templates/js/attribute-landing.phtml +++ b/src/view/frontend/templates/js/attribute-landing.phtml @@ -24,3 +24,4 @@ $landingPage = $block->getLandingPage(); window['twn-starter-config'].navigation.sortingTemplateId = getTweakwiseSortTemplate()?>; window['twn-starter-config'].navigation.builderTemplateId = getTweakwiseBuilderTemplate()?>; +registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/category/hyva/add-to.phtml b/src/view/frontend/templates/js/category/hyva/add-to.phtml index 5ec3d53..bf3116a 100644 --- a/src/view/frontend/templates/js/category/hyva/add-to.phtml +++ b/src/view/frontend/templates/js/category/hyva/add-to.phtml @@ -196,3 +196,4 @@ $storeId = $viewModel->getStoreId(); timeout); } +registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/category/listing.phtml b/src/view/frontend/templates/js/category/listing.phtml index dd11605..9f54c87 100644 --- a/src/view/frontend/templates/js/category/listing.phtml +++ b/src/view/frontend/templates/js/category/listing.phtml @@ -13,4 +13,5 @@ $viewModel = $block->getViewModel(); +registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/default.phtml b/src/view/frontend/templates/js/default.phtml index 36bcaa1..8966cbc 100644 --- a/src/view/frontend/templates/js/default.phtml +++ b/src/view/frontend/templates/js/default.phtml @@ -16,4 +16,5 @@ use Magento\Framework\View\Element\Template; window['twn-starter-config'] = window['twn-starter-config'] || {}; window['twn-starter-config'].on = window['twn-starter-config'].on || {}; +registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/event/default.phtml b/src/view/frontend/templates/js/event/default.phtml index 7d5fd2d..ce1adaa 100644 --- a/src/view/frontend/templates/js/event/default.phtml +++ b/src/view/frontend/templates/js/event/default.phtml @@ -104,3 +104,4 @@ $eventsCookieName = $eventViewModel->getEventsCookieName(); document.cookie = `${cookieName}=${encodeURIComponent(profileKey)}; expires=${expirationDate.toGMTString()}; path=/; secure; SameSite=lax` } +registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/event/event-push.phtml b/src/view/frontend/templates/js/event/event-push.phtml new file mode 100644 index 0000000..a33f3ab --- /dev/null +++ b/src/view/frontend/templates/js/event/event-push.phtml @@ -0,0 +1,15 @@ + +registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/event/hyva/init-event-push.phtml b/src/view/frontend/templates/js/event/hyva/init-event-push.phtml new file mode 100644 index 0000000..6e2c140 --- /dev/null +++ b/src/view/frontend/templates/js/event/hyva/init-event-push.phtml @@ -0,0 +1,8 @@ + +registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/event/luma/init-event-push.phtml b/src/view/frontend/templates/js/event/luma/init-event-push.phtml new file mode 100644 index 0000000..e5fd2e6 --- /dev/null +++ b/src/view/frontend/templates/js/event/luma/init-event-push.phtml @@ -0,0 +1,14 @@ + diff --git a/src/view/frontend/templates/js/event/purchase-event.phtml b/src/view/frontend/templates/js/event/purchase-event.phtml index 001d9fc..fea3137 100644 --- a/src/view/frontend/templates/js/event/purchase-event.phtml +++ b/src/view/frontend/templates/js/event/purchase-event.phtml @@ -20,3 +20,4 @@ $eventViewModel = $block->getViewModel(); } }); +registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/event/search-event.phtml b/src/view/frontend/templates/js/event/search-event.phtml index c917c70..8db5dd7 100644 --- a/src/view/frontend/templates/js/event/search-event.phtml +++ b/src/view/frontend/templates/js/event/search-event.phtml @@ -36,3 +36,4 @@ use Magento\Catalog\Block\Product\View; return ''; } +registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/event/view-product-event.phtml b/src/view/frontend/templates/js/event/view-product-event.phtml index a450ace..c2f6588 100644 --- a/src/view/frontend/templates/js/event/view-product-event.phtml +++ b/src/view/frontend/templates/js/event/view-product-event.phtml @@ -19,3 +19,4 @@ $eventViewModel = $block->getViewModel(); } }); +registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/search.phtml b/src/view/frontend/templates/js/search.phtml index f94fb45..e6ff564 100644 --- a/src/view/frontend/templates/js/search.phtml +++ b/src/view/frontend/templates/js/search.phtml @@ -63,6 +63,7 @@ $viewModel = $block->getViewModel(); }; }); + registerInlineScript(); ?> getSearchType()->value === SearchType::INSTANT_SEARCH->value): ?> + registerInlineScript(); ?> + registerInlineScript(); ?> isSearchResultsPage()): ?> + registerInlineScript(); ?> From 8c951c50e6a692778fe73250fc47c7d3127ff3fd Mon Sep 17 00:00:00 2001 From: Pascal Wientjes Date: Thu, 5 Mar 2026 11:29:50 +0100 Subject: [PATCH 4/6] feat(TW-104): added customer in hyva check --- .../frontend/templates/js/event/hyva/init-event-push.phtml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/view/frontend/templates/js/event/hyva/init-event-push.phtml b/src/view/frontend/templates/js/event/hyva/init-event-push.phtml index 6e2c140..d66fcf3 100644 --- a/src/view/frontend/templates/js/event/hyva/init-event-push.phtml +++ b/src/view/frontend/templates/js/event/hyva/init-event-push.phtml @@ -1,6 +1,9 @@ registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/event/hyva/init-event-push.phtml b/src/view/frontend/templates/js/event/hyva/init-event-push.phtml index d66fcf3..563081d 100644 --- a/src/view/frontend/templates/js/event/hyva/init-event-push.phtml +++ b/src/view/frontend/templates/js/event/hyva/init-event-push.phtml @@ -1,11 +1,12 @@ registerInlineScript(); ?> diff --git a/src/view/frontend/templates/js/event/luma/init-event-push.phtml b/src/view/frontend/templates/js/event/luma/init-event-push.phtml index 4879dac..1efba06 100644 --- a/src/view/frontend/templates/js/event/luma/init-event-push.phtml +++ b/src/view/frontend/templates/js/event/luma/init-event-push.phtml @@ -1,9 +1,11 @@ From 05fd0ac41d8323d57d07148ddcbb96544660f6f3 Mon Sep 17 00:00:00 2001 From: tjeujansen Date: Fri, 6 Mar 2026 10:38:19 +0100 Subject: [PATCH 6/6] feat(TW-104): Added add to wishlist event --- ...erface.php => SessionServiceInterface.php} | 2 +- src/Event/AddToWishlist.php | 50 +++++++++++++++++ src/Observer/Event/TriggerAddToCartEvent.php | 8 +-- .../Event/TriggerAddToWishlistEvent.php | 40 ++++++++++++++ src/Plugin/Event/AddEventDataToSection.php | 32 +++++++++++ .../Cart/AddEventDataToCartSection.php | 32 ----------- ...tSessionService.php => SessionService.php} | 19 ++++--- src/etc/frontend/di.xml | 53 ++++++++++++++++++- src/etc/frontend/events.xml | 4 ++ .../templates/js/event/event-push.phtml | 2 +- .../js/event/hyva/init-event-push.phtml | 6 +-- 11 files changed, 197 insertions(+), 51 deletions(-) rename src/Api/Event/{CheckoutSessionServiceInterface.php => SessionServiceInterface.php} (90%) create mode 100644 src/Event/AddToWishlist.php create mode 100644 src/Observer/Event/TriggerAddToWishlistEvent.php create mode 100644 src/Plugin/Event/AddEventDataToSection.php delete mode 100644 src/Plugin/Event/CustomerData/Cart/AddEventDataToCartSection.php rename src/Service/Event/{CheckoutSessionService.php => SessionService.php} (53%) diff --git a/src/Api/Event/CheckoutSessionServiceInterface.php b/src/Api/Event/SessionServiceInterface.php similarity index 90% rename from src/Api/Event/CheckoutSessionServiceInterface.php rename to src/Api/Event/SessionServiceInterface.php index 35d9976..4fd4bfc 100644 --- a/src/Api/Event/CheckoutSessionServiceInterface.php +++ b/src/Api/Event/SessionServiceInterface.php @@ -4,7 +4,7 @@ namespace Tweakwise\TweakwiseJs\Api\Event; -interface CheckoutSessionServiceInterface +interface SessionServiceInterface { /** * @param string $identifier diff --git a/src/Event/AddToWishlist.php b/src/Event/AddToWishlist.php new file mode 100644 index 0000000..815b4ac --- /dev/null +++ b/src/Event/AddToWishlist.php @@ -0,0 +1,50 @@ + 'addtowishlist', + 'data' => [ + 'productKey' => $this->dataHelper->getTweakwiseId((int)$this->product->getId()) + ] + ]; + } + + /** + * @param Product $product + * @return AddToWishlist + */ + public function setProduct(Product $product): AddToWishlist + { + $this->product = $product; + return $this; + } +} diff --git a/src/Observer/Event/TriggerAddToCartEvent.php b/src/Observer/Event/TriggerAddToCartEvent.php index 47f8833..81b2bf0 100644 --- a/src/Observer/Event/TriggerAddToCartEvent.php +++ b/src/Observer/Event/TriggerAddToCartEvent.php @@ -8,17 +8,17 @@ use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Event\Observer; use Magento\Framework\Exception\NoSuchEntityException; -use Tweakwise\TweakwiseJs\Api\Event\CheckoutSessionServiceInterface; +use Tweakwise\TweakwiseJs\Api\Event\SessionServiceInterface; use Tweakwise\TweakwiseJs\Event\AddToCart as AddToCartEvent; class TriggerAddToCartEvent implements ObserverInterface { /** - * @param CheckoutSessionServiceInterface $checkoutSessionService + * @param SessionServiceInterface $sessionService * @param AddToCartEvent $addToCartEvent */ public function __construct( - private readonly CheckoutSessionServiceInterface $checkoutSessionService, + private readonly SessionServiceInterface $sessionService, private readonly AddToCartEvent $addToCartEvent ) { } @@ -37,7 +37,7 @@ public function execute(Observer $observer) $qty = 1; } - $this->checkoutSessionService->add( + $this->sessionService->add( 'AddToCart', $this->addToCartEvent->setProduct($product)->setQty($qty)->get() ); diff --git a/src/Observer/Event/TriggerAddToWishlistEvent.php b/src/Observer/Event/TriggerAddToWishlistEvent.php new file mode 100644 index 0000000..e43392b --- /dev/null +++ b/src/Observer/Event/TriggerAddToWishlistEvent.php @@ -0,0 +1,40 @@ +getData('product'); + $this->sessionService->add( + 'AddToWishlist', + $this->addToWishlistEvent->setProduct($product)->get() + ); + } +} diff --git a/src/Plugin/Event/AddEventDataToSection.php b/src/Plugin/Event/AddEventDataToSection.php new file mode 100644 index 0000000..033ab83 --- /dev/null +++ b/src/Plugin/Event/AddEventDataToSection.php @@ -0,0 +1,32 @@ +sessionService->get(); + $this->sessionService->clear(); + + return array_merge($result, ['tweakwise_events' => $events]); + } +} diff --git a/src/Plugin/Event/CustomerData/Cart/AddEventDataToCartSection.php b/src/Plugin/Event/CustomerData/Cart/AddEventDataToCartSection.php deleted file mode 100644 index 9b5f2f3..0000000 --- a/src/Plugin/Event/CustomerData/Cart/AddEventDataToCartSection.php +++ /dev/null @@ -1,32 +0,0 @@ -checkoutSessionService->get(); - $this->checkoutSessionService->clear(); - - return array_merge($result, ['tweakwise_events' => $events]); - } -} diff --git a/src/Service/Event/CheckoutSessionService.php b/src/Service/Event/SessionService.php similarity index 53% rename from src/Service/Event/CheckoutSessionService.php rename to src/Service/Event/SessionService.php index da2e834..241bc0f 100644 --- a/src/Service/Event/CheckoutSessionService.php +++ b/src/Service/Event/SessionService.php @@ -4,16 +4,16 @@ namespace Tweakwise\TweakwiseJs\Service\Event; -use Magento\Checkout\Model\Session as CheckoutSession; -use Tweakwise\TweakwiseJs\Api\Event\CheckoutSessionServiceInterface; +use Magento\Framework\Session\SessionManagerInterface; +use Tweakwise\TweakwiseJs\Api\Event\SessionServiceInterface; -class CheckoutSessionService implements CheckoutSessionServiceInterface +class SessionService implements SessionServiceInterface { /** - * @param CheckoutSession $checkoutSession + * @param SessionManagerInterface $session */ public function __construct( - private readonly CheckoutSession $checkoutSession, + private readonly SessionManagerInterface $session, ) { } @@ -26,7 +26,8 @@ public function add(string $identifier, array $data): void { $eventData = $this->get(); $eventData[$identifier] = $data; - $this->checkoutSession->setTweakwiseEventData($eventData); + /** @phpstan-ignore-next-line */ + $this->session->setTweakwiseEventData($eventData); } /** @@ -34,7 +35,8 @@ public function add(string $identifier, array $data): void */ public function get(): array { - $eventData = $this->checkoutSession->getTweakwiseEventData(); + /** @phpstan-ignore-next-line */ + $eventData = $this->session->getTweakwiseEventData(); if (is_array($eventData)) { return $eventData; } @@ -47,6 +49,7 @@ public function get(): array */ public function clear(): void { - $this->checkoutSession->setTweakwiseEventData([]); + /** @phpstan-ignore-next-line */ + $this->session->setTweakwiseEventData([]); } } diff --git a/src/etc/frontend/di.xml b/src/etc/frontend/di.xml index 263ace3..5f8530a 100644 --- a/src/etc/frontend/di.xml +++ b/src/etc/frontend/di.xml @@ -1,8 +1,8 @@ - + @@ -10,8 +10,57 @@ + + + + Magento\Checkout\Model\Session + + + + + + Tweakwise\TweakwiseJs\Service\Event\CheckoutSessionService + + + + + + + Tweakwise\TweakwiseJs\Service\Event\CheckoutSessionService + + + + + + + Magento\Customer\Model\Session + + + + + + Tweakwise\TweakwiseJs\Service\Event\CustomerSessionService + + + + + + + + Tweakwise\TweakwiseJs\Service\Event\CustomerSessionService + + + + + + diff --git a/src/etc/frontend/events.xml b/src/etc/frontend/events.xml index fe1704c..352543c 100644 --- a/src/etc/frontend/events.xml +++ b/src/etc/frontend/events.xml @@ -13,4 +13,8 @@ + + + diff --git a/src/view/frontend/templates/js/event/event-push.phtml b/src/view/frontend/templates/js/event/event-push.phtml index b1f1d63..974e48e 100644 --- a/src/view/frontend/templates/js/event/event-push.phtml +++ b/src/view/frontend/templates/js/event/event-push.phtml @@ -3,7 +3,7 @@ * @return array */ function getSectionNames() { - return ['cart', 'customer']; + return ['cart', 'wishlist']; } /** diff --git a/src/view/frontend/templates/js/event/hyva/init-event-push.phtml b/src/view/frontend/templates/js/event/hyva/init-event-push.phtml index 563081d..9392e11 100644 --- a/src/view/frontend/templates/js/event/hyva/init-event-push.phtml +++ b/src/view/frontend/templates/js/event/hyva/init-event-push.phtml @@ -1,10 +1,10 @@