Facebook
From Putrid Camel, 5 Years ago, written in PHP.
Embed
Download Paste or View Raw
Hits: 247
  1. *
  2.         Cache category ids FOR product breadcrumps;
  3.      */
  4.     public function cacheCategoryIds(){
  5.         $identifier='network_cache_category_ids';
  6.         $this->cache = \Magento\Framework\App\ObjectManager::getInstance()->get('\Magento\Framework\App\CacheInterface');
  7.         if(!$this->cache->load($identifier)){
  8.             $data=$this->getCategoriesToCache();
  9.             $tags=array();
  10.             $this->cache->save($data, $identifier, $tags, 60*5);
  11.         }
  12.         return $this->cache->load($identifier);
  13.  
  14.     }
  15.  
  16.     public function getCategoriesToCache(){
  17.         $connection = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\App\ResourceConnection')->getConnection('\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION');
  18.         $result = $connection->fetchCol("SELECT entity_id FROM m2_catalog_category_entity_text where attribute_id='161' and value!='' and store_id='0'; ");
  19.         return $this->getSerializer()->serialize($result);
  20.     }
  21.  
  22.     public function getCategoriesFromCache(){
  23.         return $this->getSerializer()->unserialize($this->cacheCategoryIds());
  24.     }
  25.  
  26.     private function getSerializer()
  27.     {
  28.         if ($this->serializer === null) {
  29.             $this->serializer = \Magento\Framework\App\ObjectManager::getInstance()
  30.                 ->get('\Magento\Framework\Serialize\Serializer\Json');
  31.         }
  32.         return $this->serializer;
  33.     }
  34.    
  35.     public function  getParentIds($categoryId = false)
  36.     {
  37.         $categoryFactory = \Magento\Framework\App\ObjectManager::getInstance()->get('\Magento\Catalog\Model\CategoryFactory');
  38.         $categoryHelper = \Magento\Framework\App\ObjectManager::getInstance()->get('\Magento\Catalog\Helper\Category');
  39.         $categoryRepository = \Magento\Framework\App\ObjectManager::getInstance()->get('\Magento\Catalog\Model\CategoryRepository');
  40.         $category = $categoryFactory->create()->load($categoryId);  
  41.         foreach ($category->getParentCategories() as $cat) {
  42.            $ids[]=array('id'=> $cat->getId(),
  43.                   'name'=> $cat->getName(),
  44.                   'url'=> explode('?', $cat->getUrl())[0]);
  45.         }
  46.         return $ids;  
  47.     }
  48.  
  49.     public function getBreadCrumpsCatIds($current_cats){
  50.         $data = array_intersect($current_cats, $this->getCategoriesFromCache());
  51.         if(!empty($data)){
  52.                 $id = max($data);
  53.                 return $this->getParentIds($id);
  54.         }
  55.         else{
  56.                 return array();
  57.         }
  58.         //return $this->getParentIds($id);
  59.         //return array_intersect($current_cats, $this->getCategoriesFromCache())
  60.     }
  61.  
  62.     public function renderBreadCrumps($current_cats){
  63.         $i=1;
  64.         $script = '<script type="application/ld+json">{
  65.                    "@context": "https://schema.org",
  66.                    "@type":"BreadcrumbList",
  67.                    "itemListElement": [
  68.                    {"@type":"ListItem","position":'.$i.',"item":{"@id":"https://bookland.com.pl/","name":"Bookland"}},';                  
  69.         foreach ($this->getBreadCrumpsCatIds($current_cats) as $value) {
  70.             $i++;
  71.             $script .='{"@type":"ListItem","position":'.$i.',"item":{"@id":"'.$value['url'].'","name":"'.$value['name'].'"}},';
  72.         }
  73.         $script=rtrim($script,',');
  74.    $script.=']}</script>';
  75.    return $script;
  76.     }