Skip to content

Commit

Permalink
SEO: Add native DataLayer bridge.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Feb 15, 2022
1 parent 562837e commit fe08387
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/OrderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ final class OrderManager implements OrderManagerInterface
{
private OrderRepository $orderRepository;

private ?Seo $seo = null;


public function __construct(
OrderPaymentClient $paymentClient,
Expand Down Expand Up @@ -162,4 +164,14 @@ public function addFile(

return $file;
}


public function getSeo(): Seo
{
if ($this->seo === null) {
$this->seo = new Seo;
}

return $this->seo;
}
}
57 changes: 57 additions & 0 deletions src/Seo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Baraja\Shop\Order;


use Baraja\EcommerceStandard\DTO\OrderInterface;

final class Seo
{
public function getDataLayer(OrderInterface $order): string
{
return (string) json_encode(
$this->buildDataLayerStructure($order),
JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT,
);
}


/**
* @return array<string, mixed>
*/
private function buildDataLayerStructure(OrderInterface $order): array
{
$products = [];
foreach ($order->getItems() as $item) {
$product = $item->getProduct();
$variant = $item->getVariant();
$products[] = [
'name' => $product->getLabel(),
'id' => (string) $product->getId(),
'price' => $item->getFinalPrice()->getValue(),
'brand' => 'CLEVER MINDS',
'category' => $product->getMainCategory()?->getLabel(),
'variant' => $variant?->getName(),
'quantity' => $item->getCount(),
];
}

return [
'ecommerce' => [
'purchase' => [
'actionField' => [
'id' => $order->getNumber(), // Transaction ID. Required for purchases and refunds.
'affiliation' => 'Online Store',
'revenue' => $order->getPrice()->getValue(), // Total transaction value (incl. tax and shipping)
'tax' => $order->getPriceWithoutVat()->getValue(),
'shipping' => $order->getDeliveryPrice()->getValue(),
// TODO: 'coupon' => 'SUMMER_SALE',
],
'products' => $products,
],
],
];
}
}

0 comments on commit fe08387

Please sign in to comment.