Skip to content

Commit

Permalink
Merge pull request #212 from EmicoEcommerce/beta
Browse files Browse the repository at this point in the history
Merge beta into master
  • Loading branch information
ah-net authored Jul 31, 2024
2 parents 5821780 + da2b7a2 commit c0b2c12
Show file tree
Hide file tree
Showing 23 changed files with 984 additions and 71 deletions.
36 changes: 36 additions & 0 deletions Block/Catalog/Product/ProductList/Featured.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Tweakwise\Magento2Tweakwise\Block\Catalog\Product\ProductList;

use Tweakwise\Magento2Tweakwise\Exception\ApiException;
use Tweakwise\Magento2Tweakwise\Helper\Cache;
use Tweakwise\Magento2Tweakwise\MagentoCompat\PreparePostDataFactory;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Product\Recommendation\Collection;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Product\Recommendation\Context as RecommendationsContext;
Expand All @@ -23,6 +24,7 @@
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Framework\Data\Helper\PostHelper;
use Magento\Framework\Url\Helper\Data;
use Tweakwise\Magento2Tweakwise\ViewModel\LinkedProductListItem;

class Featured extends ListProduct
{
Expand Down Expand Up @@ -58,6 +60,8 @@ class Featured extends ListProduct
* @param RecommendationsContext $recommendationsContext
* @param Config $config
* @param PreparePostDataFactory $preparePostDataFactory
* @param Cache $cacheHelper
* @param LinkedProductListItem $linkedProductListItemViewModel
* @param array $data
* @internal param CategoryRepositoryInterface $categoryRepository
* @internal param Data $urlHelper
Expand All @@ -75,6 +79,8 @@ public function __construct(
RecommendationsContext $recommendationsContext,
Config $config,
PreparePostDataFactory $preparePostDataFactory,
private readonly Cache $cacheHelper,
private readonly LinkedProductListItem $linkedProductListItemViewModel,
array $data = []
) {
parent::__construct(
Expand All @@ -91,6 +97,32 @@ public function __construct(
$this->preparePostDataFactory = $preparePostDataFactory;
}

/**
* @return int|bool|null
*/
protected function getCacheLifetime()
{
if (!$this->cacheHelper->personalMerchandisingCanBeApplied()) {
return parent::getCacheLifetime();
}

$this->setData('ttl', Cache::PRODUCT_LIST_TTL);
$this->setData('cache_lifetime', Cache::PRODUCT_LIST_TTL);
return $this->getData('cache_lifetime');
}

/**
* @return string
*/
public function getTemplate()
{
if (!$this->cacheHelper->personalMerchandisingCanBeApplied() || $this->cacheHelper->isHyvaTheme()) {
return parent::getTemplate();
}

return 'Tweakwise_Magento2Tweakwise::product/list/items.phtml';
}

/**
* @return Collection
*/
Expand All @@ -117,6 +149,10 @@ public function toHtml()
$this->setData('view_model', $this->preparePostDataFactory->getPreparePostData());
}

if ($this->cacheHelper->personalMerchandisingCanBeApplied()) {
$this->setData('view_model_linked_product_list_item', $this->linkedProductListItemViewModel);
}

try {
$this->_getProductCollection();
} catch (ApiException $e) {
Expand Down
41 changes: 4 additions & 37 deletions Block/Product/ListProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,11 @@
use Magento\Framework\Registry;
use Magento\Framework\Stdlib\CookieManagerInterface;
use Magento\Framework\Url\Helper\Data;
use Magento\Framework\View\DesignInterface;
use Tweakwise\Magento2Tweakwise\Model\Config;
use Tweakwise\Magento2Tweakwise\Helper\Cache;

class ListProduct extends MagentoListProduct
{
/**
* @var bool|null
*/
private ?bool $isHyvaTheme = null;

/**
* @param Context $context
* @param PostHelper $postDataHelper
Expand All @@ -35,7 +29,6 @@ class ListProduct extends MagentoListProduct
* @param CookieManagerInterface $cookieManager
* @param Cache $cacheHelper
* @param Registry $registry
* @param DesignInterface $viewDesign
* @param RequestInterface $request
* @param array $data
* @param OutputHelper|null $outputHelper
Expand All @@ -50,7 +43,6 @@ public function __construct(
private readonly CookieManagerInterface $cookieManager,
private readonly Cache $cacheHelper,
private readonly Registry $registry,
private readonly DesignInterface $viewDesign,
private readonly RequestInterface $request,
array $data = [],
?OutputHelper $outputHelper = null
Expand All @@ -72,8 +64,7 @@ public function __construct(
protected function getCacheLifetime()
{
if (
!$this->cacheHelper->isVarnishEnabled() ||
!$this->tweakwiseConfig->isPersonalMerchandisingActive() ||
!$this->cacheHelper->personalMerchandisingCanBeApplied() ||
$this->cacheHelper->isTweakwiseAjaxRequest()
) {
return parent::getCacheLifetime();
Expand All @@ -92,8 +83,7 @@ protected function getCacheLifetime()
public function getUrl($route = '', $params = [])
{
if (
!$this->cacheHelper->isVarnishEnabled() ||
!$this->tweakwiseConfig->isPersonalMerchandisingActive() ||
!$this->cacheHelper->personalMerchandisingCanBeApplied() ||
$route !== 'page_cache/block/esi'
) {
return parent::getUrl($route, $params);
Expand Down Expand Up @@ -122,9 +112,8 @@ public function getUrl($route = '', $params = [])
public function getTemplate()
{
if (
!$this->cacheHelper->isVarnishEnabled() ||
!$this->tweakwiseConfig->isPersonalMerchandisingActive() ||
$this->isHyvaTheme()
!$this->cacheHelper->personalMerchandisingCanBeApplied() ||
$this->cacheHelper->isHyvaTheme()
) {
return parent::getTemplate();
}
Expand All @@ -142,26 +131,4 @@ private function getProfileKey(): ?string
null
);
}

/**
* @return bool
*/
private function isHyvaTheme(): bool
{
if ($this->isHyvaTheme === null) {
$theme = $this->viewDesign->getDesignTheme();
while ($theme) {
if (strpos($theme->getCode(), 'Hyva/') === 0) {
$this->isHyvaTheme = true;
return $this->isHyvaTheme;
}

$theme = $theme->getParentTheme();
}

$this->isHyvaTheme = false;
}

return $this->isHyvaTheme;
}
}
92 changes: 92 additions & 0 deletions Block/Product/ProductList/Linked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

namespace Tweakwise\Magento2Tweakwise\Block\Product\ProductList;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Block\Product\AbstractProduct;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Registry;
use Tweakwise\Magento2Tweakwise\Helper\Cache;

class Linked
{
/**
* @param Cache $cacheHelper
* @param RequestInterface $request
* @param ProductRepositoryInterface $productRepository
* @param Registry $registry
*/
public function __construct(
private readonly Cache $cacheHelper,
private readonly RequestInterface $request,
private readonly ProductRepositoryInterface $productRepository,
private readonly Registry $registry,
) {
}

/**
* @param AbstractProduct $productBlock
* @return int|null
*/
public function getCacheLifetime(AbstractProduct $productBlock): ?int
{
if (!$this->cacheHelper->personalMerchandisingCanBeApplied()) {
return null;
}

$productBlock->setData('ttl', Cache::PRODUCT_LIST_TTL);
$productBlock->setData('cache_lifetime', Cache::PRODUCT_LIST_TTL);
return $productBlock->getData('cache_lifetime');
}

/**
* @return string|null
*/
public function getTemplate(): ?string
{
if (!$this->cacheHelper->personalMerchandisingCanBeApplied()) {
return null;
}

return 'Tweakwise_Magento2Tweakwise::product/list/items.phtml';
}

/**
* @param AbstractProduct $productBlock
* @throws NoSuchEntityException
*/
public function prepareData(AbstractProduct $productBlock): void
{
if ($this->cacheHelper->isEsiRequest($this->request) && !$productBlock->getProduct()) {
$productId = $this->request->getParam('product_id');
$product = $this->productRepository->getById($productId);
$this->registry->register('product', $product);
$productBlock->setData('product', $product);
}
}

/**
* @param AbstractProduct $productBlock
* @param string $route
* @param array $params
* @return array|null
*/
public function getUrl(AbstractProduct $productBlock, string $route = '', array $params = []): ?array
{
if (
!$this->cacheHelper->personalMerchandisingCanBeApplied() ||
$route !== 'page_cache/block/esi'
) {
return null;
}

$params['_query'] = [
'product_id' => $productBlock->getProduct()->getId()
];

return $params;
}
}
96 changes: 96 additions & 0 deletions Block/Product/ProductList/Related.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

namespace Tweakwise\Magento2Tweakwise\Block\Product\ProductList;

use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Block\Product\ProductList\Related as MagentoRelated;
use Magento\Catalog\Model\Product\Visibility as ProductVisibility;
use Magento\Checkout\Model\ResourceModel\Cart as CartResourceModel;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Module\Manager;

class Related extends MagentoRelated
{
/**
* @param Context $context
* @param CartResourceModel $checkoutCart
* @param ProductVisibility $catalogProductVisibility
* @param CheckoutSession $checkoutSession
* @param Manager $moduleManager
* @param Linked $linked
* @param array $data
*/
public function __construct(
Context $context,
CartResourceModel $checkoutCart,
ProductVisibility $catalogProductVisibility,
CheckoutSession $checkoutSession,
Manager $moduleManager,
private readonly Linked $linked,
array $data = []
) {
parent::__construct(
$context,
$checkoutCart,
$catalogProductVisibility,
$checkoutSession,
$moduleManager,
$data
);
}

/**
* @return int|bool|null
*/
protected function getCacheLifetime()
{
$linkedResult = $this->linked->getCacheLifetime($this);
if ($linkedResult) {
return $linkedResult;
}

return parent::getCacheLifetime();
}

/**
* @return string
*/
public function getTemplate()
{
$linkedResult = $this->linked->getTemplate();
if ($linkedResult) {
return $linkedResult;
}

return parent::getTemplate();
}

/**
* @return Related
* @throws NoSuchEntityException
*/
protected function _prepareData()
{
$this->linked->prepareData($this);

return parent::_prepareData();
}

/**
* @param string $route
* @param array $params
* @return string
*/
public function getUrl($route = '', $params = [])
{
$linkedResult = $this->linked->getUrl($this, $route, $params);
if ($linkedResult) {
$params = $linkedResult;
}

return parent::getUrl($route, $params);
}
}
Loading

0 comments on commit c0b2c12

Please sign in to comment.