-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
371 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Oksydan\IsFavoriteProducts\Hook; | ||
|
||
abstract class AbstractCacheableDisplayHook extends AbstractDisplayHook | ||
{ | ||
public function execute(array $params): string | ||
{ | ||
if (!$this->shouldBlockBeDisplayed($params)) { | ||
return ''; | ||
} | ||
|
||
if (!$this->isTemplateCached()) { | ||
$this->assignTemplateVariables($params); | ||
} | ||
|
||
return $this->module->fetch($this->getTemplateFullPath(), $this->getCacheKey()); | ||
} | ||
|
||
protected function getCacheKey(): string | ||
{ | ||
return $this->module->getCacheId($this->module->name . '|' . $this->context->cart->id); | ||
} | ||
|
||
protected function isTemplateCached(): bool | ||
{ | ||
return $this->module->isCached($this->getTemplateFullPath(), $this->getCacheKey()); | ||
} | ||
|
||
public function clearCache(): void | ||
{ | ||
$this->module->_clearCache($this->getTemplateFullPath(), $this->getCacheKey()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Oksydan\IsFavoriteProducts\Hook; | ||
|
||
use Oksydan\IsFavoriteProducts\Services\FavoriteProductService; | ||
|
||
class ActionCartSave extends AbstractHook | ||
{ | ||
private DisplayCrossSellingShoppingCart $displayCrossSellingShoppingCart; | ||
|
||
public function __construct( | ||
\Is_favoriteproducts $module, | ||
\Context $context, | ||
FavoriteProductService $favoriteProductService, | ||
DisplayCrossSellingShoppingCart $displayCrossSellingShoppingCart | ||
) { | ||
parent::__construct($module, $context, $favoriteProductService); | ||
$this->displayCrossSellingShoppingCart = $displayCrossSellingShoppingCart; | ||
} | ||
|
||
public function execute(array $params): void | ||
{ | ||
if (empty($this->context->cart->id)) { | ||
return; | ||
} | ||
|
||
$this->displayCrossSellingShoppingCart->clearCache(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Oksydan\IsFavoriteProducts\Hook; | ||
|
||
use Oksydan\IsFavoriteProducts\DTO\FavoriteProduct; | ||
use Oksydan\IsFavoriteProducts\Presenter\FrontProductProductPresenter; | ||
use Oksydan\IsFavoriteProducts\Services\FavoriteProductService; | ||
use PrestaShop\PrestaShop\Adapter\Presenter\Product\ProductLazyArray; | ||
|
||
class DisplayCrossSellingShoppingCart extends AbstractCacheableDisplayHook | ||
{ | ||
private FrontProductProductPresenter $productPresenter; | ||
|
||
public function __construct( | ||
\Is_favoriteproducts $module, | ||
\Context $context, | ||
FavoriteProductService $favoriteProductService, | ||
FrontProductProductPresenter $productPresenter | ||
) { | ||
parent::__construct($module, $context, $favoriteProductService); | ||
$this->productPresenter = $productPresenter; | ||
} | ||
|
||
private const TEMPLATE_FILE = 'displayCrossSellingShoppingCart.tpl'; | ||
|
||
protected function getTemplate(): string | ||
{ | ||
return self::TEMPLATE_FILE; | ||
} | ||
|
||
protected function assignTemplateVariables(array $params) | ||
{ | ||
$products = $this->favoriteProductService->getFavoriteProductsForCartUpSelling($this->getExcludedProducts()); | ||
|
||
$products = array_map(function (array $favoriteProduct) { | ||
return $this->presentProduct($favoriteProduct); | ||
}, $products); | ||
|
||
$this->context->smarty->assign([ | ||
'products' => $products, | ||
]); | ||
} | ||
|
||
private function getExcludedProducts(): array | ||
{ | ||
$smartyCart = $this->context->smarty->getTemplateVars('cart'); | ||
$products = $smartyCart['products'] ?? []; | ||
$productsCollection = []; | ||
|
||
foreach ($products as $product) { | ||
$productsCollection[] = (new FavoriteProduct()) | ||
->setIdProduct((int) $product['id_product']) | ||
->setIdProductAttribute((int) $product['id_product_attribute']); | ||
} | ||
|
||
return $productsCollection; | ||
} | ||
|
||
private function presentProduct(array $product): ProductLazyArray | ||
{ | ||
return $this->productPresenter->present($product); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Oksydan\IsFavoriteProducts\Presenter; | ||
|
||
use PrestaShop\PrestaShop\Adapter\Presenter\Product\ProductLazyArray; | ||
|
||
interface FrontProductPresenterInterface | ||
{ | ||
public function present(array $product): ProductLazyArray; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Oksydan\IsFavoriteProducts\Presenter; | ||
|
||
use PrestaShop\PrestaShop\Adapter\Presenter\Product\ProductLazyArray; | ||
|
||
class FrontProductProductPresenter implements FrontProductPresenterInterface | ||
{ | ||
protected \Context $context; | ||
|
||
public function __construct( | ||
\Context $context | ||
) { | ||
$this->context = $context; | ||
} | ||
|
||
/** | ||
* @param array{id_product: int, id_product_attribute: int} $product | ||
* | ||
* @return ProductLazyArray | ||
*/ | ||
public function present(array $product): ProductLazyArray | ||
{ | ||
$presenterFactory = $this->presenterFactory(); | ||
$presenter = $presenterFactory->getPresenter(); | ||
|
||
return $presenter->present( | ||
$presenterFactory->getPresentationSettings(), | ||
(new \ProductAssembler($this->context))->assembleProduct($product), | ||
$this->context->language | ||
); | ||
} | ||
|
||
private function presenterFactory(): \ProductPresenterFactory | ||
{ | ||
return new \ProductPresenterFactory($this->context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.