Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InstantPurchase.php
Go to the documentation of this file.
1 <?php
7 
15 
26 {
30  private $customerSession;
31 
35  private $storeManager;
36 
40  private $instantPurchase;
41 
45  private $paymentTokenFormatter;
46 
50  private $customerAddressesFormatter;
51 
55  private $shippingMethodFormatter;
56 
66  public function __construct(
67  Session $customerSession,
68  StoreManagerInterface $storeManager,
69  InstantPurchaseModel $instantPurchase,
70  PaymentTokenFormatter $paymentTokenFormatter,
71  CustomerAddressesFormatter $customerAddressesFormatter,
72  ShippingMethodFormatter $shippingMethodFormatter
73  ) {
74  $this->customerSession = $customerSession;
75  $this->storeManager = $storeManager;
76  $this->instantPurchase = $instantPurchase;
77  $this->paymentTokenFormatter = $paymentTokenFormatter;
78  $this->customerAddressesFormatter = $customerAddressesFormatter;
79  $this->shippingMethodFormatter = $shippingMethodFormatter;
80  }
81 
85  public function getSectionData(): array
86  {
87  if (!$this->customerSession->isLoggedIn()) {
88  return ['available' => false];
89  }
90 
91  $store = $this->storeManager->getStore();
92  $customer = $this->customerSession->getCustomer();
93  $instantPurchaseOption = $this->instantPurchase->getOption($store, $customer);
94  $data = [
95  'available' => $instantPurchaseOption->isAvailable()
96  ];
97  if (!$instantPurchaseOption->isAvailable()) {
98  return $data;
99  }
100 
101  $paymentToken = $instantPurchaseOption->getPaymentToken();
102  $shippingAddress = $instantPurchaseOption->getShippingAddress();
103  $billingAddress = $instantPurchaseOption->getBillingAddress();
104  $shippingMethod = $instantPurchaseOption->getShippingMethod();
105  $data += [
106  'paymentToken' => [
107  'publicHash' => $paymentToken->getPublicHash(),
108  'summary' => $this->paymentTokenFormatter->format($paymentToken),
109  ],
110  'shippingAddress' => [
111  'id' => $shippingAddress->getId(),
112  'summary' => $this->customerAddressesFormatter->format($shippingAddress),
113  ],
114  'billingAddress' => [
115  'id' => $billingAddress->getId(),
116  'summary' => $this->customerAddressesFormatter->format($billingAddress),
117  ],
118  'shippingMethod' => [
119  'carrier' => $shippingMethod->getCarrierCode(),
120  'method' => $shippingMethod->getMethodCode(),
121  'summary' => $this->shippingMethodFormatter->format($shippingMethod),
122  ]
123  ];
124 
125  return $data;
126  }
127 }
$billingAddress
Definition: order.php:25
$customer
Definition: customers.php:11
$shippingAddress
Definition: order.php:40
$storeManager
$shippingMethod
Definition: popup.phtml:12
__construct(Session $customerSession, StoreManagerInterface $storeManager, InstantPurchaseModel $instantPurchase, PaymentTokenFormatter $paymentTokenFormatter, CustomerAddressesFormatter $customerAddressesFormatter, ShippingMethodFormatter $shippingMethodFormatter)