<?php
namespace App\Controller\Catalog;
use App\Controller\BaseController;
use App\Entity\Setting;
use App\Entity\Store;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/catalog/bestsellers", name="best_sellers_products")
*/
class GetBestSellersController extends BaseController
{
public function __invoke(Request $request)
{
$container = 'CheckoutPreview';
$this->setState('campaignProduct', $this->getCampaignProduct());
return $this->response('catalog/product/best_sellers_product.html.twig', [
'props' => [
],
'page' => $container,
], [
'X-Robots-Tag' => 'noindex'
]);
}
private function getCampaignProduct(): array
{
$em = $this->getDoctrine()->getManager();
$showNotInStocks = $em->getRepository(Setting::class)->findOneBy(['key' => 'showNotInStocks'])->getValue();
$urNo = $em->getRepository(Setting::class)->findOneBy(['key' => 'urNo'])->getValue();
$em = $this->getDoctrine()->getManager();
if ($showNotInStocks !== null && $showNotInStocks === "0") {
$result = $em->createQuery('select m from App:Product m where m.quantity > 0 AND m.salesNumber > 0 AND m.parent IS NULL AND m.id != ' . $urNo . '')
->setMaxResults(20)
->getResult();
} else {
$result = $em->createQuery('select m from App:Product m where m.salesNumber > 0 AND m.parent IS NULL AND m.id != ' . $urNo . '')
->setMaxResults(20)
->getResult();
}
$store = $this->getDoctrine()->getRepository(Store::class)->find(1);
if (count($result) == 0) {
if ($showNotInStocks !== null && $showNotInStocks === "0") {
$result = $em->createQuery('select m from App:Product m where m.quantity > 0 AND m.parent IS NULL AND m.id != ' . $urNo . ' ORDER BY m.id DESC')
->setMaxResults
(20)
->getResult();
} else {
$result = $em->createQuery('select m from App:Product m where m.parent IS NULL AND m.id != ' . $urNo . ' ORDER BY m.id DESC')
->setMaxResults
(20)
->getResult();
}
return [
'items' => $result,
'store' => $store
];
} else {
return [
'items' => $result,
'store' => $store
];
}
}
}