Skip to content

Commit

Permalink
Merge pull request #14 from Datatrics/1.3.2
Browse files Browse the repository at this point in the history
1.3.2
  • Loading branch information
Marvin-Magmodules authored Apr 20, 2021
2 parents de1b720 + 73d0080 commit ad8ade7
Show file tree
Hide file tree
Showing 25 changed files with 282 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Api/Profile/DataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface DataInterface extends ExtensibleDataInterface
const FIRSTNAME = 'firstname';
const LASTNAME = 'lastname';
const NAME = 'name';
const EMAIL = 'datatrics_email';
const EMAIL = 'email';
const COMPANY = 'company';
const ADDRESS = 'address';
const COUNTRY = 'country';
Expand Down
79 changes: 79 additions & 0 deletions Controller/Cart/Get.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Copyright © Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Datatrics\Connect\Controller\Cart;

use Magento\Checkout\Model\SessionFactory as Session;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\ResultInterface;
use Datatrics\Connect\ViewModel\PreProcessor;

/**
* Class Get
* Ajax controller to get queued events
*/
class Get extends Action implements HttpPostActionInterface
{

/**
* @var JsonFactory
*/
private $resultJsonFactory;

/**
* @var Session
*/
private $checkoutSession;

/**
* @var PreProcessor
*/
private $preProcessor;

/**
* Get constructor.
*
* @param Context $context
* @param JsonFactory $resultJsonFactory
* @param Session $checkoutSession
* @param PreProcessor $preProcessor
*/
public function __construct(
Context $context,
JsonFactory $resultJsonFactory,
Session $checkoutSession,
PreProcessor $preProcessor
) {
$this->resultJsonFactory = $resultJsonFactory;
$this->checkoutSession = $checkoutSession;
$this->preProcessor = $preProcessor;
parent::__construct($context);
}

/**
* @return ResponseInterface|Json|ResultInterface
*/
public function execute()
{
$result = $this->resultJsonFactory->create();
$html = '';
if ($this->checkoutSession->create()->getCartTrigger()) {
$html = $this->preProcessor->getTrack(
'Datatrics_Connect::cart.phtml',
'cart'
);
}
$this->checkoutSession->create()->setCartTrigger(false);
$result->setData($html);
return $result;
}
}
4 changes: 3 additions & 1 deletion Model/ProductData/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ public function getProductData(int $storeId = 0, array $entityIds = []): array
$result = [];
foreach ($this->collectProductData($storeId) as $entityId => $productData) {
$this->addImageData($storeId, $entityId, $productData);
$this->addCategoryData($productData);
if (isset($productData['category'])) {
$this->addCategoryData($productData);
}
foreach ($this->resultMap as $index => $attr) {
$result[$entityId][$index] = $productData[$attr] ?? '';
}
Expand Down
5 changes: 4 additions & 1 deletion Plugin/CustomerGridCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public function afterSearch($intercepter, $collection)
['datatrics_profile' => $leftJoinTableName],
"datatrics_profile.customer_id = main_table.entity_id",
['status']
)->group('main_table.entity_id');
);
$where = $collection->getSelect()->getPart(\Magento\Framework\DB\Select::WHERE);

$collection->getSelect()->setPart(\Magento\Framework\DB\Select::WHERE, $where);
$collection->addFilterToMap('status', 'datatrics_profile.status');
}
return $collection;
Expand Down
74 changes: 74 additions & 0 deletions Plugin/Quote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Copyright © Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Datatrics\Connect\Plugin;

use Magento\Catalog\Model\Product;
use Magento\Checkout\Model\SessionFactory as Session;
use Magento\Quote\Model\Quote as QuoteModel;
use Magento\Quote\Model\Quote\Item;

/**
* Class Quote
* Plugin for quote model
*/
class Quote
{
const CART_TRIGGER = 'TriggerCart';

/**
* @var Session
*/
private $checkoutSession;

/**
* Quote constructor.
*
* @param Session $checkoutSession
*/
public function __construct(
Session $checkoutSession
) {
$this->checkoutSession = $checkoutSession;
}

/**
* Fire event after item was removed from cart
*
* @param QuoteModel $subject
* @param QuoteModel $result
* @param int $itemId
*
* @return QuoteModel
*/
public function afterRemoveItem(
QuoteModel $subject,
QuoteModel $result,
int $itemId
) {
$this->checkoutSession->create()->setCartTrigger(true);
return $result;
}

/**
* Fire event after item was added to the cart (only after post request)
*
* @param QuoteModel $subject
* @param Item $result
* @param Product $product
*
* @return Item
*/
public function afterAddProduct(
QuoteModel $subject,
Item $result,
Product $product
) {
$this->checkoutSession->create()->setCartTrigger(true);
return $result;
}
}
6 changes: 3 additions & 3 deletions Service/Pixel/TemplatePreparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ private function applyRecursiveParameters(string $html, array $recursive)
$positionEnd = strpos(
$html,
sprintf('{{%s end}}', $key)
) - strlen(sprintf('{{%s start}}', $key));
$html = str_replace(sprintf('{{%s start}}', $key), '', $html, $count);
$html = str_replace(sprintf('{{%s end}}', $key), '', $html, $count);
) - $positionStart + strlen(sprintf('{{%s end}}', $key));
$toReplace = substr($html, $positionStart, $positionEnd);
$replace = '';
foreach ($values as $subValue) {
$replace .= $this->applySimpleParameters($toReplace, $subValue);
}
$replace = str_replace(sprintf('{{%s start}}', $key), '', $replace);
$replace = str_replace(sprintf('{{%s end}}', $key), '', $replace);
$html = str_replace($toReplace, $replace, $html, $count);
}
return $html;
Expand Down
2 changes: 1 addition & 1 deletion Service/Pixel/VariableCollector/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function execute(): array
$variables = [
'products' => []
];
foreach ($this->cart->getQuote()->getAllItems() as $item) {
foreach ($this->cart->getQuote()->getAllVisibleItems() as $item) {
$variables['products'][] = [
'sku' => $item->getSku(),
'name' => $item->getName(),
Expand Down
2 changes: 1 addition & 1 deletion Service/Pixel/VariableCollector/Conversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function execute(): array
'shipping' => $order->getShippingAmount(),
'discount' => $order->getDiscountAmount()
];
foreach ($order->getAllItems() as $item) {
foreach ($order->getAllVisibleItems() as $item) {
$result['products'][] = [
'sku' => $item->getSku(),
'name' => $item->getName(),
Expand Down
25 changes: 23 additions & 2 deletions ViewModel/PreProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@

namespace Datatrics\Connect\ViewModel;

use Magento\Framework\View\Element\Block\ArgumentInterface;
use Datatrics\Connect\Api\Config\System\TrackingInterface as TrackingConfigRepository;
use Datatrics\Connect\Service\Pixel\TemplatePreparator;
use Datatrics\Connect\Service\Pixel\TemplateResolver;
use Datatrics\Connect\Api\Config\System\TrackingInterface as TrackingConfigRepository;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Framework\UrlInterface;

/**
* PreProcessor data class
*/
class PreProcessor implements ArgumentInterface
{
/**
*
*/
const URL_PATH = 'datatrics/cart/get';

/**
* @var TemplatePreparator
Expand All @@ -33,6 +38,11 @@ class PreProcessor implements ArgumentInterface
*/
private $variableProcessors;

/**
* @var UrlInterface
*/
private $urlBuilder;

/**
* @var TrackingConfigRepository
*/
Expand All @@ -43,17 +53,20 @@ class PreProcessor implements ArgumentInterface
* @param TemplatePreparator $templatePreparator
* @param TemplateResolver $templateResolver
* @param TrackingConfigRepository $trackingConfigRepository
* @param UrlInterface $urlBuilder
* @param mixed $variableProcessors
*/
public function __construct(
TemplatePreparator $templatePreparator,
TemplateResolver $templateResolver,
TrackingConfigRepository $trackingConfigRepository,
UrlInterface $urlBuilder,
$variableProcessors
) {
$this->templatePreparator = $templatePreparator;
$this->templateResolver = $templateResolver;
$this->variableProcessors = $variableProcessors;
$this->urlBuilder = $urlBuilder;
$this->trackingConfigRepository = $trackingConfigRepository;
}

Expand All @@ -76,4 +89,12 @@ public function getTrack(
$variables = $this->variableProcessors[$variableProcessor]->execute();
return $this->templatePreparator->execute($html, $variables);
}

/**
* @return string
*/
public function getAjaxUrl()
{
return $this->urlBuilder->getUrl(self::URL_PATH);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "datatrics/magento2-integration",
"description": "Datatrics Connect extension for Magento 2",
"type": "magento2-module",
"version": "1.3.1",
"version": "1.3.2",
"license": [
"BSD-2-Clause"
],
Expand Down
2 changes: 1 addition & 1 deletion etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<default>
<datatrics_connect_general>
<general>
<version>v1.3.1</version>
<version>v1.3.2</version>
<enable>0</enable>
<source>Magento 2</source>
<debug>0</debug>
Expand Down
26 changes: 26 additions & 0 deletions etc/csp_whitelist.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright © Magmodules.eu. All rights reserved.
~ See COPYING.txt for license details.
-->
<csp_whitelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Csp:etc/csp_whitelist.xsd">
<policies>
<policy id="script-src">
<values>
<value id="datatrics" type="host">*.datatrics.com</value>
</values>
</policy>
<policy id="connect-src">
<values>
<value id="datatrics_connect" type="host">*.datatrics.com</value>
</values>
</policy>
<policy id="img-src">
<values>
<value id="www-magmodules" type="host">www.magmodules.eu</value>
<value id="datatrics_image" type="host">*.datatrics.com</value>
</values>
</policy>
</policies>
</csp_whitelist>
4 changes: 2 additions & 2 deletions etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@
xsi:type="varchar"
length="50"
nullable="true"
disabled="true"
onCreate="migrateDataFrom(datatrics_email)"
comment="Email"/>
<column name="datatrics_email"
xsi:type="varchar"
length="50"
nullable="true"
onCreate="migrateDataFrom(email)"
disabled="true"
comment="Email"/>
<column name="company"
xsi:type="varchar"
Expand Down
2 changes: 1 addition & 1 deletion etc/db_schema_whitelist.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@
"DATATRICS_PROFILE_STORE_ID_STORE_STORE_ID": true
}
}
}
}
5 changes: 5 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,9 @@
<plugin name="orderPlaceAfterPlugin" type="Datatrics\Connect\Plugin\OrderPlace" sortOrder="99" />
</type>

<type name="Magento\Quote\Model\Quote">
<plugin name="quoteChanges"
type="Datatrics\Connect\Plugin\Quote" sortOrder="1" disabled="false" />
</type>

</config>
14 changes: 14 additions & 0 deletions etc/frontend/routes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="datatrics" frontName="datatrics">
<module name="Datatrics_Connect"/>
</route>
</router>
</config>
2 changes: 1 addition & 1 deletion view/frontend/layout/catalog_category_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<body>
<referenceBlock name="head.additional">
<block name="datatrics.pixel.category"
ifconfig="datatrics_connect/tracking/category"
ifconfig="datatrics_connect_tracking/tracking/category"
class="Magento\Framework\View\Element\Template"
after="datatrics.pixel.base"
template="Datatrics_Connect::pixel.phtml">
Expand Down
Loading

0 comments on commit ad8ade7

Please sign in to comment.