<?php
namespace App\Tracking\Extractor;
use CoreShop\Component\Core\Context\ShopperContextInterface;
use CoreShop\Component\Core\Model\ProductInterface;
use CoreShop\Component\Core\Product\TaxedProductPriceCalculatorInterface;
use CoreShop\Component\Order\Model\PurchasableInterface;
use CoreShop\Component\Product\Model\CategoryInterface;
class ProductExtractor extends \CoreShop\Component\Core\Tracking\Extractor\ProductExtractor
{
public function __construct(
protected TaxedProductPriceCalculatorInterface $taxedPurchasablePriceCalculator,
private ShopperContextInterface $shopperContext,
private int $decimalFactor,
)
{
}
public function updateMetadata($object, $data = []): array
{
$categories = [];
if ($object instanceof ProductInterface) {
$categories = $object->getCategories();
}
/**
* @var PurchasableInterface $object
*/
return array_merge($data, [
'id' => $object->getId(),
'name' => $object->getName(),
'category' => (is_array($categories) && count($categories) > 0) ? $categories[0]->getName() : '',
'sku' => $object instanceof ProductInterface ? $object->getSku() : '',
'price' => $this->taxedPurchasablePriceCalculator->getPrice(
$object,
$this->shopperContext->getContext(),
) / $this->decimalFactor,
'currency' => $this->shopperContext->getCurrency()->getIsoCode(),
'collection' => $object instanceof ProductInterface ? $object->getCollection() : null,
'subcollection' => $object instanceof ProductInterface ? $object->getSubCollection() : null,
'gender' => $object instanceof ProductInterface ? $object->getTypeofwatch() : null,
'categories' => array_map(static function (CategoryInterface $category) {
return [
'id' => $category->getId(),
'name' => $category->getName(),
];
}, is_array($categories) ? $categories : []),
]);
}
}