Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Container.php
Go to the documentation of this file.
1 <?php
7 
10 
20 {
24  protected $_paymentHelper;
25 
30 
34  private $paymentMethodList;
35 
39  private $paymentMethodInstanceFactory;
40 
45  protected $additionalChecks;
46 
54  public function __construct(
55  \Magento\Framework\View\Element\Template\Context $context,
56  \Magento\Payment\Helper\Data $paymentHelper,
57  \Magento\Payment\Model\Checks\SpecificationFactory $methodSpecificationFactory,
58  array $data = [],
59  array $additionalChecks = []
60  ) {
61  $this->_paymentHelper = $paymentHelper;
62  $this->methodSpecificationFactory = $methodSpecificationFactory;
63  $this->additionalChecks = $additionalChecks;
64  parent::__construct($context, $data);
65  }
66 
72  protected function _prepareLayout()
73  {
77  foreach ($this->getMethods() as $method) {
78  $this->setChild(
79  'payment.method.' . $method->getCode(),
80  $this->_paymentHelper->getMethodFormBlock($method, $this->_layout)
81  );
82  }
83 
84  return parent::_prepareLayout();
85  }
86 
93  protected function _canUseMethod($method)
94  {
95  $checks = array_merge(
96  [
97  AbstractMethod::CHECK_USE_FOR_COUNTRY,
98  AbstractMethod::CHECK_USE_FOR_CURRENCY,
99  AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX,
100  AbstractMethod::CHECK_ZERO_TOTAL
101  ],
102  $this->additionalChecks
103  );
104 
105  return $this->methodSpecificationFactory->create($checks)->isApplicable(
106  $method,
107  $this->getQuote()
108  );
109  }
110 
119  protected function _assignMethod($method)
120  {
121  $method->setInfoInstance($this->getQuote()->getPayment());
122  return $this;
123  }
124 
132  public function setMethodFormTemplate($method = '', $template = '')
133  {
134  if (!empty($method) && !empty($template)) {
135  if ($block = $this->getChildBlock('payment.method.' . $method)) {
136  $block->setTemplate($template);
137  }
138  }
139  return $this;
140  }
141 
147  public function getMethods()
148  {
149  $methods = $this->getData('methods');
150  if ($methods === null) {
151  $quote = $this->getQuote();
152  $store = $quote ? $quote->getStoreId() : null;
153  $methods = [];
154  foreach ($this->getPaymentMethodList()->getActiveList($store) as $method) {
155  $methodInstance = $this->getPaymentMethodInstanceFactory()->create($method);
156  if ($methodInstance->isAvailable($quote) && $this->_canUseMethod($methodInstance)) {
157  $this->_assignMethod($methodInstance);
158  $methods[] = $methodInstance;
159  }
160  }
161  $this->setData('methods', $methods);
162  }
163  return $methods;
164  }
165 
171  public function getSelectedMethodCode()
172  {
173  $methods = $this->getMethods();
174  if (!empty($methods)) {
175  reset($methods);
176  return current($methods)->getCode();
177  }
178  return false;
179  }
180 
187  private function getPaymentMethodList()
188  {
189  if ($this->paymentMethodList === null) {
190  $this->paymentMethodList = ObjectManager::getInstance()->get(
191  \Magento\Payment\Api\PaymentMethodListInterface::class
192  );
193  }
194  return $this->paymentMethodList;
195  }
196 
203  private function getPaymentMethodInstanceFactory()
204  {
205  if ($this->paymentMethodInstanceFactory === null) {
206  $this->paymentMethodInstanceFactory = ObjectManager::getInstance()->get(
207  \Magento\Payment\Model\Method\InstanceFactory::class
208  );
209  }
210  return $this->paymentMethodInstanceFactory;
211  }
212 }
setMethodFormTemplate($method='', $template='')
Definition: Container.php:132
getData($key='', $index=null)
Definition: DataObject.php:119
$quote
$block
Definition: block.php:8
$methods
Definition: billing.phtml:71
$method
Definition: info.phtml:13
setData($key, $value=null)
Definition: DataObject.php:72
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Payment\Helper\Data $paymentHelper, \Magento\Payment\Model\Checks\SpecificationFactory $methodSpecificationFactory, array $data=[], array $additionalChecks=[])
Definition: Container.php:54
$template
Definition: export.php:12