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/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 new file mode 100644 index 0000000..81b2bf0 --- /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->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/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 @@ +get(); + $eventData[$identifier] = $data; + /** @phpstan-ignore-next-line */ + $this->session->setTweakwiseEventData($eventData); + } + + /** + * @return array + */ + public function get(): array + { + /** @phpstan-ignore-next-line */ + $eventData = $this->session->getTweakwiseEventData(); + if (is_array($eventData)) { + return $eventData; + } + + return []; + } + + /** + * @return void + */ + public function clear(): void + { + /** @phpstan-ignore-next-line */ + $this->session->setTweakwiseEventData([]); + } +} diff --git a/src/etc/frontend/di.xml b/src/etc/frontend/di.xml index 73565f9..5f8530a 100644 --- a/src/etc/frontend/di.xml +++ b/src/etc/frontend/di.xml @@ -1,8 +1,66 @@ + + + + + + + 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 8fc4a45..352543c 100644 --- a/src/etc/frontend/events.xml +++ b/src/etc/frontend/events.xml @@ -9,4 +9,12 @@ + + + + + + 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..974e48e --- /dev/null +++ b/src/view/frontend/templates/js/event/event-push.phtml @@ -0,0 +1,38 @@ + +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..9392e11 --- /dev/null +++ b/src/view/frontend/templates/js/event/hyva/init-event-push.phtml @@ -0,0 +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 new file mode 100644 index 0000000..1efba06 --- /dev/null +++ b/src/view/frontend/templates/js/event/luma/init-event-push.phtml @@ -0,0 +1,11 @@ + 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(); ?>