src/Controller/ProductController.php line 251

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