Collection.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\ResourceModel\Wishlist;
  3. class Collection extends
  4. \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  5. {
  6. /**
  7. * @var string
  8. */
  9. protected $_idFieldName = 'id';
  10. /**
  11. * Initialize resource collection.
  12. *
  13. * @return null
  14. */
  15. public function _construct()
  16. {
  17. $this->_init(
  18. \Dotdigitalgroup\Email\Model\Wishlist::class,
  19. \Dotdigitalgroup\Email\Model\ResourceModel\Wishlist::class
  20. );
  21. }
  22. /**
  23. * Join the customer email and store id.
  24. * @return \Magento\Framework\DB\Select
  25. */
  26. public function joinLeftCustomer()
  27. {
  28. return $this->getSelect()
  29. ->joinLeft([
  30. 'c' => $this->_resource->getTable('customer_entity')
  31. ], 'c.entity_id = customer_id', ['email', 'store_id']);
  32. }
  33. /**
  34. * Get the collection first item.
  35. *
  36. * @param int $wishListId
  37. *
  38. * @return bool|\Magento\Framework\DataObject
  39. */
  40. public function getWishlistById($wishListId)
  41. {
  42. $collection = $this->addFieldToFilter('wishlist_id', $wishListId)
  43. ->setPageSize(1);
  44. if ($collection->getSize()) {
  45. return $collection->getFirstItem();
  46. }
  47. return false;
  48. }
  49. /**
  50. * @param \Magento\Store\Api\Data\WebsiteInterface $website
  51. * @param int $limit
  52. *
  53. * @return $this
  54. */
  55. public function getWishlistToImportByWebsite(\Magento\Store\Api\Data\WebsiteInterface $website, $limit = 100)
  56. {
  57. $collection = $this->addFieldToFilter('wishlist_imported', ['null' => true])
  58. ->addFieldToFilter(
  59. 'store_id',
  60. ['in' => $website->getStoreIds()]
  61. )
  62. ->addFieldToFilter('item_count', ['gt' => 0]);
  63. $collection->getSelect()->limit($limit);
  64. return $collection;
  65. }
  66. /**
  67. * Get wishlists marked as modified for website.
  68. *
  69. * @param \Magento\Store\Api\Data\WebsiteInterface $website
  70. * @param int $limit
  71. * @return $this
  72. */
  73. public function getModifiedWishlistToImportByWebsite(
  74. \Magento\Store\Api\Data\WebsiteInterface $website,
  75. $limit = 100
  76. ) {
  77. $collection = $this->addFieldToFilter('wishlist_modified', 1)
  78. ->addFieldToFilter(
  79. 'store_id',
  80. ['in' => $website->getStoreIds()]
  81. );
  82. $collection->getSelect()->limit($limit);
  83. return $collection;
  84. }
  85. }