src/Controller/ProductController.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Pimcore\Model\DataObject\Category;
  4. use Pimcore\Model\DataObject\Product;
  5. use Pimcore\Model\Document;
  6. use App\Classes\Controller\AbstractFrontController as AbstractFrontController;
  7. use App\Classes\DataMapper\Customer\Address\AddressDataMapper;
  8. use App\Classes\DataMapper\Product\ProductCardDataMapper;
  9. use App\Classes\DataMapper\Product\ProductDataMapper;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  11. use Symfony\Component\HttpFoundation\Request;
  12. class ProductController extends AbstractFrontController
  13. {
  14.     #[Template()]
  15.     public function showAction(Request $request)
  16.     {
  17.         $product Product::getById($request->get('id'));
  18.         $documentCategory null;
  19.         if (!$product || !$product->isPublished()) {
  20.             // error page
  21.             throw new \Exception('Product not found!');
  22.         }
  23.         if ($product->getProductType() != 'grouped') {
  24.             throw new \Exception('Product not found!');
  25.         }
  26.         $mapperProduct = (new ProductDataMapper($product))->toArray($request);
  27.         $this->view->product $mapperProduct;
  28.         $this->view->variants ProductDataMapper::list($product->getProducts())->toArray($request);
  29.         $this->view->relatedProducts ProductCardDataMapper::list($product->getRelatedProducts())->all($request);
  30.         $this->view->purchasedTogether ProductCardDataMapper::list($product->getPurchasedTogether())->all($request);
  31.         $this->view->replacementParts ProductCardDataMapper::list($product->getReplacementParts())->all($request);
  32.         $isEU false;
  33.         if ($this->getUser()) {
  34.             $defaultBillingAddress $this->getUser()->getDefaultBillingAddressObject();
  35.             $addresses = (new AddressDataMapper($defaultBillingAddress))->toArray($request);
  36.             $isEU $addresses['isEU'];
  37.             if (!$isEU) {
  38.                 $defaultShippingAddress $this->getUser()->getDefaultShippingAddressObject();
  39.                 $addresses = (new AddressDataMapper($defaultShippingAddress))->toArray($request);
  40.                 $isEU $addresses['isEU'];
  41.             }
  42.         }
  43.         $this->view->isEU $isEU;
  44.         $category $mapperProduct['category'];
  45.         if ($categoryId $request->get('navigated_from_object')) {
  46.             foreach($product->getCategories() as $_category) {
  47.                 if ($_category->getId() != $categoryId) {
  48.                     continue;
  49.                 }
  50.                 $category $_category;
  51.             }
  52.         }
  53.         if ($category && $category->getCategoryName() != 'Root category') {
  54.             // get breadcrumbs
  55.             $db \Pimcore\Db::get();
  56.             $categoryDocumentIds $db->fetchAll('SELECT cid FROM properties WHERE name = "category" AND data = "' .
  57.                 $category->getId() . '"');
  58.             if ($categoryDocumentIds) {
  59.                 foreach ($categoryDocumentIds as $categoryDocumentId) {
  60.                     $document Document::getById($categoryDocumentId['cid']);
  61.                     if ($document && $document->getProperty('products_filter')){
  62.                         $documentCategory $document->getProperty('products_filter')->getParent();
  63.                         if ($documentCategory->getId() == $category->getId()){
  64.                             break;
  65.                         }
  66.                     }
  67.                 }
  68.                 $breadcrumbs['category'] = [
  69.                     'name' => $category->getCategoryName(),
  70.                     'href' => $document $document->getHref() : ''
  71.                 ];
  72.             }
  73.             $parentCategory $documentCategory?->getParent();
  74.             if ($parentCategory instanceof Category) {
  75.                 $parentDocument $document->getParent();
  76.                 if ($parentDocument) {
  77.                     $breadcrumbs['parentCategory'] = [
  78.                         'name' => $parentCategory->getCategoryName(),
  79.                         'href' => $parentDocument $parentDocument->getHref() : ''
  80.                     ];
  81.                 }
  82.             }
  83.             $this->view->breadcrumbs $breadcrumbs ?? [];
  84.             //get division document (for header, foooter)
  85.             while ($category->getParent()->getCategoryName() != 'Root category') {
  86.                 $category $category->getParent();
  87.                 if (!$category instanceof Category) {
  88.                     break;
  89.                 }
  90.             }
  91.             $db \Pimcore\Db::get();
  92.             $rootDocumentId $db->fetchOne('SELECT cid FROM properties WHERE name = "category" AND data = "' .
  93.                 $category->getId() . '"');
  94.             if ($rootDocumentId) {
  95.                 $rootDocument Document::getById($rootDocumentId);
  96.                 $request->request->set('division'$rootDocumentId);
  97.                 $this->view->document $rootDocument;
  98.                 $this->view->division $rootDocument->getKey();
  99.             }
  100.         }
  101.     }
  102.     protected function getProductGroup($product)
  103.     {
  104.         return $product;
  105.     }
  106. }