Skip to content

Commit aed8a0b

Browse files
authored
Merge pull request #4783 from magento-chaika/Chaika-PR-2019-09-15-2.2
Chaika-PR-2019-09-15-2.2
2 parents 3a68504 + 4fc5977 commit aed8a0b

File tree

6 files changed

+123
-7
lines changed

6 files changed

+123
-7
lines changed

app/code/Magento/Braintree/Observer/AddPaypalShortcuts.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,27 @@
1515
class AddPaypalShortcuts implements ObserverInterface
1616
{
1717
/**
18-
* Block class
18+
* Alias for mini-cart block.
1919
*/
20-
const PAYPAL_SHORTCUT_BLOCK = \Magento\Braintree\Block\Paypal\Button::class;
20+
private static $paypalMinicartAlias = 'mini_cart';
21+
22+
/**
23+
* Alias for shopping cart page.
24+
*/
25+
private static $paypalShoppingcartAlias = 'shopping_cart';
26+
27+
/**
28+
* @var string[]
29+
*/
30+
private $buttonBlocks;
31+
32+
/**
33+
* @param string[] $buttonBlocks
34+
*/
35+
public function __construct(array $buttonBlocks = [])
36+
{
37+
$this->buttonBlocks = $buttonBlocks;
38+
}
2139

2240
/**
2341
* Add Braintree PayPal shortcut buttons
@@ -35,7 +53,13 @@ public function execute(Observer $observer)
3553
/** @var ShortcutButtons $shortcutButtons */
3654
$shortcutButtons = $observer->getEvent()->getContainer();
3755

38-
$shortcut = $shortcutButtons->getLayout()->createBlock(self::PAYPAL_SHORTCUT_BLOCK);
56+
if ($observer->getData('is_shopping_cart')) {
57+
$shortcut = $shortcutButtons->getLayout()
58+
->createBlock($this->buttonBlocks[self::$paypalShoppingcartAlias]);
59+
} else {
60+
$shortcut = $shortcutButtons->getLayout()
61+
->createBlock($this->buttonBlocks[self::$paypalMinicartAlias]);
62+
}
3963

4064
$shortcutButtons->addShortcut($shortcut);
4165
}

app/code/Magento/Braintree/Test/Unit/Observer/AddPaypalShortcutsTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ class AddPaypalShortcutsTest extends \PHPUnit\Framework\TestCase
2121
{
2222
public function testExecute()
2323
{
24-
$addPaypalShortcuts = new AddPaypalShortcuts();
24+
$addPaypalShortcuts = new AddPaypalShortcuts(
25+
[
26+
'mini_cart' => 'Minicart-block',
27+
'shopping_cart' => 'Shoppingcart-block'
28+
]
29+
);
2530

2631
/** @var Observer|\PHPUnit_Framework_MockObject_MockObject $observerMock */
2732
$observerMock = $this->getMockBuilder(Observer::class)
@@ -60,7 +65,7 @@ public function testExecute()
6065

6166
$layoutMock->expects(self::once())
6267
->method('createBlock')
63-
->with(AddPaypalShortcuts::PAYPAL_SHORTCUT_BLOCK)
68+
->with('Minicart-block')
6469
->willReturn($blockMock);
6570

6671
$shortcutButtonsMock->expects(self::once())

app/code/Magento/Braintree/etc/frontend/di.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,25 @@
5555
<argument name="payment" xsi:type="object">BraintreePayPalFacade</argument>
5656
</arguments>
5757
</type>
58-
58+
59+
<virtualType name="Magento\Braintree\Block\Paypal\ButtonShoppingCartVirtual" type="Magento\Braintree\Block\Paypal\Button">
60+
<arguments>
61+
<argument name="data" xsi:type="array">
62+
<item name="template" xsi:type="string">Magento_Braintree::paypal/button_shopping_cart.phtml</item>
63+
<item name="alias" xsi:type="string">braintree.paypal.mini-cart</item>
64+
<item name="button_id" xsi:type="string">braintree-paypal-mini-cart</item>
65+
</argument>
66+
</arguments>
67+
</virtualType>
68+
<type name="Magento\Braintree\Observer\AddPaypalShortcuts">
69+
<arguments>
70+
<argument name="buttonBlocks" xsi:type="array">
71+
<item name="mini_cart" xsi:type="string">Magento\Braintree\Block\Paypal\Button</item>
72+
<item name="shopping_cart" xsi:type="string">Magento\Braintree\Block\Paypal\ButtonShoppingCartVirtual</item>
73+
</argument>
74+
</arguments>
75+
</type>
76+
5977
<type name="Magento\Braintree\Model\Ui\PayPal\ConfigProvider">
6078
<arguments>
6179
<argument name="resolver" xsi:type="object">Magento\Braintree\Model\LocaleResolver</argument>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/**
8+
* @var \Magento\Braintree\Block\Paypal\Button $block
9+
*/
10+
11+
$id = $block->getContainerId() . random_int(0, PHP_INT_MAX);
12+
13+
$config = [
14+
'Magento_Braintree/js/paypal/button_shopping_cart' => [
15+
'id' => $id,
16+
'clientToken' => $block->getClientToken(),
17+
'displayName' => $block->getMerchantName(),
18+
'actionSuccess' => $block->getActionSuccess(),
19+
'environment' => $block->getEnvironment()
20+
]
21+
];
22+
23+
?>
24+
<div data-mage-init='<?= /* @noEscape */ json_encode($config); ?>'
25+
class="paypal checkout paypal-logo braintree-paypal-logo<?= /* @noEscape */ $block->getContainerId(); ?>-container">
26+
<div data-currency="<?= /* @noEscape */ $block->getCurrency(); ?>"
27+
data-locale="<?= /* @noEscape */ $block->getLocale(); ?>"
28+
data-amount="<?= /* @noEscape */ $block->getAmount(); ?>"
29+
id="<?= /* @noEscape */ $id; ?>"
30+
class="action-braintree-paypal-logo">
31+
</div>
32+
</div>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
define(
6+
[
7+
'Magento_Braintree/js/paypal/button',
8+
'Magento_Checkout/js/model/quote',
9+
'domReady!'
10+
],
11+
function (
12+
Component,
13+
quote
14+
) {
15+
'use strict';
16+
17+
return Component.extend({
18+
19+
/**
20+
* Overrides amount with a value from quote.
21+
*
22+
* @returns {Object}
23+
* @private
24+
*/
25+
getClientConfig: function (data) {
26+
var config = this._super(data);
27+
28+
if (config.amount !== quote.totals()['base_grand_total']) {
29+
config.amount = quote.totals()['base_grand_total'];
30+
}
31+
32+
return config;
33+
}
34+
});
35+
}
36+
);

app/code/Magento/Checkout/Block/QuoteShortcutButtons.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ protected function _beforeToHtml()
4545
'container' => $this,
4646
'is_catalog_product' => $this->_isCatalogProduct,
4747
'or_position' => $this->_orPosition,
48-
'checkout_session' => $this->_checkoutSession
48+
'checkout_session' => $this->_checkoutSession,
49+
'is_shopping_cart' => true
4950
]
5051
);
5152
return $this;

0 commit comments

Comments
 (0)