Skip to content

Commit

Permalink
Merge pull request #222 from EmicoEcommerce/beta
Browse files Browse the repository at this point in the history
Merge beta into master
  • Loading branch information
ah-net authored Sep 24, 2024
2 parents c0b2c12 + 9dc0372 commit 5d9083b
Show file tree
Hide file tree
Showing 14 changed files with 265 additions and 37 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
3 changes: 0 additions & 3 deletions Model/Catalog/Layer/Url/Strategy/QueryParameterStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,7 @@ protected function getCurrentQueryUrl(MagentoHttpRequest $request, array $query)
{
$selectedFilters = $request->getQuery();
$reservedParams = [
self::PARAM_LIMIT,
self::PARAM_MODE,
self::PARAM_PAGE,
self::PARAM_ORDER,
self::PARAM_CATEGORY,
];

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);
}
}
15 changes: 12 additions & 3 deletions ViewModel/LinkedProductListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
namespace Tweakwise\Magento2Tweakwise\ViewModel;

use Magento\Catalog\Model\Product;
use Magento\Customer\Model\Session;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Framework\View\LayoutInterface;
use Tweakwise\Magento2Tweakwise\Helper\Cache;
use Magento\Store\Model\StoreManagerInterface;

class LinkedProductListItem implements ArgumentInterface
{
Expand All @@ -20,7 +22,9 @@ class LinkedProductListItem implements ArgumentInterface
*/
public function __construct(
private readonly LayoutInterface $layout,
private readonly Cache $cacheHelper
private readonly Cache $cacheHelper,
private readonly StoreManagerInterface $storeManager,
private readonly Session $customerSession
) {
}

Expand Down Expand Up @@ -48,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 @@ -59,10 +63,15 @@ public function getItemHtml(
$this->cacheHelper->save($itemHtml, $productId, $cardType);
}

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

return sprintf(
'<esi:include src="/%s?product_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,
$customerGroupId,
$cardType
);
}
Expand Down
Loading

0 comments on commit 5d9083b

Please sign in to comment.