src/Controller/ProductController.php line 251

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Pimcore\Controller\FrontendController;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Contracts\Translation\TranslatorInterface;
  8. use Pimcore\Localization\LocaleServiceInterface;
  9. use Pimcore\Model\DataObject\Recipe;
  10. use Pimcore\Model\DataObject\Review;
  11. use Pimcore\Model\DataObject\ProductBanner;
  12. use Pimcore\Model\DataObject\Product;
  13. use Pimcore\Model\DataObject\ProductAttribute;
  14. use Pimcore\Model\DataObject\ProductCategory;
  15. use Pimcore\Targeting\VisitorInfoStorageInterface;
  16. use Pimcore\Model\Tool\Targeting;
  17. use Pimcore\Model\Tool\Targeting\TargetGroup;
  18. use GeoIp2\Database\Reader;
  19. class ProductController extends FrontendController {
  20.     /**
  21.      * @Template
  22.      *
  23.      * @param Request $request
  24.      *
  25.      * @return array
  26.      */
  27.     /**
  28.      * @var LocaleService
  29.      */
  30.     //  /**
  31.     //  * @var VisitorInfoStorageInterface
  32.     //  */
  33.     private $visitorInfoStorage;
  34.     public function __construct(VisitorInfoStorageInterface $visitorInfoStorage) {
  35.         $this->visitorInfoStorage $visitorInfoStorage;
  36.     }
  37.     public function defaultAction(Request $request) {
  38.         return [];
  39.     }
  40.     public function productAction(Request $request) {
  41.         // $visitorInfo = $this->visitorInfoStorage->getVisitorInfo();
  42.         // $groups = $visitorInfo->getAssignedTargetGroups();
  43.         // $groupids = [];
  44.         // foreach ($groups as $key => $val) {
  45.         //     $groupids[] = $val->id;
  46.         // }
  47.         // if(!$groupids){
  48.         //     $groupids[] = '1';
  49.         // }
  50.         //print_r($groupids);die();
  51.         $groups = [];
  52.         $code 'AE';
  53.         if (!$this->visitorInfoStorage->hasVisitorInfo()) {
  54.             $groups = [];
  55.             $code 'AE';
  56.         }else{
  57.             $visitorInfo $this->visitorInfoStorage->getVisitorInfo();
  58.             if($visitorInfo instanceof \Pimcore\Targeting\Model\VisitorInfo){
  59.             
  60.                     $groups $visitorInfo->getAssignedTargetGroups();
  61.                     $city $visitorInfo->get('geoip');
  62.                     if($city){
  63.                         $code $city['country']['iso_code'];
  64.                     }
  65.                     
  66.                     if (!$code) {
  67.                         $code 'AE';
  68.                     }   
  69.                 
  70.             }
  71.         }
  72.         $groupids = [];
  73.         foreach ($groups as $key => $val) {
  74.             $groupids[] = $val->getId();
  75.         }
  76.         if(!$groupids){
  77.             $groupids[] = '22';
  78.         }
  79.         // Region wise ip Handler start
  80.         try {
  81.             $ipaddress '';
  82.             if (isset($_SERVER['HTTP_CLIENT_IP'])) {
  83.                 $ipaddress $_SERVER['HTTP_CLIENT_IP'];
  84.             } else if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  85.                 $ipaddress $_SERVER['HTTP_X_FORWARDED_FOR'];
  86.             } else if (isset($_SERVER['HTTP_X_FORWARDED'])) {
  87.                 $ipaddress $_SERVER['HTTP_X_FORWARDED'];
  88.             } else if (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
  89.                 $ipaddress $_SERVER['HTTP_FORWARDED_FOR'];
  90.             } else if (isset($_SERVER['HTTP_FORWARDED'])) {
  91.                 $ipaddress $_SERVER['HTTP_FORWARDED'];
  92.             } else if (isset($_SERVER['REMOTE_ADDR'])) {
  93.                 $ipaddress $_SERVER['REMOTE_ADDR'];
  94.             } else {
  95.                 $ipaddress 'UNKNOWN';
  96.             }
  97.             if ($ipaddress != 'UNKNOWN') {
  98.                 $ip_info = new  Reader(__DIR__ '/GeoLite2-Country.mmdb');
  99.                 $record $ip_info->country($ipaddress);
  100.                 if ($record) {
  101.                     $ip $record->country->isoCode;
  102.                 }
  103.             }
  104.         } catch (\Exception $e) {
  105.             $ip '';
  106.         }
  107.         $code $ip// issign country-iso code to code varible
  108.         if ($code == 'AE') {
  109.             $groupids[] = '29';
  110.         } elseif ($code == 'SA') {
  111.             $groupids[] = '27';
  112.         } elseif ($code == 'OM' || $code == 'QA' || $code == 'KW' || $code == 'BH') {
  113.             $groupids[] = '28';
  114.         } else {
  115.             //$code = 'AE';
  116.             $groupids[] = '29';
  117.         }
  118.         if(!$groupids){
  119.             $groupids[] = '22';
  120.         }
  121.         // Region wise ip Handler end  
  122.         $Products = new Product\Listing();
  123.         //$Products->setCondition("regions IN (?)", [$groupids]);
  124.         $Products->load();
  125.         foreach ($Products as $key => $Product) {
  126.             $product[] = $Product;
  127.         }
  128.         $Categories = new ProductCategory\Listing();
  129.         $Categories->setOrderKey("sort");
  130.         $Categories->setOrder("asc");
  131.         $Categories->load();
  132.         foreach ($Categories as $key => $Category) {
  133.             $category[] = $Category;
  134.             //$categoryy[] = $Category->getName() . $Category->getSort();
  135.         }
  136.         //print_r($categoryy);die();
  137.         $Banner = new ProductBanner\Listing();
  138.         return $this->render('Product/Product.html.twig', [
  139.             'banners' => $Banner,
  140.             'product' => $product,
  141.             'category' => $category,
  142.             'groupids' => $groupids,
  143.             // 'groupids'=>'1', // temporary basses
  144.         ]);
  145.         // $this->view->banners = $Banner;
  146.         // $this->view->product = $product;       
  147.         // $this->view->category = $category;
  148.         // $this->view->groupids = $groupids;
  149.     }
  150.     //  /**
  151.     //      * @Route("/{locale}/product/{url}", name="product_category")
  152.     //      */
  153.     // public function productcategoryAction(Request $request)
  154.     // {   
  155.     //     // $url = strtolower($request->get('url'));
  156.     //     // $recipes = new Recipe\Listing();
  157.     //     // $recipes->load();
  158.     //     // foreach ($recipes as $key => $recipe) {
  159.     //     //     // $product = DefaultProduct::getById($product->getId());
  160.     //     //     break;
  161.     //     // }
  162.     //     // $this->view->recipe = $recipe;
  163.     //     $visitorInfo = $this->visitorInfoStorage->getVisitorInfo();
  164.     //     $groups = $visitorInfo->getAssignedTargetGroups();
  165.     //     $groupids = [];
  166.     //     // $city = $visitorInfo->get('geoip');
  167.     //     // $code = $city['country']['iso_code'];
  168.     //     // if (!$code) {
  169.     //     //     $code = 'AE';
  170.     //     // }
  171.     //     foreach ($groups as $key => $val) {
  172.     //         $groupids[] = $val->id;
  173.     //     }
  174.     //     if(!$groupids){
  175.     //         $groupids[] = '1';
  176.     //     }
  177.     //     if($request->get('url')){
  178.     //         $url = strtolower($request->get('url'));
  179.     //         $locale = $request->getLocale();
  180.     //         $id = $request->get('id');
  181.     //         $Products = new ProductCategory\Listing();
  182.     //         $Products->setCondition("url IN (?)", [[$url]]);
  183.     //         $Products->load();
  184.     //         foreach ($Products as $key => $Product) {
  185.     //            $product[] = ProductCategory::getById($Product->getId());
  186.     //         }
  187.     //     }
  188.     //     return $this->render('Product/ProductCategory.html.twig', [
  189.     //         'category'=>$product,
  190.     //         'locale'=>$locale,
  191.     //         'groupids'=>$groupids,
  192.     //         //'country'=>$code,
  193.     //         'country'=>'AE',  // temporary basses
  194.     //     ]);
  195.     //     // $this->view->category = $product;
  196.     //     // $this->view->locale = $locale;
  197.     //     // $this->view->groupids = $groupids;
  198.     //     // $this->view->country = $code;
  199.     // }
  200.     // /**
  201.     //  * @Route("/{locale}/product/{url}", name="blog_list")
  202.     //  */
  203.     public function productdetailsAction(Request $request) {
  204.         // Region wise ip Handler start
  205.         try {
  206.             $ipaddress '';
  207.             if (isset($_SERVER['HTTP_CLIENT_IP'])) {
  208.                 $ipaddress $_SERVER['HTTP_CLIENT_IP'];
  209.             } else if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  210.                 $ipaddress $_SERVER['HTTP_X_FORWARDED_FOR'];
  211.             } else if (isset($_SERVER['HTTP_X_FORWARDED'])) {
  212.                 $ipaddress $_SERVER['HTTP_X_FORWARDED'];
  213.             } else if (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
  214.                 $ipaddress $_SERVER['HTTP_FORWARDED_FOR'];
  215.             } else if (isset($_SERVER['HTTP_FORWARDED'])) {
  216.                 $ipaddress $_SERVER['HTTP_FORWARDED'];
  217.             } else if (isset($_SERVER['REMOTE_ADDR'])) {
  218.                 $ipaddress $_SERVER['REMOTE_ADDR'];
  219.             } else {
  220.                 $ipaddress 'UNKNOWN';
  221.             }
  222.             if ($ipaddress != 'UNKNOWN') {
  223.                 $ip_info = new  Reader(__DIR__ '/GeoLite2-Country.mmdb');
  224.                 $record $ip_info->country($ipaddress);
  225.                 if ($record) {
  226.                     $ip $record->country->isoCode;
  227.                 }
  228.             }
  229.         } catch (\Exception $e) {
  230.             $ip '';
  231.         }
  232.         $code $ip// issign country-iso code to code varible
  233.         if ($code == 'AE') {
  234.             $groupids[] = '29';
  235.         } elseif ($code == 'SA') {
  236.             $groupids[] = '27';
  237.         } elseif ($code == 'OM' || $code == 'QA' || $code == 'KW' || $code == 'BH') {
  238.             $groupids[] = '28';
  239.         } else {
  240.             //$code = 'AE';
  241.             $groupids[] = '29';
  242.         }
  243.         // Region wise ip Handler end    
  244.         if ($request->get('url')) {
  245.             $variant_id 0;
  246.             $url strtolower($request->get('url'));
  247.             $locale $request->getLocale();
  248.             $cat false;
  249.            
  250.             $cats = new ProductCategory\Listing();
  251.             $cats->setCondition("url IN (?)", [[$url]]);
  252.             $cats->load();
  253.             if ($cats && count($cats) > 0) {
  254.                 foreach ($cats as $key => $Product) {
  255.                     $productt[] = ProductCategory::getById($Product->getId());
  256.                 }
  257.                 $cat true;
  258.             }
  259.             //die($url);
  260.             $products = new Product\Listing();
  261.             $products->setCondition("url IN (?)", [[$url]]);
  262.             $products->load();
  263.             $count count($products);
  264.             if ($count 0) {
  265.                 foreach ($products as $key => $product) {
  266.                     $product Product::getById($product->getId());
  267.                 }
  268.             } else {
  269.                 $ProductAttribute = new ProductAttribute\Listing();
  270.                 $ProductAttribute->setCondition("url IN (?)", [[$url]]);
  271.                 $ProductAttribute->load();
  272.                 $product_used_recipe_IDs = [];
  273.                 foreach ($ProductAttribute as $key => $productt) {
  274.                     $variant_id $productt->getId();
  275.                     
  276.                     $product ProductAttribute::getById($productt->getId())->getProduct();
  277.                     if(empty($product)){
  278.                         $productsList = new Product\Listing();
  279.                         $productsList->setCondition("attributes LIKE :attributes", ["attributes" => "%object|".$variant_id."%"]);
  280.                         $productsList->load();
  281.                         if(!empty($productsList)){
  282.                             foreach ($productsList as $key => $value) {
  283.                                 # code...
  284.                                 $product $value;
  285.                                 break;
  286.                             }
  287.                         
  288.                         }
  289.                         
  290.                     }
  291.                  
  292.                     foreach ($product->getRecipe() as $product_used_recipes) {
  293.                         $product_used_recipe_IDs[] = $product_used_recipes->getID();
  294.                     }
  295.                 
  296.                 }
  297.                
  298.                 $product_recipe_reviews = new Review\Listing();
  299.                 $product_recipe_reviews->setCondition("recipe_id__id in (?)", [$product_used_recipe_IDs ]);
  300.                  $product_recipe_reviews->setLimit(4);
  301.                 $product_recipe_reviews->load();
  302.                 
  303.             }
  304.         }
  305.         if ($cat) {
  306.             return $this->render('Product/ProductCategory.html.twig', [
  307.                 'category' => $productt,
  308.                 'locale' => $locale,
  309.                 'groupids' => $groupids,
  310.                 'country' => $code,
  311.                 //'country'=>'AE',  // temporary basses
  312.                 'cat' => $cat,
  313.             ]);
  314.             // $this->view->category = $productt;
  315.             // $this->view->locale = $locale;
  316.             // $this->view->groupids = $groupids;
  317.             // $this->view->country = $code;
  318.             // $this->view->cat = $cat;
  319.         } else {
  320.             return $this->render('Product/Productdetail.html.twig', [
  321.                 'country' => $code,
  322.                 //'country'=>'AE',  // temporary basses
  323.                 //'vindex'=>$id2,
  324.                 'variant_id' => $variant_id,
  325.                 'product' => $product,
  326.                 'productsss' => $product,
  327.                 'product_recipe_reviews' => $product_recipe_reviews,
  328.                 'product_used_recipe_IDs' => $product_used_recipe_IDs,
  329.             ]);
  330.             // $this->view->country = $code;
  331.             // $this->view->vindex = $id2; 
  332.             // $this->view->variant_id = $variant_id; 
  333.             // $this->view->product = $product;
  334.             // $this->view->productsss = $product;
  335.         }
  336.     }
  337. }