<?php
namespace CoreBundle\Controller;
use CoreBundle\Helper\CoreBundleHelper;
use CoreBundle\Helper\ProductVariants;
use CoreBundle\Model\DefaultProductCategory;
use CoreBundle\Services\ProductPricesHistoryService;
use CoreBundle\Services\ProductService;
use Doctrine\DBAL\DBALException;
use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ProductListInterface;
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Model\DataObject\Folder;
use Pimcore\Model\DataObject\ProductFood;
use Symfony\Component\HttpFoundation\Request;
class ProductController extends AbstractController
{
/**
* @var ProductPricesHistoryService
*/
protected $pricesHistoryService;
/**
* @var ProductService
*/
private $productService;
/**
* @param ProductPricesHistoryService $pricesHistoryService
* @param ProductService $productService
*/
public function __construct(ProductPricesHistoryService $pricesHistoryService, ProductService $productService)
{
$this->pricesHistoryService = $pricesHistoryService;
$this->productService = $productService;
}
/**
* @param Request $request
*
* @return void
* @throws DBALException
*/
public function listByProductCategoryIdAction(Request $request): void
{
$productCategory = DefaultProductCategory::getProductCategoryById($this->view->document->getProperty("productCategoryId"), $this);
$products = CoreBundleHelper::getCoreProductList($this->view->applicationChannel->getPath().$this->view->applicationChannel->getKey());
$products->setCategory(DefaultProductCategory::getProductCategoryById($productCategory->getId(), $this));
$products->setVariantMode(ProductListInterface::VARIANT_MODE_HIDE);
$products->load();
$refinedProductList = $this->productService->prepareProductList($products, true);
$productStockInformCustomerData = [];
$user = $this->getUser();
foreach ($refinedProductList as $product) {
$productStockInformCustomerData[$product['pimId']] = AbstractController::getProductStockInformCustomerExists($user, $product['pimId']);
}
$this->view->productStockInformCustomerData = $productStockInformCustomerData;
$this->view->uri = $request->getUri();
$this->view->user = $user;
$this->view->productCategory = $productCategory;
$this->view->products = $refinedProductList;
}
public function showByArticleNumberAction(Request $request)
{
$product = CoreBundleHelper::getProductByArticleNumber($request->get("articlenumber"), $this);
if (!$product) {
throw $this->createNotFoundException();
}
if($request->get("name") != $product->getUrlTitle()){
return $this->redirectToRoute("shopHandlerProductDetail", ["articlenumber" => $request->get("articlenumber"), "name" => $product->getUrlTitle()]);
}
// Get the variants for a given product
$productVariants = new ProductVariants();
$productVariants->get($product);
$showReferencePrice = $isProductFood = ($product instanceof ProductFood);
$this->view->minimumPrice30DaysAllCountries = $this->pricesHistoryService->getMinimumPrice($product);
$this->view->language = $request->getLocale();
$this->view->product = $product;
$this->view->variants = $productVariants->variants;
$this->view->highlightedProductId = $productVariants->highlightedProductId;
$this->view->showReferencePrice = $showReferencePrice;
$this->view->isProductFood = $isProductFood;
// Set SEO title and description if available
$this->view->seoTitle = $product->getSeoTitle() ?? null;
$this->view->seoDescription = $product->getSeoDescription() ?? null;
$user = $this->getUser();
$this->view->user = $user;
if ($request->get('email')) {
$this->view->email = $request->get('email');
}
$this->view->productStockInformCustomerData = AbstractController::getProductStockInformCustomerExists($user, $product->getId());
return $this->render('Product/showByArticleNumber.html.twig', $this->view->getAllParameters());
}
public function showByArticleIdAction(Request $request)
{
$product = CoreBundleHelper::getProductById($request->get('articleid'));
$variants = null;
$parentObject = $product->getParent();
if($parentObject instanceof Folder){
$variants = $product->getChildren([AbstractObject::OBJECT_TYPE_VARIANT]);
$variants[] = $product;
}else{
$variants = $parentObject->getChildren([AbstractObject::OBJECT_TYPE_VARIANT]);
$variants[] = $parentObject;
}
$showReferencePrice = ($product instanceof ProductFood);
$this->cleanVariants($variants, $product);
$this->sortVariants($variants);
$this->view->minimumPrice30DaysAllCountries = $this->pricesHistoryService->getMinimumPrice($product);
$this->view->language = $request->get("language");
$this->view->product = $product;
$this->view->variants = $variants;
$this->view->showReferencePrice = $showReferencePrice;
}
}