Advertisement
Guest User

Model/ResourceModel/NodeType/Product.php

a guest
Apr 7th, 2020
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.95 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Snowdog\Menu\Model\ResourceModel\NodeType;
  4.  
  5. use Magento\Store\Model\Store;
  6. use Magento\Framework\App\ResourceConnection;
  7. use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
  8. use Magento\Framework\EntityManager\MetadataPool;
  9.  
  10. class Product extends AbstractNode
  11. {
  12.     /**
  13.      * @var CollectionFactory
  14.      */
  15.     private $productCollection;
  16.  
  17.     /**
  18.      * @var MetadataPool
  19.      */
  20.     private $metadataPool;
  21.  
  22.     public function __construct(
  23.         ResourceConnection $resource,
  24.         CollectionFactory $productCollection,
  25.         MetadataPool $metadataPool
  26.     ) {
  27.         $this->productCollection = $productCollection;
  28.         $this->metadataPool = $metadataPool;
  29.         parent::__construct($resource);
  30.     }
  31.  
  32.     /**
  33.      * @param int   $storeId
  34.      * @param array $productIds
  35.      * @return array
  36.      */
  37.     public function fetchData($storeId = Store::DEFAULT_STORE_ID, $productIds = [])
  38.     {
  39.         $connection = $this->getConnection('read');
  40.         $table = $this->getTable('url_rewrite');
  41.         $select = $connection
  42.             ->select()
  43.             ->from($table, ['entity_id', 'request_path'])
  44.             ->where('entity_type = ?', 'product')
  45.             ->where('redirect_type = ?', 0)
  46.             ->where('store_id = ?', $storeId)
  47.             ->where('entity_id IN (?)', $productIds)
  48.             ->where('metadata IS NULL');
  49.  
  50.         return $connection->fetchPairs($select);
  51.     }
  52.  
  53.     /**
  54.      * @param int $websiteId
  55.      * @param int $customerGroupId
  56.      * @param array $productIds
  57.      * @return array
  58.      */
  59.     public function fetchPriceData($websiteId, $customerGroupId, $productIds = [])
  60.     {
  61.         $connection = $this->getConnection('read');
  62.         $table = $this->getTable('catalog_product_index_price');
  63.         $select = $connection
  64.             ->select()
  65.             ->from($table, ['entity_id', 'final_price'])
  66.             ->where('customer_group_id = ?', $customerGroupId)
  67.             ->where('website_id = ?', $websiteId)
  68.             ->where('entity_id IN (?)', $productIds);
  69.  
  70.         return $connection->fetchPairs($select);
  71.     }
  72.  
  73.     /**
  74.      * @param int $storeId
  75.      * @param array $productIds
  76.      * @return array
  77.      */
  78.     public function fetchImageData($storeId, $productIds = [])
  79.     {
  80.         $collection = $this->productCollection->create();
  81.         $collection->addAttributeToSelect(['thumbnail'], 'left')
  82.             ->addFieldToFilter('entity_id', ['in' => $productIds])
  83.             ->addStoreFilter($storeId);
  84.  
  85.         $imageData = [];
  86.         foreach ($collection->getData() as $data) {
  87.             $imageData[$data['entity_id']] = $data['thumbnail'] ?? '';
  88.         }
  89.  
  90.         return $imageData;
  91.     }
  92.  
  93.     /**
  94.      * @inheritDoc
  95.      */
  96.     public function fetchConfigData()
  97.     {
  98.         return [];
  99.     }
  100.  
  101.     /**
  102.      * @param int $storeId
  103.      * @param array $productIds
  104.      * @return array
  105.      */
  106.     public function fetchTitleData($storeId = Store::DEFAULT_STORE_ID, $productIds = [])
  107.     {
  108.         $collection = $this->productCollection->create();
  109.         $collection->addAttributeToSelect(['name'])
  110.             ->addFieldToFilter('entity_id', ['in' => $productIds])
  111.             ->addStoreFilter($storeId);
  112.  
  113.         $titleData = [];
  114.         foreach ($collection->getData() as $data) {
  115.             $titleData[$data['entity_id']] = $data['name'] ?? '';
  116.         }
  117.  
  118.         return $titleData;
  119.     }
  120.  
  121.     public function fetchColourData($storeId = Store::DEFAULT_STORE_ID, $productIds = [])
  122.     {
  123.         $collection = $this->productCollection->create();
  124.         $collection->addAttributeToSelect(['colour'])
  125.             ->addFieldToFilter('entity_id', ['in' => $productIds])
  126.             ->addStoreFilter($storeId);
  127.  
  128.         $colourData = [];
  129.         foreach ($collection->getData() as $data) {
  130.             $colourData[$data['entity_id']] = $data['colour'] ?? '';
  131.         }
  132.  
  133.         return $colourData;
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement