first commit
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License version 3.0
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\PsxMarketingWithGoogle\Provider;
|
||||
|
||||
use Carrier;
|
||||
use Currency;
|
||||
use Language;
|
||||
use PrestaShop\Module\PsxMarketingWithGoogle\Adapter\ConfigurationAdapter;
|
||||
use PrestaShop\Module\PsxMarketingWithGoogle\Builder\CarrierBuilder;
|
||||
use PrestaShop\Module\PsxMarketingWithGoogle\DTO\Carrier as DTOCarrier;
|
||||
use PrestaShop\Module\PsxMarketingWithGoogle\Repository\CarrierRepository;
|
||||
use RangePrice;
|
||||
use RangeWeight;
|
||||
|
||||
class CarrierDataProvider
|
||||
{
|
||||
/**
|
||||
* @var ConfigurationAdapter
|
||||
*/
|
||||
private $configurationAdapter;
|
||||
|
||||
/**
|
||||
* @var CarrierBuilder
|
||||
*/
|
||||
private $carrierBuilder;
|
||||
|
||||
/**
|
||||
* @var CarrierRepository
|
||||
*/
|
||||
private $carrierRepository;
|
||||
|
||||
public function __construct(
|
||||
ConfigurationAdapter $configurationAdapter,
|
||||
CarrierBuilder $carrierBuilder,
|
||||
CarrierRepository $carrierRepository
|
||||
) {
|
||||
$this->configurationAdapter = $configurationAdapter;
|
||||
$this->carrierBuilder = $carrierBuilder;
|
||||
$this->carrierRepository = $carrierRepository;
|
||||
}
|
||||
|
||||
public function getFormattedData(): array
|
||||
{
|
||||
$language = new Language($this->configurationAdapter->get('PS_LANG_DEFAULT'));
|
||||
$currency = new Currency($this->configurationAdapter->get('PS_CURRENCY_DEFAULT'));
|
||||
|
||||
$carriers = Carrier::getCarriers($language->id, false, false, false, null, Carrier::ALL_CARRIERS);
|
||||
|
||||
/** @var DTOCarrier[] $carrierLines */
|
||||
$carrierLines = $this->carrierBuilder->buildCarriers(
|
||||
$carriers,
|
||||
$language,
|
||||
$currency,
|
||||
$this->configurationAdapter->get('PS_WEIGHT_UNIT')
|
||||
);
|
||||
|
||||
return $carrierLines;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $deliveryPriceByRange
|
||||
*
|
||||
* @return false|RangeWeight|RangePrice
|
||||
*
|
||||
* @throws \PrestaShopDatabaseException
|
||||
* @throws \PrestaShopException
|
||||
*/
|
||||
public function getCarrierRange(array $deliveryPriceByRange)
|
||||
{
|
||||
if (isset($deliveryPriceByRange['id_range_weight'])) {
|
||||
return new RangeWeight($deliveryPriceByRange['id_range_weight']);
|
||||
}
|
||||
if (isset($deliveryPriceByRange['id_range_price'])) {
|
||||
return new RangePrice($deliveryPriceByRange['id_range_price']);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License version 3.0
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\PsxMarketingWithGoogle\Provider;
|
||||
|
||||
use Context;
|
||||
use PrestaShop\Module\PsxMarketingWithGoogle\DTO\ConversionEventData;
|
||||
|
||||
class CartEventDataProvider
|
||||
{
|
||||
/**
|
||||
* @var Context
|
||||
*/
|
||||
protected $context;
|
||||
|
||||
public function __construct(Context $context)
|
||||
{
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the items concerned by the transaction
|
||||
* https://developers.google.com/analytics/devguides/collection/gtagjs/enhanced-ecommerce#action-data
|
||||
*/
|
||||
public function getEventData($sendTo, $data): ConversionEventData
|
||||
{
|
||||
$product = $data['product'];
|
||||
$idProductAttribute = isset($data['id_product_attribute']) ? $data['id_product_attribute'] : 0;
|
||||
|
||||
return (new ConversionEventData())
|
||||
->setSendTo($sendTo)
|
||||
->setCurrency($this->context->currency->iso_code)
|
||||
->setValue((string) \Product::getPriceStatic($product->id, true, $idProductAttribute, 2));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License version 3.0
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\PsxMarketingWithGoogle\Provider;
|
||||
|
||||
use Context;
|
||||
use Currency;
|
||||
use Order;
|
||||
use PrestaShop\Module\PsxMarketingWithGoogle\DTO\ConversionEventData;
|
||||
|
||||
class ConversionEventDataProvider
|
||||
{
|
||||
/**
|
||||
* @var Context
|
||||
*/
|
||||
protected $context;
|
||||
|
||||
public function __construct(Context $context)
|
||||
{
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
public function getActionDataByOrderObject(Order $order): ConversionEventData
|
||||
{
|
||||
return (new ConversionEventData())
|
||||
->setTransactionId((string) $order->id)
|
||||
->setValue((string) $order->total_products_wt)
|
||||
->setCurrency((new Currency($order->id_currency))->iso_code);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License version 3.0
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\PsxMarketingWithGoogle\Provider;
|
||||
|
||||
class GoogleTagProvider
|
||||
{
|
||||
/**
|
||||
* html contents (front office)
|
||||
*
|
||||
* @var string|bool
|
||||
*/
|
||||
private $contents;
|
||||
|
||||
/**
|
||||
* Check if the given tag already exist on the shop
|
||||
*
|
||||
* @param string $googleTag eg: AW-253749562
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function checkGoogleTagAlreadyExists(string $googleTag, int $shopId): bool
|
||||
{
|
||||
$this->crawlFront($shopId);
|
||||
|
||||
if (empty($this->contents)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return str_contains($this->contents, $googleTag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Crawl front office
|
||||
*
|
||||
* @param int $shopId
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function crawlFront(int $shopId): void
|
||||
{
|
||||
$shop = \Shop::getShop($shopId);
|
||||
$url = \Tools::getShopProtocol() . $shop[$this->getDomainType()] . $shop['uri'];
|
||||
|
||||
$contents = \Tools::file_get_contents($url);
|
||||
|
||||
$this->contents = $contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the domain type to use depending on ssl
|
||||
*
|
||||
* @return string domain type can be domain_ssl || domain
|
||||
*/
|
||||
private function getDomainType(): string
|
||||
{
|
||||
return \Tools::usingSecureMode() ? 'domain_ssl' : 'domain';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License version 3.0
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\PsxMarketingWithGoogle\Provider;
|
||||
|
||||
use Context;
|
||||
use PrestaShop\Module\PsxMarketingWithGoogle\DTO\ConversionEventData;
|
||||
|
||||
class PageViewEventDataProvider
|
||||
{
|
||||
/**
|
||||
* @var Context
|
||||
*/
|
||||
protected $context;
|
||||
|
||||
public function __construct(Context $context)
|
||||
{
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the items concerned by the transaction
|
||||
* https://developers.google.com/analytics/devguides/collection/gtagjs/enhanced-ecommerce#action-data
|
||||
*
|
||||
* @return ConversionEventData|null
|
||||
*/
|
||||
public function getEventData($sendTo)
|
||||
{
|
||||
if ($this->context->controller instanceof \ProductControllerCore) {
|
||||
/** @var \ProductControllerCore $controller */
|
||||
$controller = $this->context->controller;
|
||||
$product = $controller->getTemplateVarProduct();
|
||||
|
||||
return (new ConversionEventData())
|
||||
->setSendTo($sendTo)
|
||||
->setCurrency($this->context->currency->iso_code)
|
||||
->setValue((string) $product['price_amount']);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License version 3.0
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\PsxMarketingWithGoogle\Provider;
|
||||
|
||||
use Context;
|
||||
use PrestaShop\Module\PsxMarketingWithGoogle\DTO\Remarketing\ProductData;
|
||||
|
||||
class ProductDataProvider
|
||||
{
|
||||
/**
|
||||
* @var Context
|
||||
*/
|
||||
protected $context;
|
||||
|
||||
public function __construct(Context $context)
|
||||
{
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
public function getProductDataByProductArray(array $product): ProductData
|
||||
{
|
||||
$productData = new ProductData();
|
||||
|
||||
$productData->setId(implode(
|
||||
'-',
|
||||
[
|
||||
(int) $product['id_product'],
|
||||
(int) $product['id_product_attribute'],
|
||||
]
|
||||
));
|
||||
$productData->setPrice((float) $product['product_price_wt']);
|
||||
$productData->setQuantity((int) $product['product_quantity']);
|
||||
|
||||
return $productData;
|
||||
}
|
||||
|
||||
public function getProductDataByProductObject(array $params): ProductData
|
||||
{
|
||||
$product = $params['product'];
|
||||
$productData = new ProductData();
|
||||
|
||||
$productData->setId(implode(
|
||||
'-',
|
||||
[
|
||||
(int) $product->id,
|
||||
(int) $params['id_product_attribute'],
|
||||
]
|
||||
));
|
||||
|
||||
$productData->setPrice($product->price);
|
||||
$productData->setQuantity($params['quantity']);
|
||||
|
||||
return $productData;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License version 3.0
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\PsxMarketingWithGoogle\Provider;
|
||||
|
||||
use Context;
|
||||
use Order;
|
||||
use PrestaShop\Module\PsxMarketingWithGoogle\Adapter\ConfigurationAdapter;
|
||||
use PrestaShop\Module\PsxMarketingWithGoogle\Config\Config;
|
||||
use PrestaShop\Module\PsxMarketingWithGoogle\DTO\Remarketing\PurchaseEventData;
|
||||
use PrestaShop\Module\PsxMarketingWithGoogle\Repository\CountryRepository;
|
||||
use PrestaShop\Module\PsxMarketingWithGoogle\Repository\LanguageRepository;
|
||||
|
||||
class PurchaseEventDataProvider
|
||||
{
|
||||
/**
|
||||
* @var ProductDataProvider
|
||||
*/
|
||||
protected $productDataProvider;
|
||||
|
||||
/**
|
||||
* @var Context
|
||||
*/
|
||||
protected $context;
|
||||
|
||||
/**
|
||||
* @var ConfigurationAdapter
|
||||
*/
|
||||
private $configurationAdapter;
|
||||
|
||||
/**
|
||||
* @var LanguageRepository
|
||||
*/
|
||||
private $languageRepository;
|
||||
|
||||
/**
|
||||
* @var CountryRepository
|
||||
*/
|
||||
private $countryRepository;
|
||||
|
||||
public function __construct(
|
||||
ProductDataProvider $productDataProvider,
|
||||
Context $context,
|
||||
ConfigurationAdapter $configurationAdapter,
|
||||
LanguageRepository $languageRepository,
|
||||
CountryRepository $countryRepository
|
||||
) {
|
||||
$this->productDataProvider = $productDataProvider;
|
||||
$this->context = $context;
|
||||
$this->configurationAdapter = $configurationAdapter;
|
||||
$this->languageRepository = $languageRepository;
|
||||
$this->countryRepository = $countryRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the items concerned by the transaction
|
||||
*
|
||||
* @see https://support.google.com/google-ads/answer/9028614
|
||||
*/
|
||||
public function getEventData($sendTo, Order $order): PurchaseEventData
|
||||
{
|
||||
$purchaseData = new PurchaseEventData();
|
||||
// Common details
|
||||
$purchaseData->setSendTo($sendTo);
|
||||
$purchaseData->setCurrency($this->context->currency->iso_code);
|
||||
$purchaseData->setValue((string) $order->total_products_wt);
|
||||
$purchaseData->setTransactionId($order->reference);
|
||||
|
||||
// CwCD Parameters
|
||||
$purchaseData->setDiscount((float) $order->total_discounts_tax_incl);
|
||||
$purchaseData->setAwMerchandId((int) $this->configurationAdapter->get(Config::REMARKETING_CONVERSION_MERCHANT_GMC_ID));
|
||||
$purchaseData->setAwFeedCountry(
|
||||
$this->countryRepository->getIsoById(
|
||||
$this->context->country->id
|
||||
)
|
||||
);
|
||||
$purchaseData->setAwFeedLanguage(
|
||||
$this->languageRepository->getIsoById(
|
||||
$this->context->language->id
|
||||
)
|
||||
);
|
||||
|
||||
$items = [];
|
||||
foreach ($order->getCartProducts() as $product) {
|
||||
$items[] = $this->productDataProvider->getProductDataByProductArray($product);
|
||||
}
|
||||
|
||||
$purchaseData->setItems($items);
|
||||
|
||||
return $purchaseData;
|
||||
}
|
||||
}
|
||||
11
modules/psxmarketingwithgoogle/classes/Provider/index.php
Normal file
11
modules/psxmarketingwithgoogle/classes/Provider/index.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
|
||||
header('Location: ../');
|
||||
exit;
|
||||
Reference in New Issue
Block a user