Skip to content

Commit

Permalink
2.2.x (#156) Refactor - same search design everywhere
Browse files Browse the repository at this point in the history
- Refactor - same search design everywhere
- Update contact form
  • Loading branch information
livca-smile authored May 3, 2024
1 parent 32017e7 commit a4fa9c3
Show file tree
Hide file tree
Showing 27 changed files with 578 additions and 344 deletions.
35 changes: 35 additions & 0 deletions Block/AbstractView.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

namespace Smile\StoreLocator\Block;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Registry;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Store\Model\Store;
use Smile\Retailer\Api\Data\RetailerInterface;

/**
Expand All @@ -29,4 +32,36 @@ public function getRetailer(): ?RetailerInterface
{
return $this->coreRegistry->registry('current_retailer');
}

/**
* Get image name.
*/
protected function getMediaPath(): string|bool
{
return $this->getRetailer()->getMediaPath() ?: false;
}

/**
* Get full image url.
*/
public function getImage(): string|bool
{
$mediaPath = $this->getMediaPath();
$imageUrlRetailer = $this->getImageUrl() . 'seller/';

return $mediaPath ? $imageUrlRetailer . $mediaPath : false;
}

/**
* * Get base media url.
*
* @throws NoSuchEntityException
*/
public function getImageUrl(): string
{
/** @var Store $currentStore */
$currentStore = $this->_storeManager->getStore();

return $currentStore->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
}
}
51 changes: 51 additions & 0 deletions Block/ContactForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

namespace Smile\StoreLocator\Block;

use Magento\Customer\Helper\View as CustomerHelperView;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\Framework\View\Element\Template\Context;
use Magento\Store\Model\Store;
use Magento\Theme\Block\Html\Breadcrumbs;
use Smile\Map\Model\AddressFormatter;
use Smile\StoreLocator\Api\Data\RetailerAddressInterface;
use Smile\StoreLocator\Helper\Data as StoreLocatorHelper;

/**
Expand All @@ -25,7 +29,10 @@ public function __construct(
Context $context,
Registry $coreRegistry,
private StoreLocatorHelper $storeLocatorHelper,
protected Session $customerSession,
protected CustomerHelperView $customerViewHelper,
DataPersistorInterface $dataPersistorInterface,
private AddressFormatter $addressFormatter,
array $data
) {
$this->dataPersistor = $dataPersistorInterface;
Expand Down Expand Up @@ -129,4 +136,48 @@ private function setBreadcrumbs(): self

return $this;
}

/**
* Get customer user name if logged in
*/
public function getUserName(): string
{
if (!$this->customerSession->isLoggedIn()) {
return '';
}
$customer = $this->customerSession->getCustomerDataObject();

return trim($this->customerViewHelper->getCustomerName($customer));
}

/**
* Get customer user email if logged in
*/
public function getUserEmail(): string
{
if (!$this->customerSession->isLoggedIn()) {
return '';
}
$customer = $this->customerSession->getCustomerDataObject();

return $customer->getEmail();
}

/**
* Returns current store address.
*/
public function getAddress(): ?RetailerAddressInterface
{
return $this->getRetailer()->getData('address');
}

/**
* Get address formatted in HTML.
*/
public function getAddressHtml(): string
{
return $this->getAddress()
? $this->addressFormatter->formatAddress($this->getAddress(), AddressFormatter::FORMAT_HTML)
: '';
}
}
4 changes: 4 additions & 0 deletions Block/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public function getJsLayout()
$this->map->getConfig()
);

$placeholder = $this->storeLocatorHelper->getSearchPlaceholder();
$jsLayout['components']['store-locator-search']['searchPlaceholderText'] = $placeholder;
$jsLayout['components']['store-locator-search']['children']['geocoder']['searchPlaceholderText'] = $placeholder;

if ($this->getRequest()->getParam('query', false)) {
$query = $this->getRequest()->getParam('query', false);
$jsLayout['components']['store-locator-search']['children']['geocoder']['fulltextSearch'] =
Expand Down
8 changes: 8 additions & 0 deletions Block/StoreChooser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public function __construct(
$this->map = $mapProvider->getMap();
}

/**
* Get placeholder for search input of store_locator, default: City, Zipcode, Address, ...
*/
public function getSearchPlaceholder(): string
{
return $this->storeLocatorHelper->getSearchPlaceholder();
}

/**
* @inheritdoc
*/
Expand Down
52 changes: 18 additions & 34 deletions Block/View/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

namespace Smile\StoreLocator\Block\View;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Registry;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\Template\Context;
use Magento\Store\Model\Store;
use Smile\Map\Api\Data\GeoPointInterface;
use Smile\Map\Api\MapInterface;
use Smile\Map\Api\MapProviderInterface;
Expand All @@ -19,6 +17,7 @@
use Smile\Retailer\Model\ResourceModel\Retailer\CollectionFactory as RetailerCollectionFactory;
use Smile\StoreLocator\Api\Data\RetailerAddressInterface;
use Smile\StoreLocator\Block\AbstractView;
use Smile\StoreLocator\Helper\Contact;
use Smile\StoreLocator\Helper\Data;
use Smile\StoreLocator\Helper\Schedule;
use Smile\StoreLocator\Model\Retailer\ScheduleManagement;
Expand All @@ -39,6 +38,7 @@ public function __construct(
Context $context,
Registry $coreRegistry,
MapProviderInterface $mapProvider,
private Contact $contactHelper,
private Data $storeLocatorHelper,
private AddressFormatter $addressFormatter,
private Schedule $scheduleHelper,
Expand Down Expand Up @@ -168,38 +168,6 @@ public function getDescription(): ?string
return $this->getRetailer()->getData('description');
}

/**
* * Get base media url.
*
* @throws NoSuchEntityException
*/
public function getImageUrl(): string
{
/** @var Store $currentStore */
$currentStore = $this->_storeManager->getStore();

return $currentStore->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
}

/**
* Get image name.
*/
protected function getMediaPath(): string|bool
{
return $this->getRetailer()->getMediaPath() ?: false;
}

/**
* Get full image url.
*/
public function getImage(): string|bool
{
$mediaPath = $this->getMediaPath();
$imageUrlRetailer = $this->getImageUrl() . 'seller/';

return $mediaPath ? $imageUrlRetailer . $mediaPath : false;
}

/**
* Get store name.
*/
Expand Down Expand Up @@ -294,4 +262,20 @@ private function getSetStorePostData(RetailerInterface $retailer): array

return ['action' => $setUrl, 'data' => $postData];
}

/**
* Check if we can display contact form for current retailer.
*/
public function showContactForm(): bool
{
return $this->contactHelper->canDisplayContactForm($this->getRetailer());
}

/**
* Retrieve Contact form Url for current retailer.
*/
public function getContactFormUrl(): string
{
return $this->contactHelper->getContactFormUrl($this->getRetailer());
}
}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to this project will be documented in this file.

## [2.2.1] - 2024-05-03
[2.2.1]: https://github.com/Smile-SA/magento2-module-store-locator/compare/2.2.0...2.2.1

- Refactor - same search design everywhere
- Update contact form

## [2.2.0] - 2023-09-20
[2.2.0]: https://github.com/Smile-SA/magento2-module-store-locator/compare/2.1.0...2.2.0

Expand Down
4 changes: 4 additions & 0 deletions Controller/Store/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function execute()
$retailer = $this->retailerRepository->get((int) $retailerId);
$this->customerData->setRetailer($retailer);
} catch (Exception $exception) {
$retailer = null;
$this->messageManager->addExceptionMessage(
$exception,
__('We are sorry, an error occured when switching retailer.')
Expand All @@ -48,6 +49,9 @@ public function execute()
/** @var Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
if ($retailer) {
$this->messageManager->addSuccess(__('Retailer shop %1 has been chosen.', $retailer->getName()));
}

return $resultRedirect;
}
Expand Down
19 changes: 19 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\ScopeInterface;
use Smile\Retailer\Api\Data\RetailerInterface;
use Smile\StoreLocator\Model\Url;

Expand All @@ -14,13 +15,31 @@
*/
class Data extends AbstractHelper
{
public const SEARCH_PLACEHOLDER_XML_PATH = 'store_locator/search/placeholder';

public function __construct(
Context $context,
private Url $urlModel
) {
parent::__construct($context);
}

/**
* Get config by config path.
*/
public function getConfigByPath(string $path, string $scope = ScopeInterface::SCOPE_STORE): mixed
{
return $this->scopeConfig->getValue($path, $scope);
}

/**
* Get placeholder for search input of store_locator, default: City, Zipcode, Address, ...
*/
public function getSearchPlaceholder(): string
{
return (string) $this->getConfigByPath(self::SEARCH_PLACEHOLDER_XML_PATH) ?: 'City, Zipcode, Address ...';
}

/**
* Store locator home URL.
*/
Expand Down
45 changes: 45 additions & 0 deletions Plugin/Model/Layout/RetailerDepersonalizePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Smile\StoreLocator\Plugin\Model\Layout;

use Magento\Customer\Model\Layout\DepersonalizePlugin;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\View\LayoutInterface;
use Magento\PageCache\Model\DepersonalizeChecker;
use Smile\StoreLocator\CustomerData\CurrentStore;

/**
* Depersonalize customer data.
*
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class RetailerDepersonalizePlugin
{
//@SmileAnalyserSkip magento2/badpractices
public function __construct(
protected DepersonalizeChecker $depersonalizeChecker,
protected CustomerSession $customerSession,
protected CurrentStore $currentStore
) {
}

/**
* Prevent losing retailer_id chosen - show product offer price if shop has been selected, even in Retail mode
*/
public function afterAfterGenerateElements(
DepersonalizePlugin $subject,
mixed $result,
LayoutInterface $layout
): void {
if ($this->depersonalizeChecker->checkIfDepersonalize($layout)) {
$retailer = $this->currentStore->getRetailer();
if ($retailer && $retailer->getId()) {
$data = $this->customerSession->getData();
$data['retailer_id'] = $retailer->getId();
$this->customerSession->setData($data);
}
}
}
}
22 changes: 22 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="store_locator" translate="label" type="text" sortOrder="2020" showInDefault="1" showInWebsite="1" showInStore="1">
<class>separator-top</class>
<label>Store Locator Settings</label>
<tab>smile_elasticsuite</tab>
<resource>Magento_Backend::smile_map</resource>

<group id="search" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>General Settings</label>
<field id="placeholder" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Shown placeholder to help user</label>
<comment>default: City, Zipcode, Address ...</comment>
</field>
</group>
<group id="seo" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Seo</label>
<field id="base_url" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Store Locator URL</label>
<comment>default: stores</comment>
</field>
</group>

</section>

<section id="smile_map" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">

Expand Down
3 changes: 3 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
</map>
</smile_map>
<store_locator>
<search>
<placeholder>City, Zipcode, Address ...</placeholder>
</search>
<seo>
<base_url>stores</base_url>
</seo>
Expand Down
Loading

0 comments on commit a4fa9c3

Please sign in to comment.