Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On page billing form functionality added on checkout #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 101 additions & 2 deletions Plugin/Checkout/LayoutProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SystemCode\BrazilCustomerAttributes\Plugin\Checkout;

use SystemCode\BrazilCustomerAttributes\Helper\Data as Helper;
use Magento\Checkout\Helper\Data;

/**
* Model to add label each address line
Expand Down Expand Up @@ -41,10 +42,12 @@ class LayoutProcessor
*/
public function __construct(
Helper $helper,
\SystemCode\BrazilCustomerAttributes\Model\Config\Source\Streetprefix $streetprefix
\SystemCode\BrazilCustomerAttributes\Model\Config\Source\Streetprefix $streetprefix,
Data $checkoutDataHelper
) {
$this->helper = $helper;
$this->streetprefix = $streetprefix;
$this->checkoutDataHelper = $checkoutDataHelper;
}

/**
Expand All @@ -60,7 +63,12 @@ public function afterProcess(
$numStreetLines = $this->helper->getConfig("customer/address/street_lines");
$this->setStreetPrefixOptions();
$jsLayout = $this->getShippingFormFields($jsLayout, $numStreetLines);
$jsLayout = $this->getBillingFormFields($jsLayout, $numStreetLines);

if ($this->checkoutDataHelper->isDisplayBillingOnPaymentMethodAvailable()) {
$jsLayout = $this->getBillingFormFields($jsLayout, $numStreetLines);
} else {
$jsLayout = $this->getBillingFormFieldsOnPage($jsLayout, $numStreetLines);
}

return $jsLayout;
}
Expand Down Expand Up @@ -278,4 +286,95 @@ public function getBillingFormFields($jsLayout, $numStreetLines)

return $jsLayout;
}

public function getBillingFormFieldsOnPage($jsLayout, $numStreetLines)
{
if(isset($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']
['children']['payment']['children']['afterMethods']['children']['billing-address-form'])
) {
$paymentFormChildren = $jsLayout['components']['checkout']['children']['steps']['children']
['billing-step']['children']['payment']['children']['afterMethods']['children']['billing-address-form']['children']['form-fields']['children'];

// Street Label
$paymentFormChildren['street']['label'] = '';
$paymentFormChildren['street']['required'] = false;

// Street Line 0
$paymentFormChildren['street']['children'][0]['label'] = __('Address');

// Street Line 1
if($this->helper->getConfig("brazilcustomerattributes/general/line_number")
&& $numStreetLines >= 2) {
$paymentFormChildren['street']['children'][1]['label'] = __('Number');
$paymentFormChildren['street']['children'][1]['validation'] = [
'required-entry' => true,
'min_text_len‌​gth' => 1,
'max_text_length' => 255
];
}

// Street Line 2
if($this->helper->getConfig("brazilcustomerattributes/general/line_neighborhood")
&& $numStreetLines >= 3) {
$paymentFormChildren['street']['children'][2]['label'] = __('Neighborhood');
$paymentFormChildren['street']['children'][2]['validation'] = [
'required-entry' => true,
'min_text_len‌​gth' => 1,
'max_text_length' => 255
];
}

// Street Line 3
if($this->helper->getConfig("brazilcustomerattributes/general/line_complement")
&& $numStreetLines == 4) {
$paymentFormChildren['street']['children'][3]['label'] = __('Complement');
}

// Street Prefix
if($this->helper->getConfig("brazilcustomerattributes/general/prefix_enabled")) {
$paymentFormChildren['street_prefix'] = [
'component' => 'Magento_Ui/js/form/element/select',
'config' => [
'customScope' => 'billingAddress.shared.custom_attributes',
'template' => 'ui/form/field',
'options' => $this->streetprefixoptions,
'id' => 'street-prefix'
],
'dataScope' => 'billingAddress.custom_attributes.street_prefix',
'label' => __('Street Prefix'),
'provider' => 'checkoutProvider',
'visible' => true,
'validation' => [
'required-entry' => true,
],
'sortOrder' => 65,
'id' => 'street-prefix'
];
}

// Company
$paymentFormChildren['company']['sortOrder'] = 118;

// Zipcode
$paymentFormChildren['postcode']['sortOrder'] = 40;
$paymentFormChildren['postcode']['component'] =
'SystemCode_BrazilCustomerAttributes/js/shipping-address/address-renderer/zip-code';
$paymentFormChildren['postcode']['config']['elementTmpl'] =
'SystemCode_BrazilCustomerAttributes/shipping-address/address-renderer/zip-code';

// Telephone
$paymentFormChildren['telephone']['component'] =
'SystemCode_BrazilCustomerAttributes/js/shipping-address/address-renderer/telephone';

// Fax
$paymentFormChildren['fax']['component'] =
'SystemCode_BrazilCustomerAttributes/js/shipping-address/address-renderer/telephone';

$jsLayout['components']['checkout']['children']['steps']['children']
['billing-step']['children']['payment']['children']['afterMethods']['children']['billing-address-form']['children']['form-fields']['children'] = $paymentFormChildren;

}

return $jsLayout;
}
}