Skip to content

Commit 164e57a

Browse files
authored
Merge pull request #112 from magento-commerce/feature/add-to-cart-tracking
Feature/add to cart tracking
2 parents 6ff2ed5 + e07b7da commit 164e57a

File tree

6 files changed

+132
-111
lines changed

6 files changed

+132
-111
lines changed

app/code/Meta/BusinessExtension/Helper/MagentoDataHelper.php

Lines changed: 42 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
namespace Meta\BusinessExtension\Helper;
1919

20+
use Magento\Catalog\Api\Data\ProductInterface;
2021
use Magento\Catalog\Api\ProductRepositoryInterface;
2122
use Magento\Customer\Api\CustomerMetadataInterface;
23+
use Magento\Framework\Exception\LocalizedException;
2224
use Magento\Store\Model\StoreManagerInterface;
2325
use Magento\Checkout\Model\Session as CheckoutSession;
2426
use Magento\Quote\Model\Quote;
@@ -50,57 +52,57 @@ class MagentoDataHelper extends AbstractHelper
5052
/**
5153
* @var StoreManagerInterface
5254
*/
53-
private $storeManager;
55+
private StoreManagerInterface $storeManager;
5456

5557
/**
5658
* @var CustomerMetadataInterface
5759
*/
58-
private $customerMetadata;
60+
private CustomerMetadataInterface $customerMetadata;
5961

6062
/**
6163
* @var ProductRepositoryInterface
6264
*/
63-
private $productRepository;
65+
private ProductRepositoryInterface $productRepository;
6466

6567
/**
6668
* @var ProductIdentifier
6769
*/
68-
private $productIdentifier;
70+
private ProductIdentifier $productIdentifier;
6971

7072
/**
7173
* @var CheckoutSession
7274
*/
73-
private $checkoutSession;
75+
private CheckoutSession $checkoutSession;
7476

7577
/**
76-
* @var Quote
78+
* @var Quote|null
7779
*/
7880
private $quote;
7981

8082
/**
8183
* @var CustomerSession
8284
*/
83-
private $customerSession;
85+
private CustomerSession $customerSession;
8486

8587
/**
8688
* @var CategoryRepositoryInterface
8789
*/
88-
private $categoryRepository;
90+
private CategoryRepositoryInterface $categoryRepository;
8991

9092
/**
9193
* @var PricingHelper
9294
*/
93-
private $pricingHelper;
95+
private PricingHelper $pricingHelper;
9496

9597
/**
9698
* @var AddressFactory
9799
*/
98-
private $addressFactory;
100+
private AddressFactory $addressFactory;
99101

100102
/**
101103
* @var RegionFactory
102104
*/
103-
private $regionFactory;
105+
private RegionFactory $regionFactory;
104106

105107
/**
106108
* MagentoDataHelper constructor
@@ -174,25 +176,30 @@ public function getLastName(): string
174176
}
175177

176178
/**
177-
* Return currently logged in users' Date of Birth.
179+
* Return the product by the given sku
178180
*
179-
* @return string
181+
* @param string $productSku
182+
* @return ProductInterface | bool
180183
*/
181-
public function getDateOfBirth(): string
184+
public function getProductBySku(string $productSku)
182185
{
183-
return $this->customerSession->getCustomer()->getDob();
186+
try {
187+
return $this->productRepository->get($productSku);
188+
} catch (NoSuchEntityException $e) {
189+
return false;
190+
}
184191
}
185192

186193
/**
187-
* Return the product by the given sku
194+
* Get Product by ID
188195
*
189-
* @param string $productSku
190-
* @return \Magento\Catalog\Api\Data\ProductInterface | bool
196+
* @param mixed $productId
197+
* @return false|ProductInterface
191198
*/
192-
public function getProductBySku($productSku)
199+
public function getProductById($productId)
193200
{
194201
try {
195-
return $this->productRepository->get($productSku);
202+
return $this->productRepository->getById($productId);
196203
} catch (NoSuchEntityException $e) {
197204
return false;
198205
}
@@ -201,7 +208,7 @@ public function getProductBySku($productSku)
201208
/**
202209
* Return the categories for the given product
203210
*
204-
* @param \Magento\Catalog\Model\Product $product
211+
* @param Product $product
205212
* @return string
206213
*/
207214
public function getCategoriesForProduct($product): string
@@ -235,7 +242,7 @@ public function getContentType(Product $product): string
235242
}
236243

237244
/**
238-
* Get content id
245+
* Get Content IDs (Product IDs)
239246
*
240247
* @param Product $product
241248
* @return bool|int|string
@@ -248,10 +255,10 @@ public function getContentId(Product $product)
248255
/**
249256
* Return the price for the given product
250257
*
251-
* @param \Magento\Catalog\Model\Product $product
258+
* @param Product $product
252259
* @return float
253260
*/
254-
public function getValueForProduct($product): float
261+
public function getValueForProduct(Product $product): float
255262
{
256263
$price = $product->getFinalPrice();
257264
return $this->pricingHelper->currency($price, false, false);
@@ -261,7 +268,8 @@ public function getValueForProduct($product): float
261268
* Return the currency used in the store
262269
*
263270
* @return string
264-
* @throws \Magento\Framework\Exception\NoSuchEntityException
271+
* @throws NoSuchEntityException
272+
* @throws LocalizedException
265273
*/
266274
public function getCurrency(): string
267275
{
@@ -376,7 +384,7 @@ public function getOrderContentIds(): array
376384
*
377385
* @return float|null
378386
*/
379-
public function getOrderTotal()
387+
public function getOrderTotal(): ?float
380388
{
381389
$order = $this->checkoutSession->getLastRealOrder();
382390
if (!$order) {
@@ -452,7 +460,7 @@ public function getCurrentCustomer(): ?Customer
452460
/**
453461
* Return the address of a given customer
454462
*
455-
* @param object $customer
463+
* @param mixed $customer
456464
* @return Address
457465
*/
458466
public function getCustomerAddress($customer): Address
@@ -461,42 +469,12 @@ public function getCustomerAddress($customer): Address
461469
return $this->addressFactory->create()->load($customerAddressId);
462470
}
463471

464-
/**
465-
* Return the region's code for the given address
466-
*
467-
* @param object $address
468-
* @return string|null
469-
*/
470-
public function getRegionCodeForAddress($address): ?string
471-
{
472-
$region = $this->regionFactory->create()->load($address->getRegionId());
473-
if ($region) {
474-
return $region->getCode();
475-
} else {
476-
return null;
477-
}
478-
}
479-
480-
/**
481-
* Return the string representation of the customer gender
482-
*
483-
* @param object $customer
484-
* @return string|null
485-
*/
486-
public function getGenderAsString($customer): ?string
487-
{
488-
if ($customer->getGender()) {
489-
return $customer->getResource()->getAttribute('gender')->getSource()->getOptionText($customer->getGender());
490-
}
491-
return null;
492-
}
493-
494472
/**
495473
* Return all of the match keys that can be extracted from order information
496474
*
497475
* @return array
498-
* @throws \Magento\Framework\Exception\LocalizedException
499-
* @throws \Magento\Framework\Exception\NoSuchEntityException
476+
* @throws LocalizedException
477+
* @throws NoSuchEntityException
500478
*/
501479
public function getUserDataFromOrder(): array
502480
{
@@ -537,8 +515,8 @@ public function getUserDataFromOrder(): array
537515
* Return all of the match keys that can be extracted from user session
538516
*
539517
* @return array
540-
* @throws \Magento\Framework\Exception\LocalizedException
541-
* @throws \Magento\Framework\Exception\NoSuchEntityException
518+
* @throws LocalizedException
519+
* @throws NoSuchEntityException
542520
*/
543521
public function getUserDataFromSession(): array
544522
{
@@ -576,7 +554,7 @@ public function getUserDataFromSession(): array
576554
}
577555

578556
/**
579-
* Hash value
557+
* Get Hash value
580558
*
581559
* @param string $string
582560
* @return string
@@ -590,6 +568,8 @@ private function hashValue($string): string
590568
* Get active quote
591569
*
592570
* @return Quote
571+
* @throws LocalizedException
572+
* @throws NoSuchEntityException
593573
*/
594574
public function getQuote(): Quote
595575
{

0 commit comments

Comments
 (0)