Skip to content

Commit

Permalink
Merge pull request #173 from paynl/feature/PLUG-3273
Browse files Browse the repository at this point in the history
PLUG-3273 - Add custom fields to checkout
  • Loading branch information
woutse authored Jun 27, 2024
2 parents ebc7fed + 2522fa7 commit 7606161
Show file tree
Hide file tree
Showing 31 changed files with 556 additions and 62 deletions.
38 changes: 38 additions & 0 deletions Model/Config/Source/ShowCustomFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Paynl\Payment\Model\Config\Source;

use Magento\Framework\Option\ArrayInterface;

class ShowCustomFields implements ArrayInterface
{
/**
* Options getter
*
* @return array
*/
public function toOptionArray()
{
$arrOptions = $this->toArray();

$arrResult = [];
foreach ($arrOptions as $value => $label) {
$arrResult[] = ['value' => $value, 'label' => $label];
}
return $arrResult;
}

/**
* Get options in "key-value" format
*
* @return array
*/
public function toArray()
{
return [
'0' => __('No'),
'1' => __('Optional'),
'2' => __('Required'),
];
}
}
7 changes: 4 additions & 3 deletions Model/Config/Source/ShowDobOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public function toOptionArray()
public function toArray()
{
return [
'0' => __('No'),
'1' => __('Optional'),
'2' => __('Required'),
'0' => __('Don\'t show at payment method.'),
'1' => __('Show DOB field at method, but leave it optional.'),
'2' => __('Show DOB field at method, make it required.'),
'3' => __('Don\'t show DOB field at method, but set as required. Use DOB field from checkout instead.'),
];
}
}
7 changes: 4 additions & 3 deletions Model/Config/Source/ShowKvkOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public function toOptionArray()
public function toArray()
{
return [
'0' => __('No'),
'1' => __('Optional'),
'2' => __('Required'),
'0' => __('Don\'t show at payment method.'),
'1' => __('Show COC field at method, but leave it optional.'),
'2' => __('Show COC field at method, make it required.'),
'3' => __('Don\'t show COC field at method, but set as required. Use COC field from checkout instead.'),
];
}
}
7 changes: 4 additions & 3 deletions Model/Config/Source/ShowNonPrivate.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public function toOptionArray()
public function toArray()
{
return [
'0' => __('No'),
'1' => __('Optional for business customers'),
'2' => __('Required for business customers'),
'0' => __('Don\'t show at payment method.'),
'1' => __('Show VAT field at method, but leave it optional.'),
'2' => __('Show VAT field at method, make it required.'),
'3' => __('Don\'t show VAT field at method, but set as required. Use VAT field from checkout instead.'),
];
}
}
137 changes: 137 additions & 0 deletions Plugin/Checkout/LayoutProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace Paynl\Payment\Plugin\Checkout;

class LayoutProcessor
{
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $scopeConfig;

/**
* @var \Magento\Checkout\Model\Session
*/
protected $checkoutSession;

/**
* @var \Magento\Customer\Model\AddressFactory
*/
protected $customerAddressFactory;

/**
* @var \Magento\Framework\Data\Form\FormKey
*/
protected $formKey;

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\CheckoutAgreements\Model\ResourceModel\Agreement\CollectionFactory $agreementCollectionFactory
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Customer\Model\AddressFactory $customerAddressFactory
* @return void
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\CheckoutAgreements\Model\ResourceModel\Agreement\CollectionFactory $agreementCollectionFactory,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Customer\Model\AddressFactory $customerAddressFactory
) {
$this->scopeConfig = $context->getScopeConfig();
$this->checkoutSession = $checkoutSession;
$this->customerAddressFactory = $customerAddressFactory;
}

/**
* @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
* @param array $jsLayout
* @return array
*/
public function afterProcess(\Magento\Checkout\Block\Checkout\LayoutProcessor $subject, array $jsLayout)
{
$showDOB = $this->scopeConfig->getValue('payment/paynl/show_custom_field_dob');
$showKVK = $this->scopeConfig->getValue('payment/paynl/show_custom_field_kvk');
$showVAT = $this->scopeConfig->getValue('payment/paynl/show_custom_field_vat');

// Date of Birth
if ($showDOB > 0) {
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['paynl_dob'] = $this->createCustomField('paynl_dob', 'date', ' Date of birth', '', 501, ($showDOB == 2), 'shippingAddress'); // phpcs:ignore
}

// COC number
if ($showKVK > 0) {
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['paynl_coc_number'] = $this->createCustomField('paynl_coc_number', 'input', 'COC number', '', 502, ($showKVK == 2), 'shippingAddress'); // phpcs:ignore
}

// VAT number
if ($showVAT > 0) {
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['paynl_vat_number'] = $this->createCustomField('paynl_vat_number', 'input', 'VAT number', '', 503, ($showVAT == 2), 'shippingAddress'); // phpcs:ignore
}

// Billing Address
$configuration = $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['payments-list']['children'];
foreach ($configuration as $paymentGroup => $groupConfig) {
if ($showDOB > 0) {
$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['payments-list']['children'][$paymentGroup]['children']['form-fields']['children']['paynl_dob'] = $this->createCustomField('paynl_dob', 'date', ' Date of birth', '', 501, ($showDOB == 2), 'billingAddress'); // phpcs:ignore
}
if ($showKVK > 0) {
$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['payments-list']['children'][$paymentGroup]['children']['form-fields']['children']['paynl_coc_number'] = $this->createCustomField('paynl_coc_number', 'input', 'COC number', '', 502, ($showKVK == 2), 'billingAddress'); // phpcs:ignore
}
if ($showVAT > 0) {
$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['payments-list']['children'][$paymentGroup]['children']['form-fields']['children']['paynl_vat_number'] = $this->createCustomField('paynl_vat_number', 'input', 'VAT number', '', 503, ($showVAT == 2), 'billingAddress'); // phpcs:ignore
}
}

return $jsLayout;
}

/**
* @param string $id
* @param string $type
* @param string $name
* @param string $tooltip
* @param integer $sort_order
* @param boolean $required
* @param string $adressType
* @return array
*/
public function createCustomField($id, $type, $name, $tooltip, $sort_order, $required, $adressType)
{
switch ($type) {
case 'date':
$elementTmpl = 'ui/form/element/date';
break;
default:
$elementTmpl = 'ui/form/element/input';
break;
}

$customField = [
'component' => 'Magento_Ui/js/form/element/abstract',
'config' => [
'customScope' => $adressType . '.custom_attributes',
'template' => 'ui/form/field',
'elementTmpl' => $elementTmpl,
'options' => [],
],
'dataScope' => $adressType . '.custom_attributes.' . $id,
'label' => $name,
'provider' => 'checkoutProvider',
'sortOrder' => $sort_order,
'validation' => [
'required-entry' => $required,
],
'options' => [],
'filterBy' => null,
'customEntry' => null,
'visible' => true,
'value' => '',
];

if (!empty($tooltip)) {
$customField['config']['tooltip']['description'] = $tooltip;
}

return $customField;
}
}
6 changes: 3 additions & 3 deletions etc/adminhtml/paymentmethods/afterpay.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
<field id="active">1</field>
</depends>
<config_path>payment/paynl_payment_afterpay/showkvk</config_path>
<comment><![CDATA[When enabled, an extra field is added to the checkout for customers to enter their COC number.]]></comment>
<tooltip>When Enabled, this will add an extra field to the payment method for the customer to enter in their COC number. You can also choose to make the COC number required for this method by using the 4th option. This will require you to enable the 'Show custom COC field' setting in the 'Settings' tab, which adds the COC field to the checkout itself.</tooltip>
</field>
<field id="showvat" translate="label" type="select" sortOrder="66" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Show VAT field</label>
Expand All @@ -122,7 +122,7 @@
<field id="active">1</field>
</depends>
<config_path>payment/paynl_payment_afterpay/showvat</config_path>
<comment><![CDATA[When enabled, an extra field is added to the checkout for customers to enter their VAT number.]]></comment>
<tooltip>When Enabled, this will add an extra field to the payment method for the customer to enter in their VAT number. You can also choose to make the VAT number required for this method by using the 4th option. This will require you to enable the 'Show custom COC field' setting in the 'Settings' tab, which adds the VAT field to the checkout itself.</tooltip>
</field>
<field id="showdob" translate="label" type="select" sortOrder="67" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Show date of birth field</label>
Expand All @@ -131,7 +131,7 @@
<field id="active">1</field>
</depends>
<config_path>payment/paynl_payment_afterpay/showdob</config_path>
<comment><![CDATA[When enabled, a datepicker is added to the checkout for customers to enter their date of birth.]]></comment>
<tooltip>When Enabled, this will add an datepicker field to the payment method for the customer to enter in their date of birth. You can also choose to make this required for this method by using the 4th option. This will require you to enable the 'Show custom DOB field' setting in the 'Settings' tab, which adds the date of birth field to the checkout itself.</tooltip>
</field>
<field id="disallowedshipping" translate="label" type="multiselect" sortOrder="110" showInDefault="1"
showInWebsite="1" showInStore="1">
Expand Down
6 changes: 3 additions & 3 deletions etc/adminhtml/paymentmethods/afterpay_international.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
<field id="active">1</field>
</depends>
<config_path>payment/paynl_payment_afterpay_international/showkvk</config_path>
<comment><![CDATA[When enabled, an extra field is added to the checkout for customers to enter their COC number.]]></comment>
<tooltip>When Enabled, this will add an extra field to the payment method for the customer to enter in their COC number. You can also choose to make the COC number required for this method by using the 4th option. This will require you to enable the 'Show custom COC field' setting in the 'Settings' tab, which adds the COC field to the checkout itself.</tooltip>
</field>
<field id="showvat" translate="label" type="select" sortOrder="66" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Show VAT field</label>
Expand All @@ -122,7 +122,7 @@
<field id="active">1</field>
</depends>
<config_path>payment/paynl_payment_afterpay_international/showvat</config_path>
<comment><![CDATA[When enabled, an extra field is added to the checkout for customers to enter their VAT number.]]></comment>
<tooltip>When Enabled, this will add an extra field to the payment method for the customer to enter in their VAT number. You can also choose to make the VAT number required for this method by using the 4th option. This will require you to enable the 'Show custom COC field' setting in the 'Settings' tab, which adds the VAT field to the checkout itself.</tooltip>
</field>
<field id="showdob" translate="label" type="select" sortOrder="67" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Show date of birth field</label>
Expand All @@ -131,7 +131,7 @@
<field id="active">1</field>
</depends>
<config_path>payment/paynl_payment_afterpay_international/showdob</config_path>
<comment><![CDATA[When enabled, a datepicker is added to the checkout for customers to enter their date of birth.]]></comment>
<tooltip>When Enabled, this will add an datepicker field to the payment method for the customer to enter in their date of birth. You can also choose to make this required for this method by using the 4th option. This will require you to enable the 'Show custom DOB field' setting in the 'Settings' tab, which adds the date of birth field to the checkout itself.</tooltip>
</field>
<field id="disallowedshipping" translate="label" type="multiselect" sortOrder="110" showInDefault="1"
showInWebsite="1" showInStore="1">
Expand Down
8 changes: 4 additions & 4 deletions etc/adminhtml/paymentmethods/biller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</field>
<field id="showcompanyfield" translate="label" type="select" sortOrder="64" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Show company field</label>
<source_model>Paynl\Payment\Model\Config\Source\ShowKvkOptions</source_model>
<source_model>Paynl\Payment\Model\Config\Source\ShowCustomFields</source_model>
<depends>
<field id="active">1</field>
</depends>
Expand All @@ -25,7 +25,7 @@
<field id="active">1</field>
</depends>
<config_path>payment/paynl_payment_biller/showkvk</config_path>
<comment><![CDATA[When enabled, an extra field is added to the checkout for customers to enter their COC number.]]></comment>
<tooltip>When Enabled, this will add an extra field to the payment method for the customer to enter in their COC number. You can also choose to make the COC number required for this method by using the 4th option. This will require you to enable the 'Show custom COC field' setting in the 'Settings' tab, which adds the COC field to the checkout itself.</tooltip>
</field>
<field id="showvat" translate="label" type="select" sortOrder="66" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Show VAT field</label>
Expand All @@ -34,7 +34,7 @@
<field id="active">1</field>
</depends>
<config_path>payment/paynl_payment_biller/showvat</config_path>
<comment><![CDATA[When enabled, an extra field is added to the checkout for customers to enter their VAT number.]]></comment>
<tooltip>When Enabled, this will add an extra field to the payment method for the customer to enter in their VAT number. You can also choose to make the VAT number required for this method by using the 4th option. This will require you to enable the 'Show custom COC field' setting in the 'Settings' tab, which adds the VAT field to the checkout itself.</tooltip>
</field>
<field id="showdob" translate="label" type="select" sortOrder="67" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Show date of birth field</label>
Expand All @@ -43,7 +43,7 @@
<field id="active">1</field>
</depends>
<config_path>payment/paynl_payment_biller/showdob</config_path>
<comment><![CDATA[When enabled, a datepicker is added to the checkout for customers to enter their date of birth.]]></comment>
<tooltip>When Enabled, this will add an datepicker field to the payment method for the customer to enter in their date of birth. You can also choose to make this required for this method by using the 4th option. This will require you to enable the 'Show custom DOB field' setting in the 'Settings' tab, which adds the date of birth field to the checkout itself.</tooltip>
</field>
<field id="title" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1"
showInStore="1">
Expand Down
4 changes: 2 additions & 2 deletions etc/adminhtml/paymentmethods/billink.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<field id="active">1</field>
</depends>
<config_path>payment/paynl_payment_billink/showkvk</config_path>
<comment><![CDATA[When enabled, an extra field is added to the checkout for customers to enter their COC number.]]></comment>
<tooltip>When Enabled, this will add an extra field to the payment method for the customer to enter in their COC number. You can also choose to make the COC number required for this method by using the 4th option. This will require you to enable the 'Show custom COC field' setting in the 'Settings' tab, which adds the COC field to the checkout itself.</tooltip>
</field>
<field id="showvat" translate="label" type="select" sortOrder="66" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Show VAT field</label>
Expand All @@ -25,7 +25,7 @@
<field id="active">1</field>
</depends>
<config_path>payment/paynl_payment_billink/showvat</config_path>
<comment><![CDATA[When enabled, an extra field is added to the checkout for customers to enter their VAT number.]]></comment>
<tooltip>When Enabled, this will add an extra field to the payment method for the customer to enter in their VAT number. You can also choose to make the VAT number required for this method by using the 4th option. This will require you to enable the 'Show custom COC field' setting in the 'Settings' tab, which adds the VAT field to the checkout itself.</tooltip>
</field>
<field id="showdob" translate="label" type="select" sortOrder="67" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Show date of birth field</label>
Expand Down
Loading

0 comments on commit 7606161

Please sign in to comment.