Skip to content
Open
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
15 changes: 15 additions & 0 deletions src/Api/Data/EventInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Tweakwise\TweakwiseJs\Api\Data;

use Magento\Framework\View\Element\Block\ArgumentInterface;

interface EventInterface extends ArgumentInterface
{
/**
* @return array
*/
public function get(): array;
}
14 changes: 14 additions & 0 deletions src/Api/Event/PriceFormatServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Tweakwise\TweakwiseJs\Api\Event;

interface PriceFormatServiceInterface
{
/**
* @param float $price
* @return float
*/
public function format(float $price): float;
}
25 changes: 25 additions & 0 deletions src/Api/Event/SessionServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Tweakwise\TweakwiseJs\Api\Event;

interface SessionServiceInterface
{
/**
* @param string $identifier
* @param array $data
* @return void
*/
public function add(string $identifier, array $data): void;

/**
* @return array
*/
public function get(): array;

/**
* @return void
*/
public function clear(): void;
}
80 changes: 80 additions & 0 deletions src/Event/AddToCart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace Tweakwise\TweakwiseJs\Event;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Pricing\Price\FinalPrice;
use Magento\Framework\Exception\NoSuchEntityException;
use Tweakwise\TweakwiseJs\Api\Data\EventInterface;
use Tweakwise\TweakwiseJs\Api\Event\PriceFormatServiceInterface;
use Tweakwise\TweakwiseJs\Helper\Data;

class AddToCart implements EventInterface
{
/**
* @var Product|null
*/
private ?Product $product = null;

/**
* @var int
*/
private int $qty = 1;

/**
* @param PriceFormatServiceInterface $priceFormatService
* @param Data $dataHelper
*/
public function __construct(
private readonly PriceFormatServiceInterface $priceFormatService,
private readonly Data $dataHelper,
) {
}

/**
* @return array
* @throws NoSuchEntityException
*/
public function get(): array
{
return [
'event' => '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);
}
}
50 changes: 50 additions & 0 deletions src/Event/AddToWishlist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Tweakwise\TweakwiseJs\Event;

use Magento\Catalog\Model\Product;
use Magento\Framework\Exception\NoSuchEntityException;
use Tweakwise\TweakwiseJs\Api\Data\EventInterface;
use Tweakwise\TweakwiseJs\Helper\Data;

class AddToWishlist implements EventInterface
{
/**
* @var Product|null
*/
private ?Product $product = null;

/**
* @param Data $dataHelper
*/
public function __construct(
private readonly Data $dataHelper,
) {
}

/**
* @return array
* @throws NoSuchEntityException
*/
public function get(): array
{
return [
'event' => '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;
}
}
45 changes: 45 additions & 0 deletions src/Observer/Event/TriggerAddToCartEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Tweakwise\TweakwiseJs\Observer\Event;

use Magento\Catalog\Model\Product;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Exception\NoSuchEntityException;
use Tweakwise\TweakwiseJs\Api\Event\SessionServiceInterface;
use Tweakwise\TweakwiseJs\Event\AddToCart as AddToCartEvent;

class TriggerAddToCartEvent implements ObserverInterface
{
/**
* @param SessionServiceInterface $sessionService
* @param AddToCartEvent $addToCartEvent
*/
public function __construct(
private readonly SessionServiceInterface $sessionService,
private readonly AddToCartEvent $addToCartEvent
) {
}

/**
* @param Observer $observer
* @return void
* @throws NoSuchEntityException
*/
public function execute(Observer $observer)
{
/** @var Product $product */
$product = $observer->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()
);
}
}
40 changes: 40 additions & 0 deletions src/Observer/Event/TriggerAddToWishlistEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Tweakwise\TweakwiseJs\Observer\Event;

use Magento\Catalog\Model\Product;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Exception\NoSuchEntityException;
use Tweakwise\TweakwiseJs\Api\Event\SessionServiceInterface;
use Tweakwise\TweakwiseJs\Event\AddToWishlist as AddToWishlistEvent;

class TriggerAddToWishlistEvent implements ObserverInterface
{
/**
* @param SessionServiceInterface $sessionService
* @param AddToWishlistEvent $addToWishlistEvent
*/
public function __construct(
private readonly SessionServiceInterface $sessionService,
private readonly AddToWishlistEvent $addToWishlistEvent
) {
}

/**
* @param Observer $observer
* @return void
* @throws NoSuchEntityException
*/
public function execute(Observer $observer)
{
/** @var Product $product */
$product = $observer->getData('product');
$this->sessionService->add(
'AddToWishlist',
$this->addToWishlistEvent->setProduct($product)->get()
);
}
}
32 changes: 32 additions & 0 deletions src/Plugin/Event/AddEventDataToSection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Tweakwise\TweakwiseJs\Plugin\Event;

use Magento\Customer\CustomerData\SectionSourceInterface as Subject;
use Tweakwise\TweakwiseJs\Api\Event\SessionServiceInterface;

class AddEventDataToSection
{
/**
* @param SessionServiceInterface $sessionService
*/
public function __construct(
private readonly SessionServiceInterface $sessionService,
) {
}

/**
* @param Subject $subject
* @param array $result
* @return array
*/
public function afterGetSectionData(Subject $subject, array $result): array
{
$events = $this->sessionService->get();
$this->sessionService->clear();

return array_merge($result, ['tweakwise_events' => $events]);
}
}
19 changes: 19 additions & 0 deletions src/Service/Event/PriceFormatService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Tweakwise\TweakwiseJs\Service\Event;

use Tweakwise\TweakwiseJs\Api\Event\PriceFormatServiceInterface;

class PriceFormatService implements PriceFormatServiceInterface
{
/**
* @param float $price
* @return float
*/
public function format(float $price): float
{
return (float)number_format($price, 2, '.', '');
}
}
55 changes: 55 additions & 0 deletions src/Service/Event/SessionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Tweakwise\TweakwiseJs\Service\Event;

use Magento\Framework\Session\SessionManagerInterface;
use Tweakwise\TweakwiseJs\Api\Event\SessionServiceInterface;

class SessionService implements SessionServiceInterface
{
/**
* @param SessionManagerInterface $session
*/
public function __construct(
private readonly SessionManagerInterface $session,
) {
}

/**
* @param string $identifier
* @param array $data
* @return void
*/
public function add(string $identifier, array $data): void
{
$eventData = $this->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([]);
}
}
Loading