Skip to content

Commit 13a69c6

Browse files
committed
Merge branch 'ACP2E-3425' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-11-14-2024
2 parents d4406f5 + f85bed6 commit 13a69c6

File tree

5 files changed

+126
-22
lines changed

5 files changed

+126
-22
lines changed

app/code/Magento/Sales/Model/AdminOrder/Create.php

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66

77
namespace Magento\Sales\Model\AdminOrder;
@@ -12,6 +12,8 @@
1212
use Magento\Framework\Api\ExtensibleDataObjectConverter;
1313
use Magento\Framework\App\ObjectManager;
1414
use Magento\Framework\Exception\LocalizedException;
15+
use Magento\Payment\Model\Checks\SpecificationFactory;
16+
use Magento\Payment\Model\MethodInterface;
1517
use Magento\Quote\Model\Quote\Address;
1618
use Magento\Quote\Model\Quote\Address\CustomAttributeListInterface;
1719
use Magento\Quote\Model\Quote\Item;
@@ -271,6 +273,26 @@ class Create extends \Magento\Framework\DataObject implements \Magento\Checkout\
271273
*/
272274
private $request;
273275

276+
/**
277+
* @var SpecificationFactory
278+
*/
279+
private $paymentMethodSpecificationFactory;
280+
281+
/**
282+
* List of payment method specifications
283+
*
284+
* array(
285+
* array(
286+
* 'type' => 'zero_total',
287+
* 'sortOrder' => 100,
288+
* 'disabled' => false
289+
* )
290+
* )
291+
*
292+
* @var array
293+
*/
294+
private $paymentMethodSpecifications;
295+
274296
/**
275297
* @param \Magento\Framework\ObjectManagerInterface $objectManager
276298
* @param \Magento\Framework\Event\ManagerInterface $eventManager
@@ -306,6 +328,8 @@ class Create extends \Magento\Framework\DataObject implements \Magento\Checkout\
306328
* @param CustomAttributeListInterface|null $customAttributeList
307329
* @param OrderRepositoryInterface|null $orderRepositoryInterface
308330
* @param HttpRequest|null $request
331+
* @param SpecificationFactory|null $paymentMethodSpecificationFactory
332+
* @param array $paymentMethodSpecifications
309333
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
310334
*/
311335
public function __construct(
@@ -342,7 +366,9 @@ public function __construct(
342366
StoreManagerInterface $storeManager = null,
343367
CustomAttributeListInterface $customAttributeList = null,
344368
OrderRepositoryInterface $orderRepositoryInterface = null,
345-
HttpRequest $request = null
369+
HttpRequest $request = null,
370+
?SpecificationFactory $paymentMethodSpecificationFactory = null,
371+
array $paymentMethodSpecifications = []
346372
) {
347373
$this->_objectManager = $objectManager;
348374
$this->_eventManager = $eventManager;
@@ -383,6 +409,9 @@ public function __construct(
383409
->get(OrderRepositoryInterface::class);
384410
$this->request = $request ?: ObjectManager::getInstance()
385411
->get(HttpRequest::class);
412+
$this->paymentMethodSpecificationFactory = $paymentMethodSpecificationFactory ?: ObjectManager::getInstance()
413+
->get(SpecificationFactory::class);
414+
$this->paymentMethodSpecifications = $paymentMethodSpecifications;
386415
}
387416

388417
/**
@@ -2185,7 +2214,7 @@ protected function _validate()
21852214
$this->_errors[] = __("The payment method isn't selected. Enter the payment method and try again.");
21862215
} else {
21872216
$method = $this->getQuote()->getPayment()->getMethodInstance();
2188-
if (!$method->isAvailable($this->getQuote())) {
2217+
if (!$this->isPaymentMethodApplicable($method, $this->getQuote())) {
21892218
$this->_errors[] = __('This payment method is not available.');
21902219
} else {
21912220
try {
@@ -2351,4 +2380,49 @@ private function removeCartTransferredItemsAndUpdateQty(int|null|Item $cartItem,
23512380
$this->getSession()->setTransferredItems($removeCartTransferredItems);
23522381
}
23532382
}
2383+
2384+
/**
2385+
* Check if payment method is applicable to quote
2386+
*
2387+
* @param MethodInterface $method
2388+
* @param Quote $quote
2389+
* @return bool
2390+
* @throws LocalizedException
2391+
*/
2392+
private function isPaymentMethodApplicable(MethodInterface $method, Quote $quote): bool
2393+
{
2394+
if (!$method->isAvailable($this->getQuote())) {
2395+
return false;
2396+
}
2397+
2398+
$specifications = $this->getPaymentMethodSpecifications();
2399+
2400+
if (!empty($specifications)) {
2401+
return $this->paymentMethodSpecificationFactory->create($specifications)->isApplicable($method, $quote);
2402+
}
2403+
2404+
return true;
2405+
}
2406+
2407+
/**
2408+
* Get payment method specifications
2409+
*
2410+
* @return array
2411+
*/
2412+
private function getPaymentMethodSpecifications(): array
2413+
{
2414+
$specifications = array_filter(
2415+
$this->paymentMethodSpecifications,
2416+
fn (array $spec) => !isset($spec['disabled']) || $spec['disabled'] === false
2417+
);
2418+
2419+
usort(
2420+
$specifications,
2421+
fn (array $a, array $b) => ($a['sortOrder'] ?? 0) <=> ($b['sortOrder'] ?? 0)
2422+
);
2423+
2424+
$specifications = array_column($specifications, 'type', 'type');
2425+
2426+
return array_values($specifications);
2427+
}
23542428
}

app/code/Magento/Sales/Test/Mftf/Test/AdminCreateOrderWithSimpleProductWithoutPaymentTest.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2024 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88

@@ -51,7 +51,7 @@
5151
<actionGroup ref="AdminCheckoutSelectCheckMoneyOrderBillingMethodActionGroup" stepKey="selectBillingMethod"/>
5252

5353
<actionGroup ref="AdminOrderClickSubmitOrderActionGroup" stepKey="submitOrder"/>
54-
<see selector="{{AdminOrderFormMessagesSection.error}}" userInput="The payment method you requested is not available." stepKey="seeErrorMessage"/>
54+
<see selector="{{AdminOrderFormMessagesSection.error}}" userInput="This payment method is not available." stepKey="seeErrorMessage"/>
5555
<after>
5656
<actionGroup ref="AdminLogoutActionGroup" stepKey="logout"/>
5757
<!-- Enable payment method one of "Check/Money Order" and "Zero Subtotal Checkout" -->

app/code/Magento/Sales/etc/adminhtml/di.xml

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2011 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
@@ -57,4 +57,36 @@
5757
<argument name="collectionFactory" xsi:type="object">\Magento\Catalog\Ui\DataProvider\Product\ProductCollectionFactory</argument>
5858
</arguments>
5959
</type>
60+
<type name="Magento\Sales\Model\AdminOrder\Create">
61+
<arguments>
62+
<!-- Default specifications according to Magento\Sales\Block\Adminhtml\Order\Create\Billing\Method\Form::_canUseMethod -->
63+
<argument name="paymentMethodSpecifications" xsi:type="array">
64+
<item name="internal" xsi:type="array">
65+
<item name="type" xsi:type="const">Magento\Payment\Model\MethodInterface::CHECK_USE_INTERNAL</item>
66+
<item name="sortOrder" xsi:type="number">10</item>
67+
<item name="disabled" xsi:type="boolean">false</item>
68+
</item>
69+
<item name="country" xsi:type="array">
70+
<item name="type" xsi:type="const">Magento\Payment\Model\MethodInterface::CHECK_USE_FOR_COUNTRY</item>
71+
<item name="sortOrder" xsi:type="number">20</item>
72+
<item name="disabled" xsi:type="boolean">false</item>
73+
</item>
74+
<item name="currency" xsi:type="array">
75+
<item name="type" xsi:type="const">Magento\Payment\Model\MethodInterface::CHECK_USE_FOR_CURRENCY</item>
76+
<item name="sortOrder" xsi:type="number">30</item>
77+
<item name="disabled" xsi:type="boolean">false</item>
78+
</item>
79+
<item name="total" xsi:type="array">
80+
<item name="type" xsi:type="const">Magento\Payment\Model\MethodInterface::CHECK_ORDER_TOTAL_MIN_MAX</item>
81+
<item name="sortOrder" xsi:type="number">40</item>
82+
<item name="disabled" xsi:type="boolean">false</item>
83+
</item>
84+
<item name="zero_total" xsi:type="array">
85+
<item name="type" xsi:type="const">Magento\Payment\Model\MethodInterface::CHECK_ZERO_TOTAL</item>
86+
<item name="sortOrder" xsi:type="number">50</item>
87+
<item name="disabled" xsi:type="boolean">false</item>
88+
</item>
89+
</argument>
90+
</arguments>
91+
</type>
6092
</config>

app/code/Magento/Sales/view/adminhtml/templates/order/create/billing/method/form.phtml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66

77
use Magento\Framework\Escaper;
@@ -92,13 +92,5 @@ script;
9292
<?= /* @noEscape */
9393
$secureRenderer->renderTag('script', [], $scriptString, false) ?>
9494
<?php else: ?>
95-
<div class="admin__message-empty"><?= $escaper->escapeHtml(__('No Payment Methods')); ?>
96-
<span class="no-display">
97-
<input id="p_method"
98-
value=""
99-
type="text"
100-
name="payment[method]" class="admin__control-radio"
101-
checked="checked"/>
102-
</span>
103-
</div>
95+
<div class="admin__message-empty"><?= $escaper->escapeHtml(__('No Payment Methods')); ?></div>
10496
<?php endif; ?>

dev/tests/integration/testsuite/Magento/Sales/Model/AdminOrder/CreateTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2012 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Sales\Model\AdminOrder;
77

@@ -781,6 +781,12 @@ private function preparePreconditionsForCreateOrder(
781781
$session->setCustomerId(0);
782782
}
783783

784+
/** Save changes and reload the quote to make sure future changes to the quote trigger collectTotals */
785+
$quoteRepository = $this->objectManager->create(\Magento\Quote\Api\CartRepositoryInterface::class);
786+
$quoteRepository->save($this->model->getQuote());
787+
$quote = $quoteRepository->get($this->model->getQuote()->getId(), [$this->model->getQuote()->getStoreId()]);
788+
$this->model->setQuote($quote);
789+
784790
/** Emulate availability of shipping method (all are disabled by default) */
785791
/** @var $rate Quote\Address\Rate */
786792
$rate = $this->objectManager->create(Quote\Address\Rate::class);

0 commit comments

Comments
 (0)