Skip to content

Commit 6bdbd9d

Browse files
authored
Release/2.2.0 (#26)
* Added base currency values to Orders API response. * Use correct value for billing address name (was previously using shipping address name). * Cast string to float to ensure discount line is formatted correctly when greater than zero but less than one. * Add base currency values to line items in Orders API response. * M2-30 Fix bug in currency code caching, ensure prices are converted to correct currency. * M2-28 Default to trying to use the configurable product image regardless of UseConfigurableImage config. * M2-34 Fix case on return variable name. * M2-23 Move inventory access to independent service to allow MSI with backwards compatibility, used in ometria.raw_data and Product API V2. * Return boolean for Product API V2 is_in_stock value. * Add "reward_points" value to customer API response for Magento Commerce merchants. * M2-37 Fix issue with pricing for configurables caused by Magento internal caching of price info per store. * M2-37 Fix issue with pricing for configurables caused by Magento internal caching of price info per store. * Remove redundant DI class.
1 parent 354b0ff commit 6bdbd9d

File tree

11 files changed

+759
-362
lines changed

11 files changed

+759
-362
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<?xml version="1.0"?>
2-
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"><module name="Ometria_AbandonedCarts" setup_version="2.1.1"/></config>
3-
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3+
<module name="Ometria_AbandonedCarts" setup_version="2.2.0"/>
4+
</config>

app/code/Ometria/Api/Controller/V1/Customers.php

Lines changed: 151 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,82 @@
11
<?php
22
namespace Ometria\Api\Controller\V1;
33

4+
use Magento\Customer\Api\CustomerMetadataInterface;
5+
use Magento\Customer\Api\CustomerRepositoryInterface;
46
use Magento\Customer\Api\Data\CustomerInterface;
7+
use Magento\Customer\Api\GroupRepositoryInterface;
8+
use Magento\Framework\Api\SearchCriteriaBuilder;
9+
use Magento\Framework\App\Action\Context;
10+
use Magento\Framework\Controller\Result\JsonFactory;
511
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Newsletter\Model\ResourceModel\Subscriber\Collection as SubscriberCollection;
13+
use Ometria\Api\Helper\CustomerData;
614
use Ometria\Api\Helper\Format\V1\Customers as Helper;
7-
use Ometria\Api\Controller\V1\Base;
15+
use Ometria\Api\Helper\Service\Filterable\Service as FilterableService;
16+
use Ometria\Core\Service\Customer\RewardPoints as RewardPointsService;
817

918
class Customers extends Base
1019
{
11-
protected $resultJsonFactory;
12-
protected $repository;
13-
protected $customerMetadataInterface;
14-
protected $genderOptions;
15-
protected $subscriberCollection;
16-
protected $customerIdsOfNewsLetterSubscribers=[];
17-
protected $customerDataHelper;
18-
protected $searchCriteriaBuilder;
19-
protected $groupRepository;
20-
protected $customerGroupNames;
21-
22-
public function __construct(
23-
\Magento\Framework\App\Action\Context $context,
24-
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
25-
\Ometria\Api\Helper\Service\Filterable\Service $apiHelperServiceFilterable,
26-
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
27-
\Magento\Customer\Api\CustomerMetadataInterface $customerMetadataInterface,
28-
\Magento\Newsletter\Model\ResourceModel\Subscriber\Collection $subscriberCollection,
29-
\Ometria\Api\Helper\CustomerData $customerDataHelper,
30-
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
31-
\Magento\Customer\Api\GroupRepositoryInterface $groupRepository
32-
) {
33-
parent::__construct($context);
34-
$this->resultJsonFactory = $resultJsonFactory;
35-
$this->apiHelperServiceFilterable = $apiHelperServiceFilterable;
36-
$this->repository = $customerRepository;
37-
$this->subscriberCollection = $subscriberCollection;
38-
$this->customerMetadataInterface = $customerMetadataInterface;
39-
$this->customerDataHelper = $customerDataHelper;
40-
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
41-
$this->groupRepository = $groupRepository;
42-
43-
$this->genderOptions = $this->customerMetadataInterface
20+
/** @var JsonFactory */
21+
private $resultJsonFactory;
22+
23+
/** @var CustomerRepositoryInterface */
24+
private $repository;
25+
26+
/** @var CustomerMetadataInterface */
27+
private $customerMetadataInterface;
28+
29+
/** @var SubscriberCollection */
30+
private $subscriberCollection;
31+
32+
/** @var CustomerData */
33+
private $customerDataHelper;
34+
35+
/** @var SearchCriteriaBuilder */
36+
private $searchCriteriaBuilder;
37+
38+
/** @var GroupRepositoryInterface */
39+
private $groupRepository;
40+
41+
/** @var RewardPointsService */
42+
private $rewardPointsService;
43+
44+
/** @var array */
45+
private $customerIdsOfNewsLetterSubscribers = [];
46+
47+
/** @var null|array */
48+
private $customerGroupNames;
49+
50+
/** @var array */
51+
private $genderOptions;
52+
53+
public function __construct(
54+
Context $context,
55+
JsonFactory $resultJsonFactory,
56+
FilterableService $apiHelperServiceFilterable,
57+
CustomerRepositoryInterface $customerRepository,
58+
CustomerMetadataInterface $customerMetadataInterface,
59+
SubscriberCollection $subscriberCollection,
60+
CustomerData $customerDataHelper,
61+
SearchCriteriaBuilder $searchCriteriaBuilder,
62+
GroupRepositoryInterface $groupRepository,
63+
RewardPointsService $rewardPointsService
64+
) {
65+
parent::__construct($context);
66+
67+
$this->resultJsonFactory = $resultJsonFactory;
68+
$this->apiHelperServiceFilterable = $apiHelperServiceFilterable;
69+
$this->repository = $customerRepository;
70+
$this->subscriberCollection = $subscriberCollection;
71+
$this->customerMetadataInterface = $customerMetadataInterface;
72+
$this->customerDataHelper = $customerDataHelper;
73+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
74+
$this->groupRepository = $groupRepository;
75+
$this->rewardPointsService = $rewardPointsService;
76+
$this->genderOptions = $this->customerMetadataInterface
4477
->getAttributeMetadata('gender')
4578
->getOptions();
46-
}
79+
}
4780

4881
public function execute()
4982
{
@@ -93,18 +126,18 @@ public function getItemsData($items)
93126

94127
$new = Helper::getBlankArray();
95128

96-
$new["@type"] = "contact";
97-
$new["id"] = array_key_exists('id', $item) ? $item['id'] : '';
98-
$new["email"] = array_key_exists('email', $item) ? $item['email'] : '';
99-
$new["prefix"] = array_key_exists('prefix', $item) ? $item['prefix'] : '';
100-
$new["firstname"] = array_key_exists('firstname', $item) ? $item['firstname'] : '';
101-
$new["middlename"] = array_key_exists('middlename', $item) ? $item['middlename'] : '';
102-
$new["lastname"] = array_key_exists('lastname', $item) ? $item['lastname'] : '';
103-
$new["gender"] = $this->customerDataHelper->getGenderLabel($item);
104-
$new["date_of_birth"] = array_key_exists('dob', $item) ? $item['dob'] : '';
129+
$new["@type"] = "contact";
130+
$new["id"] = array_key_exists('id', $item) ? $item['id'] : '';
131+
$new["email"] = array_key_exists('email', $item) ? $item['email'] : '';
132+
$new["prefix"] = array_key_exists('prefix', $item) ? $item['prefix'] : '';
133+
$new["firstname"] = array_key_exists('firstname', $item) ? $item['firstname'] : '';
134+
$new["middlename"] = array_key_exists('middlename', $item) ? $item['middlename'] : '';
135+
$new["lastname"] = array_key_exists('lastname', $item) ? $item['lastname'] : '';
136+
$new["gender"] = $this->customerDataHelper->getGenderLabel($item);
137+
$new["date_of_birth"] = array_key_exists('dob', $item) ? $item['dob'] : '';
105138
$new["marketing_optin"] = $this->getMarketingOption($item, $subscriberCollection);
106-
$new["country_id"] = $this->customerDataHelper->getCountryId($item);
107-
$new["store_id"] = array_key_exists('store_id', $item) ? $item['store_id'] : null;
139+
$new["country_id"] = $this->customerDataHelper->getCountryId($item);
140+
$new["store_id"] = array_key_exists('store_id', $item) ? $item['store_id'] : null;
108141

109142
if ($this->_request->getParam('raw') != null) {
110143
$new['_raw'] = $item;
@@ -117,44 +150,83 @@ public function getItemsData($items)
117150
return $new;
118151
}, $items);
119152

153+
if ($this->rewardPointsService->isRewardsAvailable()) {
154+
$this->appendRewardPoints($items);
155+
}
156+
120157
return $items;
121158
}
122159

123-
public function getMarketingOption($item, $subscriber_collection)
124-
{
125-
if (!array_key_exists('id', $item)) {
126-
return false;
127-
}
128-
129-
if (!$this->customerIdsOfNewsLetterSubscribers) {
130-
foreach ($subscriber_collection as $subscriber) {
131-
$this->customerIdsOfNewsLetterSubscribers[] = $subscriber->getCustomerId();
132-
}
133-
}
134-
135-
return in_array($item['id'], $this->customerIdsOfNewsLetterSubscribers);
136-
}
137-
138-
public function getSubscriberCollectionFromCustomerIds($customer_ids)
139-
{
140-
return $this->subscriberCollection
141-
->addFieldToFilter('customer_id', ['in' => $customer_ids])
142-
->addFieldToFilter('subscriber_status', \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED);
143-
}
144-
145-
public function getSubscriberCollection($items)
146-
{
147-
$customer_ids = array_map(function($item){
148-
return $item['id'];
149-
}, $items);
150-
151-
return $this->getSubscriberCollectionFromCustomerIds($customer_ids);
152-
}
160+
public function getMarketingOption($item, $subscriber_collection)
161+
{
162+
if (!array_key_exists('id', $item)) {
163+
return false;
164+
}
165+
166+
if (!$this->customerIdsOfNewsLetterSubscribers) {
167+
foreach ($subscriber_collection as $subscriber) {
168+
$this->customerIdsOfNewsLetterSubscribers[] = $subscriber->getCustomerId();
169+
}
170+
}
171+
172+
return in_array($item['id'], $this->customerIdsOfNewsLetterSubscribers);
173+
}
174+
175+
/**
176+
* @param $items
177+
* @return SubscriberCollection
178+
*/
179+
public function getSubscriberCollection($items)
180+
{
181+
$customerIds = $this->getCustomerIds($items);
182+
183+
return $this->subscriberCollection
184+
->addFieldToFilter('customer_id', ['in' => $customerIds])
185+
->addFieldToFilter('subscriber_status', \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED);
186+
}
153187

154188
/**
155-
* @param int $id
156-
* @return string|null
157-
* @throws \Magento\Framework\Exception\LocalizedException
189+
* Add reward points to items
190+
*
191+
* @param $items
192+
*/
193+
private function appendRewardPoints(&$items)
194+
{
195+
$customerIds = $this->getCustomerIds($items);
196+
197+
$rewardPointsCollection = $this->rewardPointsService->getRewardPointsCollection($customerIds)
198+
->addFieldToFilter('customer_id', ['in' => $customerIds]);
199+
200+
$websiteIds = $this->_request->getParam('website_ids');
201+
if ($websiteIds) {
202+
$rewardPointsCollection->addWebsiteFilter($websiteIds);
203+
}
204+
205+
foreach ($items as &$item) {
206+
$reward = $rewardPointsCollection->getItemByColumnValue('customer_id', $item['id']);
207+
if ($reward) {
208+
$item['reward_points'] = $reward->getPointsBalance();
209+
}
210+
}
211+
}
212+
213+
/**
214+
* @param $items
215+
* @return array
216+
*/
217+
private function getCustomerIds($items)
218+
{
219+
$customerIds = array_map(function($item){
220+
return $item['id'];
221+
}, $items);
222+
223+
return $customerIds;
224+
}
225+
226+
/**
227+
* @param $id
228+
* @return mixed|string|null
229+
* @throws LocalizedException
158230
*/
159231
protected function getCustomerGroupName($id)
160232
{

0 commit comments

Comments
 (0)