Skip to content

Commit

Permalink
Add public method to retrieve active design from customer session (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Aimes authored Mar 2, 2022
1 parent 7705a9a commit 8638356
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 28 deletions.
35 changes: 7 additions & 28 deletions Observer/CheckoutLoadBefore.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

namespace Aimes\CheckoutDesigns\Observer;

use Aimes\CheckoutDesigns\Api\CheckoutDesignInterface;
use Aimes\CheckoutDesigns\Scope\Config;
use Magento\Customer\Model\Session as CustomerSession;
use Aimes\CheckoutDesigns\Scope\Customer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\LocalizedException;
Expand All @@ -19,19 +18,19 @@ class CheckoutLoadBefore implements ObserverInterface
/** @var Config */
private $config;

/** @var CustomerSession */
private $customerSession;
/** @var Customer */
private $customer;

/**
* @param Config $config
* @param CustomerSession $customerSession
* @param Customer $customer
*/
public function __construct(
Config $config,
CustomerSession $customerSession
Customer $customer
) {
$this->config = $config;
$this->customerSession = $customerSession;
$this->customer = $customer;
}

/**
Expand All @@ -45,32 +44,12 @@ public function execute(Observer $observer): void
$route = $observer->getEvent()->getFullActionName();

if ($route === 'checkout_index_index') {
if ($design = $this->getDesign()) {
if ($design = $this->customer->getActiveDesign()) {
$observer->getEvent()
->getLayout()
->getUpdate()
->addHandle($design->getLayoutHandle());
}
}
}

/**
* @return CheckoutDesignInterface|null
* @throws LocalizedException
* @throws NoSuchEntityException
*/
private function getDesign(): ?CheckoutDesignInterface
{
$groupMapping = $this->config->getCustomerGroupMapping();

if ($groupMapping !== null) {
$currentCustomerGroup = $this->customerSession->getCustomerGroupId();

if (isset($groupMapping[$currentCustomerGroup])) {
return $this->config->getDesign($groupMapping[$currentCustomerGroup]);
}
}

return $this->config->getDesign();
}
}
52 changes: 52 additions & 0 deletions Scope/Customer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright © Rob Aimes - https://aimes.dev/
* https://github.com/robaimes
*/

namespace Aimes\CheckoutDesigns\Scope;

use Aimes\CheckoutDesigns\Api\CheckoutDesignInterface;
use Aimes\CheckoutDesigns\Model\Config\Source\CheckoutDesigns;
use Magento\Customer\Model\Session as CustomerSession;

class Customer
{
/** @var Config */
private $config;

/** @var CustomerSession */
private $customerSession;

/**
* @param Config $config
* @param CustomerSession $customerSession
*/
public function __construct(
Config $config,
CustomerSession $customerSession
) {
$this->config = $config;
$this->customerSession = $customerSession;
}

/**
* @return CheckoutDesignInterface|null
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function getActiveDesign(): ?CheckoutDesignInterface
{
$groupMapping = $this->config->getCustomerGroupMapping();

if ($groupMapping !== null) {
$currentCustomerGroup = $this->customerSession->getCustomerGroupId();

if (isset($groupMapping[$currentCustomerGroup])) {
return $this->config->getDesign($groupMapping[$currentCustomerGroup]);
}
}

return $this->config->getDesign();
}
}

0 comments on commit 8638356

Please sign in to comment.