Skip to content

Instantly share code, notes, and snippets.

@sgabhart22
Last active March 1, 2022 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgabhart22/1e2f314ed5f1bdfe185d5a11b92882f7 to your computer and use it in GitHub Desktop.
Save sgabhart22/1e2f314ed5f1bdfe185d5a11b92882f7 to your computer and use it in GitHub Desktop.
Add an Amazon Sign In button in a custom location in Magento 2 (left of search bar in Luma theme)
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="header-wrapper">
<block class="Sample\Module\Block\HeaderLogin" name="ap.login.top" as="apLoginTop" after="topSearch" template="Sample_Module::login.top.phtml" ifconfig="payment/amazon_payment_v2/active" />
</referenceContainer>
</body>
</page>
<?php
namespace Sample\Module\Block;
class HeaderLogin extends \Magento\Framework\View\Element\Template
{
/**
* @var \Amazon\Pay\Model\AmazonConfig
*/
private $amazonConfig;
/**
* @var \Amazon\Pay\Helper\Session
*/
private $session;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Amazon\Pay\Model\AmazonConfig $amazonConfig
* @param \Amazon\Pay\Helper\Session $session
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Amazon\Pay\Model\AmazonConfig $amazonConfig,
\Amazon\Pay\Helper\Session $session
) {
parent::__construct($context);
$this->amazonConfig = $amazonConfig;
$this->session = $session;
}
/**
* @inheritdoc
*/
protected function _toHtml()
{
if (!$this->shouldRender()) {
return '';
}
return parent::_toHtml();
}
/**
* @return bool
*/
protected function shouldRender()
{
return $this->amazonConfig->isLwaEnabled() && !$this->session->isLoggedIn();
}
}
<?php /** @var \Sample\Module\Block\HeaderLogin $block */?>
<div class="block block-amazon-login">
<div class="block-content">
<div class="amazon-sign-in-button-container" style="float: right">
<div class="amazon-sign-in-button-container__cell">
<div id="AmazonPayButton"
class="login-with-amazon"
data-mage-init='{"amazonPayLoginButton": {}}'>
</div>
</div>
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment