generated from baraja-core/template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
562837e
commit fe08387
Showing
2 changed files
with
69 additions
and
0 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
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,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, | ||
], | ||
], | ||
]; | ||
} | ||
} |