PHP code example of thelia / google-tag-manager-module

1. Go to this page and download the library: Download thelia/google-tag-manager-module library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thelia / google-tag-manager-module example snippets




declare(strict_types=1);

namespace FlexyBundle\Service\GoogleTag;

use GoogleTagManager\Service\GoogleTagService;
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Thelia\Model\Country;
use Thelia\Model\Currency;
use Thelia\Model\Lang;
use Thelia\Model\OrderProduct;
use Thelia\Model\Product;
use Thelia\Model\ProductSaleElements;

#[AsDecorator(decorates: GoogleTagService::class, onInvalid: ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]
final class SingleUnitGoogleTagService extends GoogleTagService
{
    public function getProductItem(
        Product $product,
        Lang $lang,
        Currency $currency,
        ?ProductSaleElements $pse = null,
        $quantity = null,
        $itemList = false,
        $taxed = false,
        ?Country $country = null
    ): array {
        return $this->asSingleUnit(
            parent::getProductItem($product, $lang, $currency, $pse, $quantity, $itemList, $taxed, $country)
        );
    }

    public function getOrderProductItem(
        OrderProduct $orderProduct,
        Lang $lang,
        Currency $currency,
        $quantity = null,
        $itemList = false,
        $taxed = false,
        ?Country $country = null
    ): array {
        return $this->asSingleUnit(
            parent::getOrderProductItem($orderProduct, $lang, $currency, $quantity, $itemList, $taxed, $country)
        );
    }

    private function asSingleUnit(array $item): array
    {
        $item['quantity'] = 1;

        return $item;
    }
}

$this->dispatchBrowserEvent('addPseToCart', [
    'pse' => $this->pseId,
    'quantity' => $quantity,
]);