есть фронт контролер
<?php
class FavoriteProductsProductsModuleFrontController extends ProductListingFrontController
{
public function __construct(){
parent::__construct();
$this->module = Module::getInstanceByName("favoriteproducts");
}
public $module;
//public $query;
//private $idCustomer;
public $customerId;
public function setMedia()
{
$this->registerStylesheet(
'front-controller-module',
'modules/' . $this->module->name . '/views/css/front/favoriteproducts-list.css',
[
'media' => 'all',
'priority' => 1000,
]
);
$this->registerJavascript(
'front-controller-module',
'modules/' . $this->module->name . '/views/js/front/ajax/ajax_favoriteproducts_list.js',
[
'position' => 'bottom',
'priority' => 1000,
]
);
$this->registerJavascript(
'front-controller-module-del',
'modules/' . $this->module->name . '/views/js/front/ajax/ajax_favoriteproducts_del.js',
[
'position' => 'bottom',
'priority' => 1000,
]
);
$this->registerJavascript(
'front-controller-module-buy',
'modules/' . $this->module->name . '/views/js/front/ajax/ajax_favoriteproducts_buy.js',
[
'position' => 'bottom',
'priority' => 1000,
]
);
return parent::setMedia();
}
//public function checkAccess()
// {
// return true;
// }
public function initContent()
{
parent::initContent();
//if ($this->customerId->checkAccess($this->context->customerId->id)) {
$this->doProductSearch(
'catalog/listing/id',
[
'entity' => 'id',
'products' => $result->getProducts()
]
);
//}
if (Context::getContext()->customer->logged) {
$admin = false;
$customerShow = true;
$id_customer = (int)$this->context->customer->id;
$db = Db::getInstance();
$id_shop = (int)Context::getContext()->shop->id;
$sql = new DbQuery();
$sql->select('id_product');
$sql->from('favorite_products', 'c');
$sql->where('c.id_customer = ' . (int)$id_customer);
$sql->where('c.id_shop = ' . (int)$id_shop);
$sql->innerJoin('customer', 'pa', 'c.id_customer = ' . (int)$id_customer);
$sql->groupBy('c.id_product');
$products = $db->executeS($sql);
$contextObject = $this->context;
$this->context->smarty->assign(array(
'id' => (int)$contextObject->customer->id,
'id_customer' => $id_customer,
'first_name' => $contextObject->customer->firstname,
'last_name' => $contextObject->customer->lastname,
'customerShow' => $customerShow,
//'products' => $query,
'admin' => $admin,
));
return $this->setTemplate("module:favoriteproducts/views/templates/front/products.tpl");
} else {
$this->context->smarty->assign(array(
'result' => false,
'error' => 'ERROR you need to register',
));
return $this->setTemplate("module:favoriteproducts/views/templates/front/show-info.tpl");
}
}
public function getListingLabel()
{
}
protected function getProductSearchQuery()
{
$query = new FavoriteProductSearchQuery();
$query
->setIdCustomer($this->context->customer->id);
return $query;
}
protected function getDefaultProductSearchProvider()
{
return new FavoriteCategoryProductSearchProvider(
//$this->getTranslator(),
$this->customerId
);
}
function doProductSearch($template, $params = [], $locale = null)
{
}
/**
* Assigns module template for page content.
*
* @param string $template Template filename
*
* @throws PrestaShopException
*/
public function setTemplate($template, $params = [], $locale = null)
{
if (strpos($template, 'module:') === 0) {
$this->template = $template;
} else {
parent::setTemplate($template, $params, $locale);
}
}
}
в getDefaultProductSearchProvider и getProductSearchQuery реальзованы класы , их немного переделал
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchContext;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchProviderInterface;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchResult;
use PrestaShop\PrestaShop\Core\Product\Search\SortOrderFactory;
use Symfony\Component\Translation\TranslatorInterface;
/**
* Responsible of getting products for specific category.
*/
class FavoriteCategoryProductSearchProvider implements ProductSearchProviderInterface
{
private $customerId;
private $translator;
private $category;
private $sortOrderFactory;
public function __construct(
TranslatorInterface $translator
) {
$this->translator = $translator;
$this->customerId;
$this->sortOrderFactory = new SortOrderFactory($this->translator);
}
/**
* @param ProductSearchContext $context
* @param ProductSearchQuery $query
*
* @return ProductSearchResult
*
* @throws \PrestaShopDatabaseException
*/
public function runQuery(
FavoriteProductSearchQuery $query
) {
$id_customer = $query->getIdCustomer();
$db = Db::getInstance();
//$id_customer = (int)$this->context->customer->id;
$request = 'SELECT `id_product` FROM `' . _DB_PREFIX_ . 'favorite_products` WHERE `id_customer` = ". $id_customer ."' ;
//$productsCount = 'SELECT `count('id_product')` FROM `' . _DB_PREFIX_ . 'favorite_products` WHERE `id_customer` = ". $id_customer ."';
$product = $db->executeS($request);
$productsCount = count($products);
$result = new ProductSearchResult();
if (!empty($product)) {
$result
->setProducts($product)
->setTotalProductsCount($productsCount);
$result->setAvailableSortOrders(
$this->sortOrderFactory->getDefaultSortOrders()
);
return $result;
}
}
}
class FavoriteProductSearchQuery
{
private $idCustomer;
public function setIdCustomer($idCustomer)
{
$this->idCustomer = $idCustomer;
return $this;
}
/**
* @return string
*/
public function getIdCustomer()
{
return $this->idCustomer;
}
}
как правильно описать doProductSearch в initConent?