src/Tracking/Extractor/ProductExtractor.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Tracking\Extractor;
  3. use CoreShop\Component\Core\Context\ShopperContextInterface;
  4. use CoreShop\Component\Core\Model\ProductInterface;
  5. use CoreShop\Component\Core\Product\TaxedProductPriceCalculatorInterface;
  6. use CoreShop\Component\Order\Model\PurchasableInterface;
  7. use CoreShop\Component\Product\Model\CategoryInterface;
  8. class ProductExtractor extends \CoreShop\Component\Core\Tracking\Extractor\ProductExtractor
  9. {
  10.     public function __construct(
  11.         protected TaxedProductPriceCalculatorInterface $taxedPurchasablePriceCalculator,
  12.         private ShopperContextInterface                $shopperContext,
  13.         private int                                    $decimalFactor,
  14.     )
  15.     {
  16.     }
  17.     public function updateMetadata($object$data = []): array
  18.     {
  19.         $categories = [];
  20.         if ($object instanceof ProductInterface) {
  21.             $categories $object->getCategories();
  22.         }
  23.         /**
  24.          * @var PurchasableInterface $object
  25.          */
  26.         return array_merge($data, [
  27.             'id' => $object->getId(),
  28.             'name' => $object->getName(),
  29.             'category' => (is_array($categories) && count($categories) > 0) ? $categories[0]->getName() : '',
  30.             'sku' => $object instanceof ProductInterface $object->getSku() : '',
  31.             'price' => $this->taxedPurchasablePriceCalculator->getPrice(
  32.                     $object,
  33.                     $this->shopperContext->getContext(),
  34.                 ) / $this->decimalFactor,
  35.             'currency' => $this->shopperContext->getCurrency()->getIsoCode(),
  36.             'collection' => $object instanceof ProductInterface ?  $object->getCollection() : null,
  37.             'subcollection' => $object instanceof ProductInterface $object->getSubCollection() : null,
  38.             'gender' => $object instanceof ProductInterface $object->getTypeofwatch() : null,
  39.             'categories' => array_map(static function (CategoryInterface $category) {
  40.                 return [
  41.                     'id' => $category->getId(),
  42.                     'name' => $category->getName(),
  43.                 ];
  44.             }, is_array($categories) ? $categories : []),
  45.         ]);
  46.     }
  47. }