diff --git a/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerCompanySelector.php b/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerCompanySelector.php new file mode 100644 index 000000000..abdd81d2a --- /dev/null +++ b/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerCompanySelector.php @@ -0,0 +1,120 @@ + + * @copyright 2024 Smile + * @license Open Software License ("OSL") v. 3.0 + */ +namespace Smile\ElasticsuiteAnalytics\Block\Adminhtml\Report; + +use Magento\Framework\Api\SearchCriteriaBuilder; +use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Module\Manager as ModuleManager; +use Magento\Framework\View\Element\Template; +use Magento\Framework\View\Element\Template\Context; + +/** + * Block used to display customer company selector in reports. + * + * @category Smile + * @package Smile\ElasticsuiteAnalytics + * @author Vadym Honcharuk + */ +class CustomerCompanySelector extends Template +{ + /** + * Company status configuration path. + * + * @var string + */ + const CONFIG_IS_B2B_COMPANY_ACTIVE_XPATH = 'btob/website_configuration/company_active'; + + /** + * @var ScopeConfigInterface + */ + protected $scopeConfig; + + /** + * @var SearchCriteriaBuilder + */ + protected $searchCriteriaBuilder; + + /** + * @var \Magento\Company\Api\CompanyRepositoryInterface|null + */ + private $companyRepository = null; + + /** + * CustomerCompanySelector constructor. + * + * @SuppressWarnings(PHPMD.ElseExpression) + * + * @param Context $context The template context. + * @param ModuleManager $moduleManager Module manager. + * @param ScopeConfigInterface $scopeConfig Scope configuration. + * @param SearchCriteriaBuilder $searchCriteriaBuilder The search criteria builder. + * @param array $data Additional block data. + * @throws LocalizedException + */ + public function __construct( + Context $context, + ModuleManager $moduleManager, + ScopeConfigInterface $scopeConfig, + SearchCriteriaBuilder $searchCriteriaBuilder, + array $data = [] + ) { + $this->scopeConfig = $scopeConfig; + $this->searchCriteriaBuilder = $searchCriteriaBuilder; + + // Check if Magento_Company module is enabled before attempting to load the repository. + if ($moduleManager->isEnabled('Magento_Company')) { + if (interface_exists('\Magento\Company\Api\CompanyRepositoryInterface')) { + $this->companyRepository = ObjectManager::getInstance()->get( + \Magento\Company\Api\CompanyRepositoryInterface::class + ); + } else { + throw new LocalizedException(__('CompanyRepositoryInterface is not available.')); + } + } + + parent::__construct($context, $data); + } + + /** + * Check if the Company feature is enabled. + * + * @return bool + */ + public function isCompanyEnabled() + { + return $this->scopeConfig->isSetFlag( + self::CONFIG_IS_B2B_COMPANY_ACTIVE_XPATH, + \Magento\Store\Model\ScopeInterface::SCOPE_STORE + ); + } + + /** + * Get the list of companies if the Company feature is enabled. + * + * @return CompanyInterface[]|array + * @throws LocalizedException + */ + public function getCompaniesList() + { + if ($this->isCompanyEnabled()) { + $searchCriteria = $this->searchCriteriaBuilder->create(); + + return $this->companyRepository->getList($searchCriteria)->getItems(); // Fetch company list. + } + + return []; + } +} diff --git a/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerGroupSelector.php b/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerGroupSelector.php new file mode 100644 index 000000000..99170e56d --- /dev/null +++ b/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerGroupSelector.php @@ -0,0 +1,60 @@ + + * @copyright 2024 Smile + * @license Open Software License ("OSL") v. 3.0 + */ +namespace Smile\ElasticsuiteAnalytics\Block\Adminhtml\Report; + +use Magento\Framework\View\Element\Template; +use Magento\Customer\Model\ResourceModel\Group\CollectionFactory; + +/** + * Block used to display customer group selector in reports. + * + * @SuppressWarnings(PHPMD.CamelCasePropertyName) + * + * @category Smile + * @package Smile\ElasticsuiteAnalytics + * @author Vadym Honcharuk + */ +class CustomerGroupSelector extends Template +{ + /** + * @var CollectionFactory + */ + protected $customerGroupCollectionFactory; + + /** + * CustomerGroupSelector constructor. + * + * @param Template\Context $context The context of the template. + * @param CollectionFactory $customerGroupCollectionFactory Factory for creating customer group collection. + * @param array $data Additional block data. + */ + public function __construct( + Template\Context $context, + CollectionFactory $customerGroupCollectionFactory, + array $data = [] + ) { + $this->customerGroupCollectionFactory = $customerGroupCollectionFactory; + parent::__construct($context, $data); + } + + /** + * Get customer groups in an option array format. + * + * @return array + */ + public function getCustomerGroups() + { + return $this->customerGroupCollectionFactory->create()->toOptionArray(); + } +} diff --git a/src/module-elasticsuite-analytics/Model/Report/Context.php b/src/module-elasticsuite-analytics/Model/Report/Context.php index b326d5585..74ffd9d49 100644 --- a/src/module-elasticsuite-analytics/Model/Report/Context.php +++ b/src/module-elasticsuite-analytics/Model/Report/Context.php @@ -66,6 +66,26 @@ public function getStoreId() return $this->request->getParam('store'); } + /** + * Get customer group ID. + * + * @return mixed + */ + public function getCustomerGroupId() + { + return $this->request->getParam('customer_group'); + } + + /** + * Get customer company ID. + * + * @return mixed + */ + public function getCustomerCompanyId() + { + return $this->request->getParam('company_id'); + } + /** * Get date range. * diff --git a/src/module-elasticsuite-analytics/Model/Report/Event/CustomerCompanyIdFilterQueryProvider.php b/src/module-elasticsuite-analytics/Model/Report/Event/CustomerCompanyIdFilterQueryProvider.php new file mode 100644 index 000000000..ad3cbe19d --- /dev/null +++ b/src/module-elasticsuite-analytics/Model/Report/Event/CustomerCompanyIdFilterQueryProvider.php @@ -0,0 +1,74 @@ + + * @copyright 2024 Smile + * @license Open Software License ("OSL") v. 3.0 + */ +namespace Smile\ElasticsuiteAnalytics\Model\Report\Event; + +use Smile\ElasticsuiteAnalytics\Model\Report\QueryProviderInterface; +use Smile\ElasticsuiteCore\Search\Request\QueryInterface; +use Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory; +use Smile\ElasticsuiteAnalytics\Model\Report\Context; + +/** + * Customer company id filter query provider. + * + * @category Smile + * @package Smile\ElasticsuiteAnalytics + */ +class CustomerCompanyIdFilterQueryProvider implements QueryProviderInterface +{ + /** + * @var QueryFactory + */ + private $queryFactory; + + /** + * @var Context + */ + private $context; + + /** + * CustomerCompanyIdFilterQueryProvider constructor. + * + * @param QueryFactory $queryFactory Query factory. + * @param Context $context Report context. + */ + public function __construct(QueryFactory $queryFactory, Context $context) + { + $this->queryFactory = $queryFactory; + $this->context = $context; + } + + /** + * {@inheritDoc} + */ + public function getQuery() + { + // Get customer company ID from the context. + $customerCompanyId = $this->context->getCustomerCompanyId(); + + // Check if customer company ID is set and not 'all'. + if ($customerCompanyId !== 'all' && $customerCompanyId !== null) { + // Return a TERM query for the customer company ID. + return $this->queryFactory->create( + QueryInterface::TYPE_TERM, + [ + 'field' => 'customer.company_id', + 'value' => (int) $customerCompanyId, + ] + ); + } + + // If 'all' is selected or no customer company ID is set, return null (no filtering). + return null; + } +} diff --git a/src/module-elasticsuite-analytics/Model/Report/Event/CustomerGroupIdFilterQueryProvider.php b/src/module-elasticsuite-analytics/Model/Report/Event/CustomerGroupIdFilterQueryProvider.php new file mode 100644 index 000000000..74fe09112 --- /dev/null +++ b/src/module-elasticsuite-analytics/Model/Report/Event/CustomerGroupIdFilterQueryProvider.php @@ -0,0 +1,74 @@ + + * @copyright 2024 Smile + * @license Open Software License ("OSL") v. 3.0 + */ +namespace Smile\ElasticsuiteAnalytics\Model\Report\Event; + +use Smile\ElasticsuiteAnalytics\Model\Report\QueryProviderInterface; +use Smile\ElasticsuiteCore\Search\Request\QueryInterface; +use Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory; +use Smile\ElasticsuiteAnalytics\Model\Report\Context; + +/** + * Customer group id filter query provider. + * + * @category Smile + * @package Smile\ElasticsuiteAnalytics + */ +class CustomerGroupIdFilterQueryProvider implements QueryProviderInterface +{ + /** + * @var QueryFactory + */ + private $queryFactory; + + /** + * @var Context + */ + private $context; + + /** + * CustomerGroupIdFilterQueryProvider constructor. + * + * @param QueryFactory $queryFactory Query factory. + * @param Context $context Report context. + */ + public function __construct(QueryFactory $queryFactory, Context $context) + { + $this->queryFactory = $queryFactory; + $this->context = $context; + } + + /** + * {@inheritDoc} + */ + public function getQuery() + { + // Get customer group ID from the context. + $customerGroupId = $this->context->getCustomerGroupId(); + + // Check if customer group ID is set and not 'all'. + if ($customerGroupId !== 'all' && $customerGroupId !== null) { + // Return a TERM query for the customer group ID. + return $this->queryFactory->create( + QueryInterface::TYPE_TERM, + [ + 'field' => 'customer.group_id', + 'value' => (int) $customerGroupId, + ] + ); + } + + // If 'all' is selected or no customer group ID is set, return null (no filtering). + return null; + } +} diff --git a/src/module-elasticsuite-analytics/Model/Report/Session/CustomerCompanyIdFilterQueryProvider.php b/src/module-elasticsuite-analytics/Model/Report/Session/CustomerCompanyIdFilterQueryProvider.php new file mode 100644 index 000000000..6b933c70f --- /dev/null +++ b/src/module-elasticsuite-analytics/Model/Report/Session/CustomerCompanyIdFilterQueryProvider.php @@ -0,0 +1,81 @@ + + * @copyright 2024 Smile + * @license Open Software License ("OSL") v. 3.0 + */ +namespace Smile\ElasticsuiteAnalytics\Model\Report\Session; + +use Smile\ElasticsuiteAnalytics\Model\Report\QueryProviderInterface; +use Smile\ElasticsuiteCore\Search\Request\QueryInterface; +use Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory; +use Smile\ElasticsuiteAnalytics\Model\Report\Context; + +/** + * Customer company id filter query provider. + * + * @category Smile + * @package Smile\ElasticsuiteAnalytics + */ +class CustomerCompanyIdFilterQueryProvider implements QueryProviderInterface +{ + /** + * @var QueryFactory + */ + private $queryFactory; + + /** + * @var Context + */ + private $context; + + /** + * CustomerCompanyIdFilterQueryProvider constructor. + * + * @param QueryFactory $queryFactory Query factory. + * @param Context $context Report context. + */ + public function __construct(QueryFactory $queryFactory, Context $context) + { + $this->queryFactory = $queryFactory; + $this->context = $context; + } + + /** + * {@inheritDoc} + */ + public function getQuery() + { + // Get customer company ID from the context. + $customerCompanyId = $this->context->getCustomerCompanyId(); + + // Check if customer company ID is set and not 'all'. + if ($customerCompanyId !== 'all' && $customerCompanyId !== null) { + // Return a TERM query for the customer company ID. + return $this->queryFactory->create( + QueryInterface::TYPE_BOOL, + [ + 'must' => [ + $this->queryFactory->create( + QueryInterface::TYPE_TERM, + [ + 'field' => 'customer_company_id', + 'value' => (int) $customerCompanyId, + ] + ), + ], + ] + ); + } + + // If 'all' is selected or no customer company ID is set, return null (no filtering). + return null; + } +} diff --git a/src/module-elasticsuite-analytics/Model/Report/Session/CustomerGroupIdFilterQueryProvider.php b/src/module-elasticsuite-analytics/Model/Report/Session/CustomerGroupIdFilterQueryProvider.php new file mode 100644 index 000000000..2a5f5b48a --- /dev/null +++ b/src/module-elasticsuite-analytics/Model/Report/Session/CustomerGroupIdFilterQueryProvider.php @@ -0,0 +1,81 @@ + + * @copyright 2024 Smile + * @license Open Software License ("OSL") v. 3.0 + */ +namespace Smile\ElasticsuiteAnalytics\Model\Report\Session; + +use Smile\ElasticsuiteAnalytics\Model\Report\QueryProviderInterface; +use Smile\ElasticsuiteCore\Search\Request\QueryInterface; +use Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory; +use Smile\ElasticsuiteAnalytics\Model\Report\Context; + +/** + * Customer group id filter query provider. + * + * @category Smile + * @package Smile\ElasticsuiteAnalytics + */ +class CustomerGroupIdFilterQueryProvider implements QueryProviderInterface +{ + /** + * @var QueryFactory + */ + private $queryFactory; + + /** + * @var Context + */ + private $context; + + /** + * CustomerGroupIdFilterQueryProvider constructor. + * + * @param QueryFactory $queryFactory Query factory. + * @param Context $context Report context. + */ + public function __construct(QueryFactory $queryFactory, Context $context) + { + $this->queryFactory = $queryFactory; + $this->context = $context; + } + + /** + * {@inheritDoc} + */ + public function getQuery() + { + // Get customer group ID from the context. + $customerGroupId = $this->context->getCustomerGroupId(); + + // Check if customer group ID is set and not 'all'. + if ($customerGroupId !== 'all' && $customerGroupId !== null) { + // Return a TERM query for the customer group ID. + return $this->queryFactory->create( + QueryInterface::TYPE_BOOL, + [ + 'must' => [ + $this->queryFactory->create( + QueryInterface::TYPE_TERM, + [ + 'field' => 'customer_group_id', + 'value' => (int) $customerGroupId, + ] + ), + ], + ] + ); + } + + // If 'all' is selected or no customer group ID is set, return null (no filtering). + return null; + } +} diff --git a/src/module-elasticsuite-analytics/etc/di.xml b/src/module-elasticsuite-analytics/etc/di.xml index ef4acea8b..63c07dbe0 100755 --- a/src/module-elasticsuite-analytics/etc/di.xml +++ b/src/module-elasticsuite-analytics/etc/di.xml @@ -23,6 +23,8 @@ Smile\ElasticsuiteAnalytics\Model\Report\Event\DateFilterQueryProvider + Smile\ElasticsuiteAnalytics\Model\Report\Event\CustomerGroupIdFilterQueryProvider + Smile\ElasticsuiteAnalytics\Model\Report\Event\CustomerCompanyIdFilterQueryProvider @@ -41,6 +43,8 @@ Smile\ElasticsuiteAnalytics\Model\Report\Session\DateFilterQueryProvider + Smile\ElasticsuiteAnalytics\Model\Report\Session\CustomerGroupIdFilterQueryProvider + Smile\ElasticsuiteAnalytics\Model\Report\Session\CustomerCompanyIdFilterQueryProvider tracking_log_session @@ -51,7 +55,7 @@ Smile\ElasticsuiteAnalytics\Model\Search\Usage\Kpi\ConversionRates\SearchRequestBuilder - + @@ -60,10 +64,12 @@ Smile\ElasticsuiteAnalytics\Model\Report\Event\DateFilterQueryProvider + Smile\ElasticsuiteAnalytics\Model\Report\Event\CustomerGroupIdFilterQueryProvider + Smile\ElasticsuiteAnalytics\Model\Report\Event\CustomerCompanyIdFilterQueryProvider - + Smile\ElasticsuiteAnalytics\Model\Search\Usage\Terms\PopularTerms\SearchRequestBuilder @@ -72,7 +78,7 @@ - + @@ -82,10 +88,12 @@ Smile\ElasticsuiteAnalytics\Model\Search\Usage\Terms\SpellcheckedTerms\QueryProvider Smile\ElasticsuiteAnalytics\Model\Report\Event\DateFilterQueryProvider + Smile\ElasticsuiteAnalytics\Model\Report\Event\CustomerGroupIdFilterQueryProvider + Smile\ElasticsuiteAnalytics\Model\Report\Event\CustomerCompanyIdFilterQueryProvider - + Smile\ElasticsuiteAnalytics\Model\Search\Usage\Terms\SpellcheckedTerms\SearchRequestBuilder @@ -94,7 +102,7 @@ - + @@ -104,10 +112,12 @@ Smile\ElasticsuiteAnalytics\Model\Report\Session\DateFilterQueryProvider + Smile\ElasticsuiteAnalytics\Model\Report\Session\CustomerGroupIdFilterQueryProvider + Smile\ElasticsuiteAnalytics\Model\Report\Session\CustomerCompanyIdFilterQueryProvider - + Smile\ElasticsuiteAnalytics\Model\Search\Usage\Terms\NoResultTerms\SearchRequestBuilder @@ -122,6 +132,8 @@ Smile\ElasticsuiteAnalytics\Model\Report\Session\DateFilterQueryProvider + Smile\ElasticsuiteAnalytics\Model\Report\Session\CustomerGroupIdFilterQueryProvider + Smile\ElasticsuiteAnalytics\Model\Report\Session\CustomerCompanyIdFilterQueryProvider diff --git a/src/module-elasticsuite-analytics/view/adminhtml/layout/smile_elasticsuite_analytics_search_usage.xml b/src/module-elasticsuite-analytics/view/adminhtml/layout/smile_elasticsuite_analytics_search_usage.xml index 087d04dba..0c3b80a1e 100644 --- a/src/module-elasticsuite-analytics/view/adminhtml/layout/smile_elasticsuite_analytics_search_usage.xml +++ b/src/module-elasticsuite-analytics/view/adminhtml/layout/smile_elasticsuite_analytics_search_usage.xml @@ -26,6 +26,13 @@ false + + + + + + + diff --git a/src/module-elasticsuite-analytics/view/adminhtml/requirejs-config.js b/src/module-elasticsuite-analytics/view/adminhtml/requirejs-config.js new file mode 100644 index 000000000..e713a74b5 --- /dev/null +++ b/src/module-elasticsuite-analytics/view/adminhtml/requirejs-config.js @@ -0,0 +1,20 @@ +/** + * DISCLAIMER + * + * Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer + * versions in the future. + * + * @category Smile + * @package Smile\ElasticsuiteAnalytics + * @author Vadym Honcharuk + * @copyright 2024 Smile + * @license Open Software License ("OSL") v. 3.0 + */ +var config = { + map: { + '*': { + 'customerGroupSelector': 'Smile_ElasticsuiteAnalytics/js/report/customer-group-selector', + 'customerCompanySelector': 'Smile_ElasticsuiteAnalytics/js/report/customer-company-selector' + } + } +}; diff --git a/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_company_selector.phtml b/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_company_selector.phtml new file mode 100644 index 000000000..b871b4230 --- /dev/null +++ b/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_company_selector.phtml @@ -0,0 +1,40 @@ + + * @copyright 2024 Smile + * @license Open Software License ("OSL") v. 3.0 + */ +?> + + +isCompanyEnabled()): ?> + getCompaniesList(); ?> +
+ + +
+ + + diff --git a/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_group_selector.phtml b/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_group_selector.phtml new file mode 100644 index 000000000..b004c3bc5 --- /dev/null +++ b/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_group_selector.phtml @@ -0,0 +1,38 @@ + + * @copyright 2024 Smile + * @license Open Software License ("OSL") v. 3.0 + */ +?> + +getCustomerGroups(); +?> +
+ + +
+ + diff --git a/src/module-elasticsuite-analytics/view/adminhtml/web/css/source/_module.less b/src/module-elasticsuite-analytics/view/adminhtml/web/css/source/_module.less index fc3f9eafb..2fef48e3e 100644 --- a/src/module-elasticsuite-analytics/view/adminhtml/web/css/source/_module.less +++ b/src/module-elasticsuite-analytics/view/adminhtml/web/css/source/_module.less @@ -69,4 +69,38 @@ } } } + + .page-main-actions { + display: block; + } + + .customer-group-selector { + color: #41362f; + float: left; + font-size: 1.3rem; + margin-top: .59rem; + margin-left: 4rem; + } + + .customer-group-selector label { + margin-right: 1rem; + font-weight: 700; + } + + .customer-company-selector { + color: #41362f; + float: left; + font-size: 1.3rem; + margin-top: .59rem; + margin-left: 2rem; + } + + .customer-company-selector label { + margin-right: 1rem; + font-weight: 700; + } + + .page-actions-inner { + margin-top: .59rem; + } } diff --git a/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-company-selector.js b/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-company-selector.js new file mode 100644 index 000000000..40c5bb880 --- /dev/null +++ b/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-company-selector.js @@ -0,0 +1,42 @@ +/** + * DISCLAIMER + * + * Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer + * versions in the future. + * + * @category Smile + * @package Smile\ElasticsuiteAnalytics + * @author Vadym Honcharuk + * @copyright 2024 Smile + * @license Open Software License ("OSL") v. 3.0 + */ + +define('Smile_ElasticsuiteAnalytics/js/report/customer-company-selector', [ + 'jquery', + 'mage/url' +], function($, urlBuilder) { + 'use strict'; + + return function() { + // On document ready, set the selected value in the company dropdown. + $(document).ready(function() { + var urlParams = new URLSearchParams(window.location.search); + var selectedCompany = urlParams.get('company_id'); + + if (selectedCompany) { + $('#company_id').val(selectedCompany); + } + }); + + // Handle the company dropdown value change. + $('#company_id').on('change', function() { + var selectedCompany = $(this).val(); + var newUrl = new URL(window.location.href); + + newUrl.searchParams.set('company_id', selectedCompany); + + // Redirect to the new URL with the company filter. + window.location.href = newUrl.href; + }); + }; +}); diff --git a/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-group-selector.js b/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-group-selector.js new file mode 100644 index 000000000..54089bc0e --- /dev/null +++ b/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-group-selector.js @@ -0,0 +1,43 @@ +/** + * DISCLAIMER + * + * Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer + * versions in the future. + * + * @category Smile + * @package Smile\ElasticsuiteAnalytics + * @author Vadym Honcharuk + * @copyright 2024 Smile + * @license Open Software License ("OSL") v. 3.0 + */ + +define('Smile_ElasticsuiteAnalytics/js/report/customer-group-selector', [ + 'jquery', + 'mage/url' +], function($, urlBuilder) { + 'use strict'; + + return function() { + // !On document ready, set the selected value in the customer group dropdown. + $(document).ready(function() { + var urlParams = new URLSearchParams(window.location.search); + var selectedGroup = urlParams.get('customer_group'); + + if (selectedGroup) { + $('#customer_group').val(selectedGroup); + } + }); + + // Handle the customer group dropdown value change. + $('#customer_group').on('change', function() { + var selectedGroup = $(this).val(); + var newUrl = new URL(window.location.href); + + newUrl.searchParams.set('customer_group', selectedGroup); + + // Redirect to the new URL with the customer group filter. + window.location.href = newUrl.href; + }); + }; +}); + diff --git a/src/module-elasticsuite-tracker/Model/CustomerDataTrackingManager.php b/src/module-elasticsuite-tracker/Model/CustomerDataTrackingManager.php index 0caf4ce5b..960df6c6e 100644 --- a/src/module-elasticsuite-tracker/Model/CustomerDataTrackingManager.php +++ b/src/module-elasticsuite-tracker/Model/CustomerDataTrackingManager.php @@ -13,11 +13,14 @@ */ namespace Smile\ElasticsuiteTracker\Model; -use DateTime; use Magento\Customer\Model\Session as CustomerSession; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Exception\NoSuchEntityException; +use Magento\Framework\Module\Manager as ModuleManager; /** - * Tracking Indices Manager + * Additional customer data tracking * * @category Smile * @package Smile\ElasticsuiteTracker @@ -31,11 +34,32 @@ class CustomerDataTrackingManager protected $customerSession; /** - * @param CustomerSession $customerSession Customer session + * @var \Magento\Company\Api\CompanyManagementInterface|null */ - public function __construct(CustomerSession $customerSession) + private $companyManagement = null; + + /** + * CustomerDataTrackingManager constructor. + * + * @SuppressWarnings(PHPMD.ElseExpression) + * + * @param CustomerSession $customerSession Customer session. + * @param ModuleManager $moduleManager Module manager. + * @throws LocalizedException + */ + public function __construct(CustomerSession $customerSession, ModuleManager $moduleManager) { $this->customerSession = $customerSession; + // Check if Magento_Company module is enabled before attempting to load the repository. + if ($moduleManager->isEnabled('Magento_Company')) { + if (interface_exists('\Magento\Company\Api\CompanyManagementInterface')) { + $this->companyManagement = ObjectManager::getInstance()->get( + \Magento\Company\Api\CompanyManagementInterface::class + ); + } else { + throw new LocalizedException(__('CompanyManagementInterface is not available.')); + } + } } /** @@ -57,6 +81,20 @@ public function getCustomerDataToTrack() $variables['group_id'] = (int) $customer->getGroupId() ?? \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID; $variables['id'] = (int) $customer->getId(); + if ($this->customerSession->isLoggedIn() && (null !== $this->companyManagement)) { + try { + // Use CompanyUserManager to retrieve company information by customer ID. + $company = $this->companyManagement->getByCustomerId($customer->getId()); + + // If company is found, add the company ID to the variables array. + if ($company) { + $variables['company_id'] = (int) $company->getId(); + } + } catch (NoSuchEntityException $e) { + // No company found for this customer. + } + } + return $variables; } } diff --git a/src/module-elasticsuite-tracker/Plugin/QuotePlugin.php b/src/module-elasticsuite-tracker/Plugin/QuotePlugin.php index 2adfbb5d9..626c168bd 100644 --- a/src/module-elasticsuite-tracker/Plugin/QuotePlugin.php +++ b/src/module-elasticsuite-tracker/Plugin/QuotePlugin.php @@ -14,12 +14,16 @@ namespace Smile\ElasticsuiteTracker\Plugin; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\View\Layout\PageType\Config as PageTypeConfig; use Magento\Quote\Model\Quote; /** * Log add to cart events into the event queue. * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * * @category Smile * @package Smile\ElasticsuiteTracker * @author Aurelien FOUCRET @@ -46,6 +50,16 @@ class QuotePlugin */ private $pageTypeConfig; + /** + * @var \Magento\Customer\Model\Session + */ + private $customerSession; + + /** + * @var \Magento\Company\Api\CompanyManagementInterface|null + */ + private $companyManagement = null; + /** * @var \Psr\Log\LoggerInterface */ @@ -54,24 +68,44 @@ class QuotePlugin /** * Constructor. * - * @param \Smile\ElasticsuiteTracker\Api\CustomerTrackingServiceInterface $service Tracker service. - * @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager Cookie manager. - * @param \Smile\ElasticsuiteTracker\Helper\Data $trackerHelper Tracker helper. - * @param \Magento\Framework\View\Layout\PageType\Config $pageTypeConfig The Page Type Configuration - * @param \Psr\Log\LoggerInterface $logger Logger. + * @SuppressWarnings(PHPMD.ElseExpression) + * + * @param \Smile\ElasticsuiteTracker\Api\CustomerTrackingServiceInterface $service Tracker service. + * @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager Cookie manager. + * @param \Smile\ElasticsuiteTracker\Helper\Data $trackerHelper Tracker helper. + * @param \Magento\Framework\View\Layout\PageType\Config $pageTypeConfig Page type configuration. + * @param \Magento\Customer\Model\Session $customerSession Customer session. + * @param \Psr\Log\LoggerInterface $logger Logger. + * @param \Magento\Framework\Module\Manager $moduleManager Module manager. + * + * @throws LocalizedException */ public function __construct( \Smile\ElasticsuiteTracker\Api\CustomerTrackingServiceInterface $service, \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager, \Smile\ElasticsuiteTracker\Helper\Data $trackerHelper, \Magento\Framework\View\Layout\PageType\Config $pageTypeConfig, - \Psr\Log\LoggerInterface $logger + \Magento\Customer\Model\Session $customerSession, + \Psr\Log\LoggerInterface $logger, + \Magento\Framework\Module\Manager $moduleManager ) { $this->service = $service; $this->cookieManager = $cookieManager; $this->trackerHelper = $trackerHelper; $this->pageTypeConfig = $pageTypeConfig; + $this->customerSession = $customerSession; $this->logger = $logger; + + // Check if Magento_Company module is enabled before attempting to load the repository. + if ($moduleManager->isEnabled('Magento_Company')) { + if (interface_exists('\Magento\Company\Api\CompanyManagementInterface')) { + $this->companyManagement = ObjectManager::getInstance()->get( + \Magento\Company\Api\CompanyManagementInterface::class + ); + } else { + throw new LocalizedException(__('CompanyManagementInterface is not available.')); + } + } } /** @@ -94,7 +128,14 @@ public function afterAddProduct( /** @var \Magento\Quote\Model\Quote\Item $result */ $product = $result->getProduct(); if ($product !== null) { - $this->logEvent($product->getId(), $product->getStoreId()); + // Retrieve the customer group ID from the product object. + $customerGroupId = $product->getCustomerGroupId(); + + // Retrieve the customer company ID rom the customer session. + $companyId = $this->getCompanyId(); + + // Log event with product, store, customer group and company ids. + $this->logEvent($product->getId(), $product->getStoreId(), $customerGroupId, $companyId); } } } catch (\Exception $e) { @@ -109,12 +150,14 @@ public function afterAddProduct( /** * Log the event. * - * @param int $productId Product Id - * @param int $storeId Store Id + * @param int $productId Product ID. + * @param int $storeId Store ID. + * @param int|null $customerGroupId Customer Group ID (null if non-logged-in). + * @param int|null $companyId Customer Company ID (null if non-logged-in or company is not available). * * @return void */ - private function logEvent(int $productId, int $storeId): void + private function logEvent(int $productId, int $storeId, ?int $customerGroupId, ?int $companyId): void { $pageData = [ 'identifier' => 'checkout_cart_add', @@ -123,11 +166,48 @@ private function logEvent(int $productId, int $storeId): void $pageData['store_id'] = $storeId; $pageData['cart']['product_id'] = $productId; - $eventData = ['page' => $pageData, 'session' => $this->getSessionData()]; + // Add customer information. + $customerData = []; + if ($customerGroupId !== null) { + $customerData['group_id'] = $customerGroupId; + } + if ($companyId !== null) { + $customerData['company_id'] = $companyId; + } + + $eventData = [ + 'page' => $pageData, + 'customer' => $customerData, + 'session' => $this->getSessionData(), + ]; $this->service->addEvent($eventData); } + /** + * Retrieve the company ID from the customer session. + * + * If the customer has an associated company, return the company ID, otherwise return null if no company is assigned. + * + * @return int|null + */ + private function getCompanyId(): ?int + { + if ($this->customerSession->isLoggedIn() && (null !== $this->companyManagement)) { + try { + $customer = $this->customerSession->getCustomer(); + $company = $this->companyManagement->getByCustomerId($customer->getId()); + + return $company ? $company->getId() : null; + } catch (\Exception $e) { + return null; + } + } + + // Return null if the user is non-logged-in or companyManagement is not available. + return null; + } + /** * Read session data. * diff --git a/src/module-elasticsuite-tracker/etc/elasticsuite_indices.xml b/src/module-elasticsuite-tracker/etc/elasticsuite_indices.xml index 0497bbe34..8fd7dd34a 100644 --- a/src/module-elasticsuite-tracker/etc/elasticsuite_indices.xml +++ b/src/module-elasticsuite-tracker/etc/elasticsuite_indices.xml @@ -31,6 +31,7 @@ + @@ -123,6 +124,8 @@ + +