-
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.
Merge pull request #10 from Oksydan/upselling
Upselling block in cart
- Loading branch information
Showing
25 changed files
with
466 additions
and
25 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
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Oksydan\IsFavoriteProducts\Cache; | ||
|
||
use Oksydan\IsFavoriteProducts\Hook\DisplayCrossSellingShoppingCart; | ||
|
||
class TemplateCache implements TemplateCacheInterface | ||
{ | ||
public $cacheableHooksCollection; | ||
|
||
public function __construct($cacheableHooksCollection) | ||
{ | ||
$this->cacheableHooksCollection = $cacheableHooksCollection; | ||
} | ||
|
||
public function clearCartTemplateCache(): void | ||
{ | ||
foreach ($this->cacheableHooksCollection as $hook) { | ||
if ($hook instanceof DisplayCrossSellingShoppingCart) { | ||
$hook->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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Oksydan\IsFavoriteProducts\Cache; | ||
|
||
interface TemplateCacheInterface | ||
{ | ||
public function clearCartTemplateCache(): void; | ||
} |
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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Oksydan\IsFavoriteProducts\Hook; | ||
|
||
use Oksydan\IsFavoriteProducts\Cache\TemplateCache; | ||
use Oksydan\IsFavoriteProducts\Services\FavoriteProductService; | ||
|
||
class ActionCartSave extends AbstractHook | ||
{ | ||
private TemplateCache $templateCache; | ||
|
||
public function __construct( | ||
\Is_favoriteproducts $module, | ||
\Context $context, | ||
FavoriteProductService $favoriteProductService, | ||
TemplateCache $templateCache | ||
) { | ||
parent::__construct($module, $context, $favoriteProductService); | ||
$this->templateCache = $templateCache; | ||
} | ||
|
||
public function execute(array $params): void | ||
{ | ||
if (empty($this->context->cart->id)) { | ||
return; | ||
} | ||
|
||
$this->templateCache->clearCartTemplateCache(); | ||
} | ||
} |
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
Oops, something went wrong.