Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vijayrami committed May 13, 2022
1 parent 67b05ab commit 37dfd79
Show file tree
Hide file tree
Showing 20 changed files with 1,394 additions and 0 deletions.
271 changes: 271 additions & 0 deletions Plugin/Block/LayoutProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
<?php
namespace Magelearn\ImprovedCheckout\Plugin\Block;

use Magento\Customer\Model\AttributeMetadataDataProvider;
use Magento\Ui\Component\Form\AttributeMapper;
use Magento\Checkout\Block\Checkout\AttributeMerger;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Customer\Model\Options;

class LayoutProcessor
{

/**
*
* @var AttributeMetadataDataProvider
*/
public $attributeMetadataDataProvider;

/**
*
* @var AttributeMapper
*/
public $attributeMapper;

/**
*
* @var AttributeMerger
*/
public $merger;

/**
*
* @var CheckoutSession
*/
public $checkoutSession;

/**
*
* @var null
*/
public $quote = null;

/**
* @var Options
*/
public $options;

/**
* LayoutProcessor constructor.
*
* @param AttributeMetadataDataProvider $attributeMetadataDataProvider
* @param AttributeMapper $attributeMapper
* @param AttributeMerger $merger
* @param CheckoutSession $checkoutSession
* @param Options $options
*/
public function __construct(
AttributeMetadataDataProvider $attributeMetadataDataProvider,
AttributeMapper $attributeMapper,
AttributeMerger $merger,
CheckoutSession $checkoutSession,
Options $options = null
)
{
$this->attributeMetadataDataProvider = $attributeMetadataDataProvider;
$this->attributeMapper = $attributeMapper;
$this->merger = $merger;
$this->checkoutSession = $checkoutSession;
$this->options = $options ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Customer\Model\Options::class);
}
/**
* Get Quote
*
* @return \Magento\Quote\Model\Quote|null
*/
public function getQuote()
{
if (null === $this->quote) {
$this->quote = $this->checkoutSession->getQuote();
}

return $this->quote;
}

/**
*
* @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
* @param array $jsLayout
* @return array
*/
public function aroundProcess(
\Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
\Closure $proceed,
array $jsLayout
)
{
$jsLayoutResult = $proceed($jsLayout);

if ($this->getQuote()->isVirtual()) {
return $jsLayoutResult;
}

$attributesToConvert = [
'prefix' => [$this->options, 'getNamePrefixOptions'],
'suffix' => [$this->options, 'getNameSuffixOptions'],
];

if (isset($jsLayoutResult['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset'])) {

$jsLayoutResult['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']
['street']['children'][0]['placeholder'] = __('Street Address');

$jsLayoutResult['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']
['street']['children'][1]['placeholder'] = __('Street line 2');

$jsLayoutResult['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['billing-address']['children']['form-fields']['children']
['street']['children'][0]['placeholder'] = __('Street Address');

$jsLayoutResult['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['billing-address']['children']['form-fields']['children']
['street']['children'][1]['placeholder'] = __('Street line 2');
}

$elements = $this->getAddressAttributes();
$elements = $this->convertElementsToSelect($elements, $attributesToConvert);

$jsLayoutResult['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']
['children']['billingAddress']['children']['address-fieldset'] = $this->getCustomBillingAddressComponent($elements);

if (isset($jsLayoutResult['components']['checkout']['children']['steps']['children']['billing-step']['children']
['payment']['children']['afterMethods']['children']['billing-address-form'])) {
unset($jsLayoutResult['components']['checkout']['children']['steps']['children']['billing-step']['children']
['payment']['children']['afterMethods']['children']['billing-address-form']);
}

if ($billingAddressForms = $jsLayoutResult['components']['checkout']['children']['steps']['children']
['billing-step']['children']['payment']['children']['payments-list']['children']) {
foreach ($billingAddressForms as $billingAddressFormsKey => $billingAddressForm) {
if ($billingAddressFormsKey != 'before-place-order') {
unset($jsLayoutResult['components']['checkout']['children']['steps']['children']['billing-step']['children']
['payment']['children']['payments-list']['children'][$billingAddressFormsKey]);
}
}
}

return $jsLayoutResult;
}



/**
* Get all visible address attribute
*
* @return array
*/
private function getAddressAttributes()
{
/** @var \Magento\Eav\Api\Data\AttributeInterface[] $attributes */
$attributes = $this->attributeMetadataDataProvider->loadAttributesCollection('customer_address', 'customer_register_address');

$elements = [];
foreach ($attributes as $attribute) {
$code = $attribute->getAttributeCode();
if ($attribute->getIsUserDefined()) {
continue;
}
$elements[$code] = $this->attributeMapper->map($attribute);
if (isset($elements[$code]['label'])) {
$label = $elements[$code]['label'];
$elements[$code]['label'] = __($label);
}
}
return $elements;
}

/**
* Prepare billing address field for shipping step for physical product
*
* @param
* $elements
* @return array
*/
public function getCustomBillingAddressComponent($elements)
{
$providerName = 'checkoutProvider';

$components = [
'component' => 'uiComponent',
'displayArea' => 'additional-fieldsets',
'children' => $this->merger->merge($elements, $providerName, 'billingAddress', [
'country_id' => [
'sortOrder' => 115
],
'region' => [
'visible' => false
],
'region_id' => [
'component' => 'Magento_Ui/js/form/element/region',
'config' => [
'template' => 'ui/form/field',
'elementTmpl' => 'ui/form/element/select',
'customEntry' => 'billingAddress.region'
],
'validation' => [
'required-entry' => true
],
'filterBy' => [
'target' => '${ $.provider }:${ $.parentScope }.country_id',
'field' => 'country_id'
]
],
'postcode' => [
'component' => 'Magento_Ui/js/form/element/post-code',
'validation' => [
'required-entry' => true
]
],
'company' => [
'validation' => [
'min_text_length' => 0
]
],
'fax' => [
'validation' => [
'min_text_length' => 0
]
],
'telephone' => [
'config' => [
'tooltip' => [
'description' => __('For delivery questions.')
]
]
]
])
];

return $components;
}

private function convertElementsToSelect($elements, $attributesToConvert)
{
$codes = array_keys($attributesToConvert);
foreach (array_keys($elements) as $code) {
if (!in_array($code, $codes)) {
continue;
}
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$options = call_user_func($attributesToConvert[$code]);
if (!is_array($options)) {
continue;
}
$elements[$code]['dataType'] = 'checkbox-set';
$elements[$code]['formElement'] = 'checkbox-set';
$elements[$code]['value'] = '0';

foreach ($options as $key => $value) {
$elements[$code]['options'][] = [
'value' => $key,
'label' => $value
];
}
}
return $elements;
}
}
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "magelearn/improved-checkout",
"description": "Move billing address just below shipping address and assign billing address from shipping step in Magento2.",
"type": "magento2-module",
"license": "proprietary",
"authors": [
{
"name": "vijay rami",
"email": "vijaymrami@gmail.com"
}
],
"minimum-stability": "dev",
"require": {},
"autoload": {
"files": [ "registration.php" ],
"psr-4": {
"Magelearn\\ImprovedCheckout\\": ""
}
}
}
9 changes: 9 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Block\Checkout\LayoutProcessor">
<plugin name="magelearn_improved_checkout_layout_processor"
type="Magelearn\ImprovedCheckout\Plugin\Block\LayoutProcessor"
sortOrder="1" />
</type>
</config>
9 changes: 9 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magelearn_ImprovedCheckout" setup_version="1.0.0">
<sequence>
<module name="Magento_Checkout" />
</sequence>
</module>
</config>
7 changes: 7 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Magelearn_ImprovedCheckout',
__DIR__
);
Loading

0 comments on commit 37dfd79

Please sign in to comment.