Wishlist.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\ResourceModel;
  3. use Dotdigitalgroup\Email\Setup\Schema;
  4. class Wishlist extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  5. {
  6. /**
  7. * @var \Magento\Wishlist\Model\WishlistFactory
  8. */
  9. public $wishlist;
  10. /**
  11. * @var \Dotdigitalgroup\Email\Helper\Data
  12. */
  13. public $helper;
  14. /**
  15. * Initialize resource.
  16. *
  17. * @return null
  18. */
  19. public function _construct()
  20. {
  21. $this->_init(Schema::EMAIL_WISHLIST_TABLE, 'id');
  22. }
  23. /**
  24. * Wishlist constructor.
  25. *
  26. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  27. * @param \Magento\Wishlist\Model\WishlistFactory $wishlist
  28. * @param \Dotdigitalgroup\Email\Helper\Data $data
  29. */
  30. public function __construct(
  31. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  32. \Magento\Wishlist\Model\WishlistFactory $wishlist,
  33. \Dotdigitalgroup\Email\Helper\Data $data
  34. ) {
  35. $this->wishlist = $wishlist;
  36. $this->helper = $data;
  37. parent::__construct($context);
  38. }
  39. /**
  40. * Reset the email wishlist for re-import.
  41. *
  42. * @param string|null $from
  43. * @param string|null $to
  44. *
  45. * @return int
  46. *
  47. */
  48. public function resetWishlists($from = null, $to = null)
  49. {
  50. $conn = $this->getConnection();
  51. if ($from && $to) {
  52. $where = [
  53. 'created_at >= ?' => $from . ' 00:00:00',
  54. 'created_at <= ?' => $to . ' 23:59:59',
  55. 'wishlist_imported is ?' => new \Zend_Db_Expr('not null')
  56. ];
  57. } else {
  58. $where = $conn->quoteInto(
  59. 'wishlist_imported is ?',
  60. new \Zend_Db_Expr('not null')
  61. );
  62. }
  63. $num = $conn->update(
  64. $this->getTable(Schema::EMAIL_WISHLIST_TABLE),
  65. [
  66. 'wishlist_imported' => new \Zend_Db_Expr('null'),
  67. 'wishlist_modified' => new \Zend_Db_Expr('null'),
  68. ],
  69. $where
  70. );
  71. return $num;
  72. }
  73. /**
  74. * @param int $customerId
  75. *
  76. * @return bool|\Magento\Framework\DataObject
  77. */
  78. public function getWishlistsForCustomer($customerId)
  79. {
  80. if ($customerId) {
  81. $collection = $this->wishlist->create()
  82. ->getCollection()
  83. ->addFieldToFilter('customer_id', $customerId)
  84. ->setOrder('updated_at', 'DESC')
  85. ->setPageSize(1);
  86. if ($collection->getSize()) {
  87. return $collection->getFirstItem();
  88. }
  89. }
  90. return false;
  91. }
  92. /**
  93. * @param array $ids
  94. *
  95. * @return mixed
  96. */
  97. public function getWishlistByIds($ids)
  98. {
  99. $collection = $this->wishlist->create()
  100. ->getCollection()
  101. ->addFieldToFilter('main_table.wishlist_id', ['in' => $ids])
  102. ->addFieldToFilter('customer_id', ['notnull' => 'true']);
  103. $collection->getSelect()
  104. ->joinLeft(
  105. ['c' => $this->getTable('customer_entity')],
  106. 'c.entity_id = customer_id',
  107. ['email', 'store_id']
  108. );
  109. return $collection;
  110. }
  111. /**
  112. * @param array $ids
  113. * @param string $updatedAt
  114. * @param bool $modified
  115. *
  116. * @return null
  117. */
  118. public function setImported($ids, $updatedAt, $modified = false)
  119. {
  120. try {
  121. $coreResource = $this->getConnection();
  122. $tableName = $this->getTable(Schema::EMAIL_WISHLIST_TABLE);
  123. //mark imported modified wishlists
  124. if ($modified) {
  125. $coreResource->update(
  126. $tableName,
  127. [
  128. 'wishlist_modified' => 'null',
  129. 'updated_at' => $updatedAt,
  130. ],
  131. ["wishlist_id IN (?)" => $ids]
  132. );
  133. } else {
  134. $coreResource->update(
  135. $tableName,
  136. ['wishlist_imported' => 1, 'updated_at' => $updatedAt],
  137. ["wishlist_id IN (?)" => $ids]
  138. );
  139. }
  140. } catch (\Exception $e) {
  141. $this->helper->debug((string)$e, []);
  142. }
  143. }
  144. }