-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
292 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusFacebookTrackingPlugin\Builder; | ||
|
||
final class AddToCartBuilder extends Builder | ||
{ | ||
use ContentIdsAwareBuilderTrait, | ||
ContentsAwareBuilderTrait, | ||
ContentTypeAwareBuilderTrait, | ||
ValueAwareBuilderTrait | ||
; | ||
|
||
public const EVENT_NAME = 'setono_sylius_facebook_tracking.builder.add_to_cart'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusFacebookTrackingPlugin\Builder; | ||
|
||
trait ContentNameAwareBuilderTrait | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $data = []; | ||
|
||
public function setContentName(string $contentName): self | ||
{ | ||
$this->data['content_name'] = $contentName; | ||
|
||
return $this; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusFacebookTrackingPlugin\Builder; | ||
|
||
trait ValueAwareBuilderTrait | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $data = []; | ||
|
||
public function setCurrency(string $currency): self | ||
{ | ||
$this->data['currency'] = $currency; | ||
|
||
return $this; | ||
} | ||
|
||
public function setValue(float $value): self | ||
{ | ||
$this->data['value'] = $value; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusFacebookTrackingPlugin\Builder; | ||
|
||
final class ViewContentBuilder extends Builder | ||
{ | ||
use ContentIdsAwareBuilderTrait, | ||
ContentNameAwareBuilderTrait, | ||
ContentsAwareBuilderTrait, | ||
ContentTypeAwareBuilderTrait, | ||
ValueAwareBuilderTrait | ||
; | ||
|
||
public const EVENT_NAME = 'setono_sylius_facebook_tracking.builder.view_content'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusFacebookTrackingPlugin\EventListener; | ||
|
||
use Setono\SyliusFacebookTrackingPlugin\Builder\AddToCartBuilder; | ||
use Setono\SyliusFacebookTrackingPlugin\Builder\ContentBuilder; | ||
use Setono\SyliusFacebookTrackingPlugin\Context\PixelContextInterface; | ||
use Setono\SyliusFacebookTrackingPlugin\Event\BuilderEvent; | ||
use Setono\SyliusFacebookTrackingPlugin\Tag\FbqTag; | ||
use Setono\SyliusFacebookTrackingPlugin\Tag\FbqTagInterface; | ||
use Setono\SyliusFacebookTrackingPlugin\Tag\Tags; | ||
use Setono\TagBagBundle\TagBag\TagBagInterface; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Order\Context\CartContextInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
|
||
final class AddToCartSubscriber extends TagSubscriber | ||
{ | ||
/** | ||
* @var CartContextInterface | ||
*/ | ||
private $cartContext; | ||
|
||
public function __construct( | ||
TagBagInterface $tagBag, | ||
PixelContextInterface $pixelContext, | ||
EventDispatcherInterface $eventDispatcher, | ||
CartContextInterface $cartContext | ||
) { | ||
parent::__construct($tagBag, $pixelContext, $eventDispatcher); | ||
|
||
$this->cartContext = $cartContext; | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
'sylius.order_item.post_add' => [ | ||
'track', | ||
], | ||
]; | ||
} | ||
|
||
public function track(): void | ||
{ | ||
if (!$this->hasPixels()) { | ||
return; | ||
} | ||
|
||
$order = $this->cartContext->getCart(); | ||
|
||
if (!$order instanceof OrderInterface) { | ||
return; | ||
} | ||
|
||
$builder = AddToCartBuilder::create() | ||
->setCurrency($order->getCurrencyCode()) | ||
->setValue($this->moneyFormatter->format($order->getTotal())) | ||
->setContentType(AddToCartBuilder::CONTENT_TYPE_PRODUCT) | ||
; | ||
|
||
foreach ($order->getItems() as $item) { | ||
$variant = $item->getVariant(); | ||
if (null === $variant) { | ||
continue; | ||
} | ||
|
||
$builder->addContentId($variant->getCode()); | ||
|
||
$contentBuilder = ContentBuilder::create() | ||
->setId($variant->getCode()) | ||
->setQuantity($item->getQuantity()) | ||
->setItemPrice($this->moneyFormatter->format($item->getDiscountedUnitPrice())) | ||
; | ||
|
||
$this->eventDispatcher->dispatch(ContentBuilder::EVENT_NAME, new BuilderEvent($contentBuilder, $item)); | ||
|
||
$builder->addContent($contentBuilder); | ||
} | ||
|
||
$this->eventDispatcher->dispatch(AddToCartBuilder::EVENT_NAME, new BuilderEvent($builder, $order)); | ||
|
||
$this->tagBag->add(new FbqTag( | ||
Tags::TAG_ADD_TO_CART, | ||
FbqTagInterface::EVENT_ADD_TO_CART, | ||
$builder | ||
), TagBagInterface::SECTION_BODY_END); | ||
} | ||
} |
Oops, something went wrong.