-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUpdate.php
More file actions
74 lines (64 loc) · 2.13 KB
/
Copy pathUpdate.php
File metadata and controls
74 lines (64 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
* Copyright © Klarna Bank AB (publ)
*
* For the full copyright and license information, please view the NOTICE
* and LICENSE files that were distributed with this source code.
*/
declare(strict_types=1);
namespace Klarna\Kco\Model\Checkout\Company;
use Magento\Framework\DataObject;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Quote\Api\Data\CartInterface;
/**
* @internal
*/
class Update
{
/**
* @var DataProvider
*/
private DataProvider $dataProvider;
/**
* @var CustomerRepositoryInterface
*/
private CustomerRepositoryInterface $customerRepository;
/**
* @param DataProvider $dataProvider
* @param CustomerRepositoryInterface $customerRepository
* @codeCoverageIgnore
*/
public function __construct(DataProvider $dataProvider, CustomerRepositoryInterface $customerRepository)
{
$this->dataProvider = $dataProvider;
$this->customerRepository = $customerRepository;
}
/**
* Setting company ID into quote and customer session
*
* @param DataObject $request
* @param CartInterface $quote
*/
public function updateCustomerBusinessIdFromRequest(DataObject $request, CartInterface $quote): void
{
if ($quote->getCustomerIsGuest()) {
return;
}
$klarnaCompanyId = $this->dataProvider->getKlarnaRequestCompanyId($request);
if ($klarnaCompanyId === '') {
return;
}
$storeCompanyIdAttributeCode = $this->dataProvider->getStoreCompanyIdAttributeCode($quote->getStore());
if ($storeCompanyIdAttributeCode === '0') {
return;
}
$customerId = $quote->getCustomer()->getId();
$customer = $this->customerRepository->getById($customerId);
$customerCompanyId = $customer->getCustomAttribute($storeCompanyIdAttributeCode)->getValue();
if (!empty($customerCompanyId) && $customerCompanyId === $klarnaCompanyId) {
return;
}
$customer->setCustomAttribute($storeCompanyIdAttributeCode, $klarnaCompanyId);
$this->customerRepository->save($customer);
}
}