Skip to content

Commit

Permalink
some changes in featuredproducts api
Browse files Browse the repository at this point in the history
  • Loading branch information
samberrry committed Sep 23, 2021
1 parent e7f17b4 commit 7dbddf5
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
2 changes: 1 addition & 1 deletion binshopsrest.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public function hookModuleRoutes()
]
],
'module-binshopsrest-featuredproducts' => [
'rule' => 'rest/featured-products',
'rule' => 'rest/featuredproducts',
'keywords' => [],
'controller' => 'featuredproducts',
'params' => [
Expand Down
87 changes: 85 additions & 2 deletions controllers/front/featuredproducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
*/

require_once dirname(__FILE__) . '/../AbstractRESTController.php';
require_once dirname(__FILE__) . '/../../classes/RESTProductLazyArray.php';

use PrestaShop\PrestaShop\Adapter\Category\CategoryProductSearchProvider;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchContext;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery;
use PrestaShop\PrestaShop\Core\Product\Search\SortOrder;

/**
* This REST endpoint gets featured products list
Expand All @@ -19,8 +25,7 @@ class BinshopsrestFeaturedproductsModuleFrontController extends AbstractRESTCont
{
protected function processGetRequest()
{
$featuredProducts = Module::getInstanceByName('ps_featuredproducts');
$featuredProductsList = $featuredProducts->getWidgetVariables(null, []);
$featuredProductsList = $this->getFeaturedProducts();

$this->ajaxRender(json_encode([
'code' => 200,
Expand Down Expand Up @@ -56,4 +61,82 @@ protected function processDeleteRequest()
]));
die;
}

public function getFeaturedProducts(){
$category = new Category((int) Configuration::get('HOME_FEATURED_CAT'));

$searchProvider = new CategoryProductSearchProvider(
$this->context->getTranslator(),
$category
);

$context = new ProductSearchContext($this->context);

$query = new ProductSearchQuery();
$nProducts = Configuration::get('HOME_FEATURED_NBR');
if ($nProducts < 0) {
$nProducts = 12;
}

$query
->setResultsPerPage($nProducts)
->setPage(1)
;

if (Configuration::get('HOME_FEATURED_RANDOMIZE')) {
$query->setSortOrder(SortOrder::random());
} else {
$query->setSortOrder(new SortOrder('product', 'position', 'asc'));
}

$result = $searchProvider->runQuery(
$context,
$query
);

$products_for_template = [];
$settings = $this->getProductPresentationSettings();
$retriever = new \PrestaShop\PrestaShop\Adapter\Image\ImageRetriever(
$this->context->link
);

foreach ($result->getProducts() as $rawProduct) {
$populated_product = (new ProductAssembler($this->context))
->assembleProduct($rawProduct);
$lazy_product = new RESTProductLazyArray(
$settings,
$populated_product,
$this->context->language,
new \PrestaShop\PrestaShop\Adapter\Product\PriceFormatter(),
$retriever,
$this->context->getTranslator()
);

$products_for_template[] = $lazy_product->getProduct();
}

return array(
'products' => $products_for_template,
'allProductsLink' => Context::getContext()->link->getCategoryLink($this->getConfigFieldsValues()['HOME_FEATURED_CAT']),
);
}

public function getConfigFieldsValues()
{
return array(
'HOME_FEATURED_NBR' => Tools::getValue('HOME_FEATURED_NBR', (int) Configuration::get('HOME_FEATURED_NBR')),
'HOME_FEATURED_CAT' => Tools::getValue('HOME_FEATURED_CAT', (int) Configuration::get('HOME_FEATURED_CAT')),
'HOME_FEATURED_RANDOMIZE' => Tools::getValue('HOME_FEATURED_RANDOMIZE', (bool) Configuration::get('HOME_FEATURED_RANDOMIZE')),
);
}

private function getFactory()
{
return new ProductPresenterFactory($this->context, new TaxConfiguration());
}

protected function getProductPresentationSettings()
{
return $this->getFactory()->getPresentationSettings();
}
}

0 comments on commit 7dbddf5

Please sign in to comment.