Skip to content

Commit

Permalink
Merge pull request #217 from EmicoEcommerce/feat/TW-44-visuals
Browse files Browse the repository at this point in the history
feat: Implemented visuals functionality
  • Loading branch information
ah-net authored Sep 24, 2024
2 parents a3d2f1d + b0e223b commit 469122f
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 31 deletions.
31 changes: 31 additions & 0 deletions Api/Data/VisualInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Tweakwise\Magento2Tweakwise\Api\Data;

interface VisualInterface
{
public const IMAGE_URL = 'image';
public const URL = 'url';

/**
* @return string
*/
public function getImageUrl(): string;

/**
* @param string $imageUrl
* @return self
*/
public function setImageUrl(string $imageUrl): self;

/**
* @return string
*/
public function getUrl(): string;

/**
* @param string $url
* @return self
*/
public function setUrl(string $url): self;
}
6 changes: 3 additions & 3 deletions Controller/Product/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public function __construct(
*/
public function execute(): HttpInterface
{
$productId = $this->request->getParam('product_id');
$itemId = (string) $this->request->getParam('item_id');
$cardType = $this->request->getParam('card_type');
$itemHtml = $cardType ? $this->cacheHelper->load((int)$productId, $cardType) :
$this->cacheHelper->load((int)$productId);
$itemHtml = $cardType ? $this->cacheHelper->load($itemId, $cardType) :
$this->cacheHelper->load($itemId);

$response = $this->httpFactory->create();
$response->appendBody($itemHtml);
Expand Down
18 changes: 9 additions & 9 deletions Helper/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,29 @@ public function __construct(
}

/**
* @param int $productId
* @param string $itemId
* @param string $cardType
* @return string
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function load(int $productId, string $cardType = 'default'): string
public function load(string $itemId, string $cardType = 'default'): string
{
$result = $this->cache->load($this->getCacheKey($productId, $cardType));
$result = $this->cache->load($this->getCacheKey($itemId, $cardType));
return $result ? $result : '';
}

/**
* @param string $data
* @param int $productId
* @param string $itemId
* @param string $cardType
* @return void
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function save(string $data, int $productId, string $cardType = 'default'): void
public function save(string $data, string $itemId, string $cardType = 'default'): void
{
$this->cache->save($data, $this->getCacheKey($productId, $cardType));
$this->cache->save($data, $this->getCacheKey($itemId, $cardType));
}

/**
Expand Down Expand Up @@ -128,13 +128,13 @@ public function isHyvaTheme(): bool
}

/**
* @param int $productId
* @param string $itemId
* @param string $cardType
* @return string
* @throws LocalizedException
* @throws NoSuchEntityException
*/
private function getCacheKey(int $productId, string $cardType): string
private function getCacheKey(string $itemId, string $cardType): string
{
$storeId = $this->storeManager->getStore()->getId();
$customerGroupId = $this->customerSession->getCustomerGroupId();
Expand All @@ -143,7 +143,7 @@ private function getCacheKey(int $productId, string $cardType): string
'%s_%s_%s_%s_%s',
$storeId,
$customerGroupId,
$productId,
$itemId,
$cardType,
self::REDIS_CACHE_KEY
);
Expand Down
62 changes: 61 additions & 1 deletion Model/Catalog/Product/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@

namespace Tweakwise\Magento2Tweakwise\Model\Catalog\Product;

use Exception;
use Tweakwise\Magento2Tweakwise\Model\Config;
use Tweakwise\Magento2Tweakwise\Model\Enum\ItemType;
use Tweakwise\Magento2Tweakwise\Api\Data\VisualInterface;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\NavigationContext;
use Tweakwise\Magento2Tweakwise\Model\Client\Request\ProductSearchRequest;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Indexer\Product\Flat\State;
use Magento\Catalog\Model\Product\OptionFactory;
use Tweakwise\Magento2Tweakwise\Model\VisualFactory;
use Magento\Catalog\Model\ResourceModel\Helper as CatalogResourceHelper;
use Magento\Catalog\Model\ResourceModel\Url;
use Magento\Customer\Api\GroupManagementInterface;
Expand Down Expand Up @@ -41,7 +46,29 @@ class Collection extends AbstractCollection
protected $navigationContext;

/**
* {@inheritdoc}
* @param CollectionEntityFactory $entityFactory
* @param LoggerInterface $logger
* @param FetchStrategyInterface $fetchStrategy
* @param ManagerInterface $eventManager
* @param EavConfig $eavConfig
* @param ResourceConnection $resource
* @param EavEntityFactory $eavEntityFactory
* @param CatalogResourceHelper $resourceHelper
* @param UniversalFactory $universalFactory
* @param StoreManagerInterface $storeManager
* @param Manager $moduleManager
* @param State $catalogProductFlatState
* @param ScopeConfigInterface $scopeConfig
* @param OptionFactory $productOptionFactory
* @param Url $catalogUrl
* @param TimezoneInterface $localeDate
* @param Session $customerSession
* @param DateTime $dateTime
* @param GroupManagementInterface $groupManagement
* @param NavigationContext $navigationContext
* @param VisualFactory $visualFactory
* @param Config $config
* @param AdapterInterface|null $connection
*/
public function __construct(
CollectionEntityFactory $entityFactory,
Expand All @@ -64,6 +91,8 @@ public function __construct(
DateTime $dateTime,
GroupManagementInterface $groupManagement,
NavigationContext $navigationContext,
private readonly VisualFactory $visualFactory,
private readonly Config $config,
AdapterInterface $connection = null
) {
parent::__construct(
Expand Down Expand Up @@ -139,9 +168,40 @@ protected function _afterLoad()

$this->applyCollectionSizeValues();

if ($this->config->isPersonalMerchandisingActive()) {
$this->addVisuals();
}

return $this;
}

/**
* @return void
*/
protected function addVisuals(): void
{
try {
$response = $this->navigationContext->getResponse();
} catch (Exception $e) {
return;
}

foreach ($response->getItems() as $item) {
if ($item->getValue('type') !== ItemType::VISUAL->value) {
continue;
}

/** @var VisualInterface $visual */
$visual = $this->visualFactory->create();
$visual->setId($item->getValue('itemno'));
$visual->setImageUrl($item->getImage());
$visual->setUrl($item->getUrl());
$itemPosition = array_search($item, $response->getItems());

array_splice($this->_items, $itemPosition, 0, [$visual]);
}
}

/**
* {@inheritdoc}
*/
Expand Down
10 changes: 10 additions & 0 deletions Model/Enum/ItemType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Tweakwise\Magento2Tweakwise\Model\Enum;

enum ItemType: string
{
case VISUAL = 'visual';
}
43 changes: 43 additions & 0 deletions Model/Visual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Tweakwise\Magento2Tweakwise\Model;

use Magento\Catalog\Model\Product;
use Tweakwise\Magento2Tweakwise\Api\Data\VisualInterface;

class Visual extends Product implements VisualInterface
{
/**
* @return string
*/
public function getImageUrl(): string
{
return $this->getData(self::IMAGE_URL);
}

/**
* @param string $imageUrl
* @return VisualInterface
*/
public function setImageUrl(string $imageUrl): VisualInterface
{
return $this->setData(self::IMAGE_URL, $imageUrl);
}

/**
* @return string
*/
public function getUrl(): string
{
return $this->getData(self::URL);
}

/**
* @param string $url
* @return VisualInterface
*/
public function setUrl(string $url): VisualInterface
{
return $this->setData(self::URL, $url);
}
}
4 changes: 2 additions & 2 deletions ViewModel/LinkedProductListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function getItemHtml(
);
}

$productId = (int) $product->getId();
$productId = (string) $product->getId();
$cardType = str_replace(' ', '_', $params['card_type']);
if (!$this->cacheHelper->load($productId, $cardType)) {
$itemHtml = $this->getItemHtmlWithRenderer(
Expand All @@ -67,7 +67,7 @@ public function getItemHtml(
$customerGroupId = $this->customerSession->getCustomerGroupId();

return sprintf(
'<esi:include src="/%s?product_id=%s&store_id=%s&customer_group_id=%s&card_type=%s" />',
'<esi:include src="/%s?item_id=%s&store_id=%s&customer_group_id=%s&card_type=%s" />',
Cache::PRODUCT_CARD_PATH,
$productId,
$storeId,
Expand Down
60 changes: 44 additions & 16 deletions ViewModel/ProductListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Framework\View\LayoutInterface;
use Tweakwise\Magento2Tweakwise\Helper\Cache;
use Tweakwise\Magento2Tweakwise\Model\Visual;
use Magento\Store\Model\StoreManagerInterface;

class ProductListItem implements ArgumentInterface
Expand All @@ -29,7 +30,7 @@ public function __construct(
}

/**
* @param Product $product
* @param Product|Visual $item
* @param AbstractBlock $parentBlock
* @param string $viewMode
* @param string $templateType
Expand All @@ -40,19 +41,24 @@ public function __construct(
* @throws NoSuchEntityException
*/
public function getItemHtml(
Product $product,
Product|Visual $item,
AbstractBlock $parentBlock,
string $viewMode,
string $templateType,
string $imageDisplayArea,
bool $showDescription
): string {
$isVisual = $item instanceof Visual;
if (
!$this->cacheHelper->personalMerchandisingCanBeApplied() ||
$this->cacheHelper->isTweakwiseAjaxRequest()
) {
if ($isVisual) {
return $this->getVisualHtml($item);
}

return $this->getItemHtmlWithRenderer(
$product,
$item,
$parentBlock,
$viewMode,
$templateType,
Expand All @@ -61,27 +67,31 @@ public function getItemHtml(
);
}

$productId = (int) $product->getId();
$itemId = (string) $item->getId();
if (!$this->cacheHelper->load($itemId)) {
if ($isVisual) {
$itemHtml = $this->getVisualHtml($item);
} else {
$itemHtml = $this->getItemHtmlWithRenderer(
$item,
$parentBlock,
$viewMode,
$templateType,
$imageDisplayArea,
$showDescription
);
}

if (!$this->cacheHelper->load($productId)) {
$itemHtml = $this->getItemHtmlWithRenderer(
$product,
$parentBlock,
$viewMode,
$templateType,
$imageDisplayArea,
$showDescription
);
$this->cacheHelper->save($itemHtml, $productId);
$this->cacheHelper->save($itemHtml, $itemId);
}

$storeId = $this->storeManager->getStore()->getId();
$customerGroupId = $this->customerSession->getCustomerGroupId();

return sprintf(
'<esi:include src="/%s?product_id=%s&store_id=%s&customer_group_id=%s" />',
'<esi:include src="/%s?item_id=%s&store_id=%s&customer_group_id=%s" />',
Cache::PRODUCT_CARD_PATH,
$productId,
$itemId,
$storeId,
$customerGroupId
);
Expand Down Expand Up @@ -128,4 +138,22 @@ private function getItemHtmlWithRenderer(

return $itemRendererBlock->toHtml();
}

/**
* @param Visual $visual
* @return string
*/
private function getVisualHtml(Visual $visual): string
{
/** @var AbstractBlock $visualRendererBlock */
$visualRendererBlock = $this->layout->getBlock('tweakwise.catalog.product.list.visual');

if (! $visualRendererBlock) {
return '';
}

$visualRendererBlock->setData('visual', $visual);

return $visualRendererBlock->toHtml();
}
}
1 change: 1 addition & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<preference for="Tweakwise\Magento2Tweakwise\Api\AttributeSlugRepositoryInterface" type="Tweakwise\Magento2Tweakwise\Model\AttributeSlugRepository"/>
<preference for="Tweakwise\Magento2Tweakwise\Api\Data\AttributeSlugSearchResultsInterface" type="Magento\Framework\Api\SearchResults"/>
<preference for="Tweakwise\Magento2Tweakwise\Api\Data\AttributeSlugInterface" type="Tweakwise\Magento2Tweakwise\Model\AttributeSlug"/>
<preference for="Tweakwise\Magento2Tweakwise\Api\Data\VisualInterface" type="Tweakwise\Magento2Tweakwise\Model\Visual"/>

<preference for="Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\Url\UrlInterface" type="Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\Url\Strategy\QueryParameterStrategy" />
<preference for="Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\Url\FilterApplierInterface" type="Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\Url\Strategy\QueryParameterStrategy" />
Expand Down
Loading

0 comments on commit 469122f

Please sign in to comment.