Skip to content

Instantly share code, notes, and snippets.

@alepane21
Last active January 10, 2018 16:05
Show Gist options
  • Save alepane21/1430b2a173b425d68d9107181a0f7bd9 to your computer and use it in GitHub Desktop.
Save alepane21/1430b2a173b425d68d9107181a0f7bd9 to your computer and use it in GitHub Desktop.
magento 2 better ux with guest checkout disabled
diff --git a/Configuration/settings.csv b/Configuration/settings.csv
index 9417446..a404630 100644
--- a/Configuration/settings.csv
+++ b/Configuration/settings.csv
@@ -189,6 +189,7 @@ Magento\CoreConfigData,default,0,customer/address/dob_show,,opt,,,
Magento\CoreConfigData,default,0,customer/address/gender_show,,opt,,,
Magento\CoreConfigData,default,0,customer/password/minimum_password_length,,5,,,
Magento\CoreConfigData,default,0,customer/password/required_character_classes_number,,1,,,
+Magento\CoreConfigData,default,0,customer/startup/redirect_dashboard,,0,,,
,,,,,,,,
# NEWSLETTER,,,,,,,,
Magento\CoreConfigData,default,0,mageside_subscribeatcheckout/general/checkout_label,,Iscriviti ora alla nostra newsletter,,,
diff --git a/htdocs/app/code/Hevelop/Moodit/Block/Customer/Register.php b/htdocs/app/code/Hevelop/Moodit/Block/Customer/Register.php
new file mode 100644
index 0000000..a9bb45b
--- /dev/null
+++ b/htdocs/app/code/Hevelop/Moodit/Block/Customer/Register.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Hevelop\Moodit\Block\Customer;
+
+/**
+ * Customer register form block
+ *
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
+ */
+class Register extends \Magento\Customer\Block\Form\Register
+{
+ public function getCheckoutParams()
+ {
+ if (!$this->isFromCheckout()) {
+ return [];
+ }
+ $referer = $this->getRequest()->getParam(\Magento\Customer\Model\Url::REFERER_QUERY_PARAM_NAME);
+ if (empty($referer)) {
+ return [];
+ }
+ return [
+ 'referer' => $referer,
+ 'context' => 'checkout'
+ ];
+ }
+
+ public function isFromCheckout() {
+ if ($this->getRequest()->getParam('context') !== 'checkout') {
+ return false;
+ }
+ return true;
+ }
+
+ public function getModuleName()
+ {
+ return 'Magento_Customer';
+ }
+
+ public function getPostActionUrl()
+ {
+ return $this->getUrl('customer/account/createpost', $this->getCheckoutParams());
+ }
+
+ public function getErrorUrl() {
+ return $this->getUrl('customer/account/create', $this->getCheckoutParams());
+ }
+ public function getSuccessUrl() {
+ return $this->getUrl('customer/account/create', $this->getCheckoutParams());
+ }
+}
\ No newline at end of file
diff --git a/htdocs/app/code/Hevelop/Moodit/Helper/Data.php b/htdocs/app/code/Hevelop/Moodit/Helper/Data.php
new file mode 100644
index 0000000..2b346f6
--- /dev/null
+++ b/htdocs/app/code/Hevelop/Moodit/Helper/Data.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace Hevelop\Moodit\Helper;
+
+class Data extends \Magento\Framework\App\Helper\AbstractHelper
+{
+ public function getUrlEncoded($url)
+ {
+ return $this->urlEncoder->encode($url);
+ }
+
+ /**
+ * @param $url
+ * @return mixed
+ */
+ public function getCheckoutParams()
+ {
+ $referer = $this->_getRequest()->getParam(\Magento\Customer\Model\Url::REFERER_QUERY_PARAM_NAME);
+ $context = $this->_getRequest()->getParam('context');
+ if (!empty($referer) && $context == 'checkout') {
+ return [
+ 'referer' => $referer
+ 'context' => $context
+ ];
+ }
+ return [];
+ }
+}
\ No newline at end of file
diff --git a/htdocs/app/code/Hevelop/Moodit/etc/di.xml b/htdocs/app/code/Hevelop/Moodit/etc/di.xml
index b225c63..8a63b29 100644
--- a/htdocs/app/code/Hevelop/Moodit/etc/di.xml
+++ b/htdocs/app/code/Hevelop/Moodit/etc/di.xml
@@ -3,4 +3,5 @@
<preference for="MGS\QuickView\Helper\Data" type="Hevelop\Moodit\Helper\QuickView" />
<preference for="Magento\SendFriend\Model\SendFriend" type="Hevelop\Moodit\Model\SendFriend" />
<preference for="Magento\SendFriend\Helper\Data" type="Hevelop\Moodit\Helper\SendFriend\Data" />
+ <preference for="Magento\Customer\Block\Form\Register" type="Hevelop\Moodit\Block\Customer\Register" />
</config>
diff --git a/htdocs/app/design/frontend/Hevelop/moodit/Magento_Checkout/templates/onepage/link.phtml b/htdocs/app/design/frontend/Hevelop/moodit/Magento_Checkout/templates/onepage/link.phtml
index 2a3f7b9..1058cf3 100644
--- a/htdocs/app/design/frontend/Hevelop/moodit/Magento_Checkout/templates/onepage/link.phtml
+++ b/htdocs/app/design/frontend/Hevelop/moodit/Magento_Checkout/templates/onepage/link.phtml
@@ -9,6 +9,7 @@
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
+$mooditHelper = $objectManager->get('Hevelop\Moodit\Helper\Data');
?>
<?php if ($customerSession->isLoggedIn()): ?>
<?php if ($block->isPossibleOnepageCheckout()): ?>
@@ -24,16 +25,18 @@ $customerSession = $objectManager->get('Magento\Customer\Model\Session');
echo __('Proceed to Checkout') ?></span>
</button>
<?php endif ?>
-<?php endif ?>
-<?php if (!$customerSession->isLoggedIn()): ?>
+<?php else: ?>
<?php if ($block->isPossibleOnepageCheckout()): ?>
<button type="button"
title="<?php /* @escapeNotVerified */
- echo __('Login') ?>"
+ echo __('Proceed to Checkout') ?>"
class="action primary checkout"
- onclick="window.location.href='<?php echo $block->getUrl("customer/account/login"); ?>'">
+ onclick="window.location.href='<?php echo $block->getUrl("customer/account/login", [
+ 'referer' => $mooditHelper->getUrlEncoded($block->getCheckoutUrl()),
+ 'context' => 'checkout'
+ ]); ?>'">
<span><?php /* @escapeNotVerified */
- echo __('Login') ?></span>
+ echo __('Proceed to Checkout') ?></span>
</button>
<?php endif ?>
<?php endif ?>
diff --git a/htdocs/app/design/frontend/Hevelop/moodit/Magento_Customer/templates/newcustomer.phtml b/htdocs/app/design/frontend/Hevelop/moodit/Magento_Customer/templates/newcustomer.phtml
index b638b18..85b8853 100755
--- a/htdocs/app/design/frontend/Hevelop/moodit/Magento_Customer/templates/newcustomer.phtml
+++ b/htdocs/app/design/frontend/Hevelop/moodit/Magento_Customer/templates/newcustomer.phtml
@@ -13,6 +13,10 @@
*
* @var $block \Magento\Customer\Block\Form\Login\Info
*/
+/**
+ * @var $mooditHelper \Hevelop\Moodit\Helper\Data
+ */
+$mooditHelper = $this->helper('Hevelop\Moodit\Helper\Data');
?>
<?php if ($block->getRegistration()->isAllowed()): ?>
<div class="block block-new-customer">
@@ -23,7 +27,7 @@
<p><?php /* @escapeNotVerified */ echo __('By creating an account with our store, you will be able to move through the checkout process faster, store multiple shipping addresses, view and track your orders in your account and more.') ?></p>
<div class="actions-toolbar padding-top10">
<div class="primary">
- <a href="<?php /* @escapeNotVerified */ echo $block->getCreateAccountUrl() ?>" class="action create btn btn-primary"><span><?php /* @escapeNotVerified */ echo __('Create an Account') ?></span></a>
+ <a href="<?php /* @escapeNotVerified */ echo $this->getUrl('customer/account/create', $mooditHelper->getCheckoutParams()) ?>" class="action create btn btn-primary"><span><?php /* @escapeNotVerified */ echo __('Create an Account') ?></span></a>
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment