Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Collection.php
Go to the documentation of this file.
1 <?php
7 
12 class Collection extends \Magento\Quote\Model\ResourceModel\Quote\Collection
13 {
17  protected $customerResource;
18 
29  public function __construct(
30  \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
31  \Psr\Log\LoggerInterface $logger,
32  \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
33  \Magento\Framework\Event\ManagerInterface $eventManager,
34  \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot,
35  \Magento\Customer\Model\ResourceModel\Customer $customerResource,
36  \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
37  \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
38  ) {
39  parent::__construct(
40  $entityFactory,
41  $logger,
42  $fetchStrategy,
43  $eventManager,
46  $resource
47  );
48  $this->customerResource = $customerResource;
49  }
50 
58  public function prepareForAbandonedReport($storeIds, $filter = null)
59  {
60  $this->addFieldToFilter(
61  'items_count',
62  ['neq' => '0']
63  )->addFieldToFilter(
64  'main_table.is_active',
65  '1'
66  )->addFieldToFilter(
67  'main_table.customer_id',
68  ['neq' => null]
69  )->addSubtotal(
70  $storeIds,
71  $filter
72  )->setOrder(
73  'updated_at'
74  );
75  if (isset($filter['email']) || isset($filter['customer_name'])) {
76  $this->addCustomerData($filter);
77  }
78  if (is_array($storeIds) && !empty($storeIds)) {
79  $this->addFieldToFilter('store_id', ['in' => $storeIds]);
80  }
81 
82  return $this;
83  }
84 
91  public function addCustomerData($filter = null)
92  {
93  $customersSelect = $this->customerResource->getConnection()->select();
94  $customersSelect->from(
95  ['customer' => $this->customerResource->getTable('customer_entity')],
96  'entity_id'
97  );
98  if (isset($filter['customer_name'])) {
99  $customerName = $this->customerResource->getConnection()
100  ->getConcatSql(['customer.firstname', 'customer.lastname'], ' ');
101  $customersSelect->where($customerName . ' LIKE ?', '%' . $filter['customer_name'] . '%');
102  }
103  if (isset($filter['email'])) {
104  $customersSelect->where('customer.email LIKE ?', '%' . $filter['email'] . '%');
105  }
106  $filteredCustomers = $this->customerResource->getConnection()->fetchCol($customersSelect);
107  $this->getSelect()->where('main_table.customer_id IN (?)', $filteredCustomers);
108  return $this;
109  }
110 
118  public function addSubtotal($storeIds = '', $filter = null)
119  {
120  if (is_array($storeIds)) {
121  $this->getSelect()->columns(
122  ['subtotal' => '(main_table.base_subtotal_with_discount*main_table.base_to_global_rate)']
123  );
124  $this->_joinedFields['subtotal'] =
125  '(main_table.base_subtotal_with_discount*main_table.base_to_global_rate)';
126  } else {
127  $this->getSelect()->columns(['subtotal' => 'main_table.base_subtotal_with_discount']);
128  $this->_joinedFields['subtotal'] = 'main_table.base_subtotal_with_discount';
129  }
130 
131  if ($filter && is_array($filter) && isset($filter['subtotal'])) {
132  if (isset($filter['subtotal']['from'])) {
133  $this->getSelect()->where(
134  $this->_joinedFields['subtotal'] . ' >= ?',
135  $filter['subtotal']['from'],
137  );
138  }
139  if (isset($filter['subtotal']['to'])) {
140  $this->getSelect()->where(
141  $this->_joinedFields['subtotal'] . ' <= ?',
142  $filter['subtotal']['to'],
144  );
145  }
146  }
147 
148  return $this;
149  }
150 
156  public function resolveCustomerNames()
157  {
158  $select = $this->customerResource->getConnection()->select();
159  $customerName = $this->customerResource->getConnection()->getConcatSql(['firstname', 'lastname'], ' ');
160 
161  $select->from(
162  ['customer' => $this->customerResource->getTable('customer_entity')],
163  ['entity_id', 'email']
164  );
165  $select->columns(
166  ['customer_name' => $customerName]
167  );
168  $select->where(
169  'customer.entity_id IN (?)',
170  array_column(
171  $this->getData(),
172  'customer_id'
173  )
174  );
175  $customersData = $this->customerResource->getConnection()->fetchAll($select);
176 
177  foreach ($this->getItems() as $item) {
178  foreach ($customersData as $customerItemData) {
179  if ($item['customer_id'] == $customerItemData['entity_id']) {
180  $item->setData(array_merge($item->getData(), $customerItemData));
181  }
182  }
183  }
184  }
185 }
$resource
Definition: bulk.php:12
const FLOAT_TYPE
Definition: Db.php:70
$logger
__construct(\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot, \Magento\Customer\Model\ResourceModel\Customer $customerResource, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null, \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource=null)
Definition: Collection.php:29
$connection
Definition: bulk.php:13