Collection.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\ResourceModel\Catalog;
  3. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  4. {
  5. /**
  6. * @var string
  7. */
  8. protected $_idFieldName = 'id';
  9. /**
  10. * @var \Dotdigitalgroup\Email\Helper\Data
  11. */
  12. protected $helper;
  13. /**
  14. * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
  15. */
  16. public $productCollection;
  17. /**
  18. * Initialize resource collection.
  19. *
  20. * @return null
  21. */
  22. public function _construct()
  23. {
  24. $this->_init(
  25. \Dotdigitalgroup\Email\Model\Catalog::class,
  26. \Dotdigitalgroup\Email\Model\ResourceModel\Catalog::class
  27. );
  28. }
  29. /**
  30. * Collection constructor.
  31. *
  32. * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
  33. * @param \Psr\Log\LoggerInterface $logger
  34. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  35. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  36. * @param \Dotdigitalgroup\Email\Helper\Data $helper
  37. * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollection
  38. * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection
  39. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb|null $resource
  40. */
  41. public function __construct(
  42. \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
  43. \Psr\Log\LoggerInterface $logger,
  44. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  45. \Magento\Framework\Event\ManagerInterface $eventManager,
  46. \Dotdigitalgroup\Email\Helper\Data $helper,
  47. \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollection,
  48. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  49. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  50. ) {
  51. $this->helper = $helper;
  52. $this->productCollection = $productCollection;
  53. parent::__construct(
  54. $entityFactory,
  55. $logger,
  56. $fetchStrategy,
  57. $eventManager,
  58. $connection,
  59. $resource
  60. );
  61. }
  62. /**
  63. * Get product collection to export.
  64. *
  65. * @param null|string|bool|int|\Magento\Store\Model\Store $store
  66. * @param int $limit
  67. * @param bool $modified
  68. *
  69. * @return \Magento\Catalog\Model\ResourceModel\Product\Collection|bool
  70. */
  71. public function getProductsToExportByStore($store, $limit, $modified = false)
  72. {
  73. $connectorCollection = $this;
  74. //for modified catalog
  75. if ($modified) {
  76. $connectorCollection->addFieldToFilter(
  77. 'modified',
  78. ['eq' => '1']
  79. );
  80. } else {
  81. $connectorCollection->addFieldToFilter(
  82. 'imported',
  83. ['null' => 'true']
  84. );
  85. }
  86. //check number of products
  87. if ($connectorCollection->getSize()) {
  88. $productIds = $connectorCollection->getColumnValues('product_id');
  89. $productCollection = $this->productCollection->create()
  90. ->addAttributeToSelect('*')
  91. ->addStoreFilter($store)
  92. ->addAttributeToFilter(
  93. 'entity_id',
  94. ['in' => $productIds]
  95. );
  96. //visibility filter
  97. if ($visibility = $this->helper->getWebsiteConfig(
  98. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_CATALOG_VISIBILITY
  99. )
  100. ) {
  101. $visibility = explode(',', $visibility);
  102. //remove the default option from values
  103. $visibility = array_filter($visibility);
  104. $productCollection->addAttributeToFilter(
  105. 'visibility',
  106. ['in' => $visibility]
  107. );
  108. }
  109. //type filter
  110. if ($type = $this->helper->getWebsiteConfig(
  111. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_CATALOG_TYPE
  112. )
  113. ) {
  114. $type = explode(',', $type);
  115. $productCollection->addAttributeToFilter(
  116. 'type_id',
  117. ['in' => $type]
  118. );
  119. }
  120. $productCollection->addWebsiteNamesToResult()
  121. ->addCategoryIds()
  122. ->addOptionsToResult();
  123. //clear the loaded data and set the limit
  124. $productCollection->clear();
  125. //set the limit of the join collection
  126. $productCollection->getSelect()->limit($limit);
  127. return $productCollection;
  128. }
  129. return false;
  130. }
  131. }