From 01484be589e29577cba618cb98f37daedc67f3da Mon Sep 17 00:00:00 2001 From: klg Date: Tue, 26 Feb 2019 17:25:56 +0100 Subject: [PATCH 01/25] Started making the fields configurable --- Model/Checkout/LayoutProcessor/Plugin.php | 117 ++++++++++++++++++ Model/Config/Source/Option.php | 20 +++ Observer/AddCustomFieldsToOrder.php | 71 +++++++---- etc/adminhtml/system.xml | 21 ++++ etc/config.xml | 10 ++ etc/di.xml | 3 + view/frontend/layout/checkout_index_index.xml | 71 +---------- 7 files changed, 221 insertions(+), 92 deletions(-) create mode 100644 Model/Checkout/LayoutProcessor/Plugin.php create mode 100644 Model/Config/Source/Option.php create mode 100644 etc/adminhtml/system.xml create mode 100644 etc/config.xml diff --git a/Model/Checkout/LayoutProcessor/Plugin.php b/Model/Checkout/LayoutProcessor/Plugin.php new file mode 100644 index 0000000..ad20c58 --- /dev/null +++ b/Model/Checkout/LayoutProcessor/Plugin.php @@ -0,0 +1,117 @@ + + * @copyright © 2017 Slawomir Bodak + * @license See LICENSE file for license details. + */ + +declare(strict_types=1); + +namespace Bodak\CheckoutCustomForm\Model\Checkout\LayoutProcessor; + +use Bodak\CheckoutCustomForm\Api\Data\CustomFieldsInterface; + +class Plugin +{ + private $fields = [ + [ + 'dataScopeName' => CustomFieldsInterface::CHECKOUT_BUYER_NAME, + 'label' => 'Buyer name', + ], + [ + 'dataScopeName' => CustomFieldsInterface::CHECKOUT_BUYER_EMAIL, + 'label' => 'Buyer email', + 'validation' => [ + 'validate-email' => true, + ], + 'config' => [ + 'tooltip' => [ + 'description' => 'We will send an order confirmation to this email address' + ] + ], + ], + [ + 'dataScopeName' => CustomFieldsInterface::CHECKOUT_PURCHASE_ORDER_NO, + 'label' => 'Purchase order no.', + ], + [ + 'dataScopeName' => CustomFieldsInterface::CHECKOUT_GOODS_MARK, + 'label' => 'Goods mark', + ], + [ + 'dataScopeName' => CustomFieldsInterface::CHECKOUT_COMMENT, + 'label' => 'Comment', + 'config' => [ + 'cols' => 15, + 'rows' => 2 + ] + ], + ]; + + /** + * @var \Magento\Framework\App\Config\ScopeConfigInterface + */ + protected $_scopeConfig; + + /** + * LayoutProcessor constructor. + * + * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig + */ + public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig) + { + $this->_scopeConfig = $scopeConfig; + } + + /** + * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject + * @param array $jsLayout + * + * @see \Magento\Checkout\Block\Checkout\LayoutProcessor::process + * @return array + */ + public function afterProcess( + \Magento\Checkout\Block\Checkout\LayoutProcessor $subject, + array $jsLayout + ) { + $config = explode(',', + $this->_scopeConfig->getValue('bodak/checkout/enabled_fields', + \Magento\Store\Model\ScopeInterface::SCOPE_STORE)); + + foreach ($this->fields as $sortOrder => $field) { + if ( ! in_array($field['dataScopeName'], $config)) { + continue; + } + + $formField = [ + 'component' => 'Magento_Ui/js/form/element/abstract', + 'config' => [ + 'customScope' => 'customCheckoutForm', + 'template' => 'ui/form/field', + 'elementTmpl' => 'ui/form/element/input', + ], + 'provider' => 'checkoutProvider', + 'dataScope' => 'customCheckoutForm.' . $field['dataScopeName'], + 'label' => $field['label'], + 'sortOrder' => $sortOrder + 1, + 'validation' => [], + ]; + + if (isset($field['config'])) { + $formField['config'] = array_merge($formField['config'], $field['config']); + } + + if (isset($field['validation'])) { + $formField['validation'] = array_merge($formField['validation'], $field['validation']); + } + + $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step'] + ['children']['shippingAddress']['children']['before-form']['children']['custom-checkout-form-container'] + ['children']['custom-checkout-form-fieldset']['children'][$field['dataScopeName']] = $formField; + } + + return $jsLayout; + } +} \ No newline at end of file diff --git a/Model/Config/Source/Option.php b/Model/Config/Source/Option.php new file mode 100644 index 0000000..c2265f0 --- /dev/null +++ b/Model/Config/Source/Option.php @@ -0,0 +1,20 @@ + CustomFieldsInterface::CHECKOUT_BUYER_NAME, 'label' => 'Buyer Name'], + ['value' => CustomFieldsInterface::CHECKOUT_BUYER_EMAIL, 'label' => 'Buyer email'], + ['value' => CustomFieldsInterface::CHECKOUT_PURCHASE_ORDER_NO, 'label' => 'Purchase order no.'], + ['value' => CustomFieldsInterface::CHECKOUT_GOODS_MARK, 'label' => 'Goods mark'], + ['value' => CustomFieldsInterface::CHECKOUT_COMMENT, 'label' => 'Comment'], + ]; + } +} \ No newline at end of file diff --git a/Observer/AddCustomFieldsToOrder.php b/Observer/AddCustomFieldsToOrder.php index 5939afa..7a84658 100644 --- a/Observer/AddCustomFieldsToOrder.php +++ b/Observer/AddCustomFieldsToOrder.php @@ -22,6 +22,21 @@ */ class AddCustomFieldsToOrder implements ObserverInterface { + /** + * @var \Magento\Framework\App\Config\ScopeConfigInterface + */ + protected $_scopeConfig; + + /** + * AddCustomFieldsToOrder constructor. + * + * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig + */ + public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig) + { + $this->_scopeConfig = $scopeConfig; + } + /** * Execute observer method. * @@ -29,30 +44,42 @@ class AddCustomFieldsToOrder implements ObserverInterface * * @return void */ - public function execute(Observer $observer) - { + public function execute( + Observer $observer + ) { $order = $observer->getEvent()->getOrder(); $quote = $observer->getEvent()->getQuote(); + $config = explode(',', $this->_scopeConfig->getValue('bodak/checkout/enabled_fields', Magento\Store\Model\ScopeInterface::SCOPE_STORE)); - $order->setData( - CustomFieldsInterface::CHECKOUT_BUYER_NAME, - $quote->getData(CustomFieldsInterface::CHECKOUT_BUYER_NAME) - ); - $order->setData( - CustomFieldsInterface::CHECKOUT_BUYER_EMAIL, - $quote->getData(CustomFieldsInterface::CHECKOUT_BUYER_EMAIL) - ); - $order->setData( - CustomFieldsInterface::CHECKOUT_PURCHASE_ORDER_NO, - $quote->getData(CustomFieldsInterface::CHECKOUT_PURCHASE_ORDER_NO) - ); - $order->setData( - CustomFieldsInterface::CHECKOUT_GOODS_MARK, - $quote->getData(CustomFieldsInterface::CHECKOUT_GOODS_MARK) - ); - $order->setData( - CustomFieldsInterface::CHECKOUT_COMMENT, - $quote->getData(CustomFieldsInterface::CHECKOUT_COMMENT) - ); + if (in_array(CustomFieldsInterface::CHECKOUT_BUYER_NAME, $config)) { + $order->setData( + CustomFieldsInterface::CHECKOUT_BUYER_NAME, + $quote->getData(CustomFieldsInterface::CHECKOUT_BUYER_NAME) + ); + } + if (in_array(CustomFieldsInterface::CHECKOUT_BUYER_EMAIL, $config)) { + $order->setData( + CustomFieldsInterface::CHECKOUT_BUYER_EMAIL, + $quote->getData(CustomFieldsInterface::CHECKOUT_BUYER_EMAIL) + ); + } + if (in_array(CustomFieldsInterface::CHECKOUT_PURCHASE_ORDER_NO, $config)) { + $order->setData( + CustomFieldsInterface::CHECKOUT_PURCHASE_ORDER_NO, + $quote->getData(CustomFieldsInterface::CHECKOUT_PURCHASE_ORDER_NO) + ); + } + if (in_array(CustomFieldsInterface::CHECKOUT_GOODS_MARK, $config)) { + $order->setData( + CustomFieldsInterface::CHECKOUT_GOODS_MARK, + $quote->getData(CustomFieldsInterface::CHECKOUT_GOODS_MARK) + ); + } + if (in_array(CustomFieldsInterface::CHECKOUT_COMMENT, $config)) { + $order->setData( + CustomFieldsInterface::CHECKOUT_COMMENT, + $quote->getData(CustomFieldsInterface::CHECKOUT_COMMENT) + ); + } } } diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml new file mode 100644 index 0000000..cf571a6 --- /dev/null +++ b/etc/adminhtml/system.xml @@ -0,0 +1,21 @@ + + + + + + +
+ separator-top + + sales + + + + + Bodak\CheckoutCustomForm\Model\Config\Source\Option + bodak/checkout/enabled_fields + + +
+
+
diff --git a/etc/config.xml b/etc/config.xml new file mode 100644 index 0000000..40e1548 --- /dev/null +++ b/etc/config.xml @@ -0,0 +1,10 @@ + + + + + + checkout_buyer_name,checkout_buyer_email,checkout_purchase_order_no,checkout_goods_mark,checkout_comment + + + + \ No newline at end of file diff --git a/etc/di.xml b/etc/di.xml index f12ec6a..2c69afe 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -3,4 +3,7 @@ + + + \ No newline at end of file diff --git a/view/frontend/layout/checkout_index_index.xml b/view/frontend/layout/checkout_index_index.xml index c847cf8..d1f3cfd 100644 --- a/view/frontend/layout/checkout_index_index.xml +++ b/view/frontend/layout/checkout_index_index.xml @@ -25,76 +25,7 @@ uiComponent custom-checkout-form-fields - - - Magento_Ui/js/form/element/abstract - - customCheckoutForm - ui/form/field - ui/form/element/input - - checkoutProvider - customCheckoutForm.checkout_buyer_name - Buyer name - 1 - - - Magento_Ui/js/form/element/abstract - - customCheckoutForm - ui/form/field - ui/form/element/email - - We will send an order confirmation to this email address - - - checkoutProvider - customCheckoutForm.checkout_buyer_email - Buyer email - 2 - - true - - - - Magento_Ui/js/form/element/abstract - - customCheckoutForm - ui/form/field - ui/form/element/input - - checkoutProvider - customCheckoutForm.checkout_purchase_order_no - Purchase order no. - 3 - - - Magento_Ui/js/form/element/abstract - - customCheckoutForm - ui/form/field - ui/form/element/input - - checkoutProvider - customCheckoutForm.checkout_goods_mark - Goods mark - 4 - - - Magento_Ui/js/form/element/abstract - - customCheckoutForm - ui/form/field - ui/form/element/textarea - 15 - 2 - - checkoutProvider - customCheckoutForm.checkout_comment - Comment - 5 - - + From 00ac7ab22cb40d2bd692aad1ff680d0df01a3ea8 Mon Sep 17 00:00:00 2001 From: klg Date: Tue, 26 Feb 2019 17:30:09 +0100 Subject: [PATCH 02/25] Fixed comment field being an input instead of a textarea --- Model/Checkout/LayoutProcessor/Plugin.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Model/Checkout/LayoutProcessor/Plugin.php b/Model/Checkout/LayoutProcessor/Plugin.php index ad20c58..51ca356 100644 --- a/Model/Checkout/LayoutProcessor/Plugin.php +++ b/Model/Checkout/LayoutProcessor/Plugin.php @@ -45,7 +45,8 @@ class Plugin 'label' => 'Comment', 'config' => [ 'cols' => 15, - 'rows' => 2 + 'rows' => 2, + 'elementTmpl' => 'ui/form/element/textarea', ] ], ]; From c26212bfb64c73e1d9600a0de6d18784b7e1d53e Mon Sep 17 00:00:00 2001 From: klg Date: Tue, 26 Feb 2019 17:31:49 +0100 Subject: [PATCH 03/25] Added header --- Model/Checkout/LayoutProcessor/Plugin.php | 2 +- Model/Config/Source/Option.php | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Model/Checkout/LayoutProcessor/Plugin.php b/Model/Checkout/LayoutProcessor/Plugin.php index 51ca356..14bb268 100644 --- a/Model/Checkout/LayoutProcessor/Plugin.php +++ b/Model/Checkout/LayoutProcessor/Plugin.php @@ -2,7 +2,7 @@ /** * @package Bodak\CheckoutCustomForm - * @author Slawomir Bodak + * @author Konrad Langenberg * @copyright © 2017 Slawomir Bodak * @license See LICENSE file for license details. */ diff --git a/Model/Config/Source/Option.php b/Model/Config/Source/Option.php index c2265f0..a8d4448 100644 --- a/Model/Config/Source/Option.php +++ b/Model/Config/Source/Option.php @@ -1,5 +1,14 @@ + * @copyright © 2017 Slawomir Bodak + * @license See LICENSE file for license details. + */ + +declare(strict_types=1); + namespace Bodak\CheckoutCustomForm\Model\Config\Source; use \Magento\Framework\Option\ArrayInterface; From cc7e4cfe598959543d106ddb6a73e3cf637abc4d Mon Sep 17 00:00:00 2001 From: klg Date: Tue, 26 Feb 2019 17:34:14 +0100 Subject: [PATCH 04/25] Added strings to translation file --- i18n/en_US.csv | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/i18n/en_US.csv b/i18n/en_US.csv index d1356bd..75b55a1 100644 --- a/i18n/en_US.csv +++ b/i18n/en_US.csv @@ -4,4 +4,6 @@ "Purchase order no.","Purchase order no." "Goods mark","Goods mark" "Comment","Comment" -"* Required Fields","* Required Fields" \ No newline at end of file +"* Required Fields","* Required Fields" +"Checkout Custom Form Configuration","Checkout Custom Form Configuration" +"Enabled Form Fields","Enabled Form Fields" \ No newline at end of file From e9317a88837dbfab164ef2c1452c5c04b324efb3 Mon Sep 17 00:00:00 2001 From: klg Date: Tue, 26 Feb 2019 17:37:14 +0100 Subject: [PATCH 05/25] Updated Readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 1e1764f..6cea86f 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ Add a custom form fields to Magento 2 checkout. The form will appear in the firs The form is available for logged in customers and guests. After an order is placed all data are set in `sales_order` table. Data are still in form after page refreshed, till cart is active. +It is also possible to enable/disable certain fields per store view in the admin backend. + Form data will be set in a `quota` table through independent API request: - `/V1/carts/mine/set-order-custom-fields` (for logged in customer) - `/V1/guest-carts/:cartId/set-order-custom-field` (for guest) From 545af11c64649872175718541684af45743483ae Mon Sep 17 00:00:00 2001 From: Alexander Menk Date: Fri, 1 Mar 2019 13:07:39 +0100 Subject: [PATCH 06/25] Move block to other information so it is not in the middle of shipping address --- Model/Checkout/LayoutProcessor/Plugin.php | 2 +- view/frontend/layout/checkout_index_index.xml | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Model/Checkout/LayoutProcessor/Plugin.php b/Model/Checkout/LayoutProcessor/Plugin.php index 14bb268..c675d63 100644 --- a/Model/Checkout/LayoutProcessor/Plugin.php +++ b/Model/Checkout/LayoutProcessor/Plugin.php @@ -109,7 +109,7 @@ public function afterProcess( } $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step'] - ['children']['shippingAddress']['children']['before-form']['children']['custom-checkout-form-container'] + ['children']['shippingAddress']['children']['shipping-additional']['children']['custom-checkout-form-container'] ['children']['custom-checkout-form-fieldset']['children'][$field['dataScopeName']] = $formField; } diff --git a/view/frontend/layout/checkout_index_index.xml b/view/frontend/layout/checkout_index_index.xml index d1f3cfd..c59a1e9 100644 --- a/view/frontend/layout/checkout_index_index.xml +++ b/view/frontend/layout/checkout_index_index.xml @@ -13,7 +13,9 @@ - + + uiComponent + shippingAdditional Bodak_CheckoutCustomForm/js/view/checkout/custom-checkout-form From 7890bdd7b6b5d77cf9ed570f3ad7bb836ea655f7 Mon Sep 17 00:00:00 2001 From: klg Date: Mon, 4 Mar 2019 12:42:57 +0100 Subject: [PATCH 07/25] Added german translations --- i18n/de_DE.csv | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 i18n/de_DE.csv diff --git a/i18n/de_DE.csv b/i18n/de_DE.csv new file mode 100644 index 0000000..e450f8c --- /dev/null +++ b/i18n/de_DE.csv @@ -0,0 +1,9 @@ +"Other information","Weitere Informationen" +"Buyer name","Käufername" +"Buyer email","Käufer-Emailadresse" +"Purchase order no.","Bestellnummer" +"Goods mark","Warenkennzeichnung" +"Comment","Kommentar" +"* Required Fields","* Pflichtfelder" +"Checkout Custom Form Configuration","Checkout Benutzerdefinierte Formular-Konfiguration" +"Enabled Form Fields","Aktivierte Felder" \ No newline at end of file From 740371f4c7d9d4c23c8f633560ce8d09826b6f9a Mon Sep 17 00:00:00 2001 From: klg Date: Tue, 5 Mar 2019 10:01:18 +0100 Subject: [PATCH 08/25] Fixed fields not showing up in the form --- Model/Checkout/LayoutProcessor/Plugin.php | 2 +- view/frontend/layout/checkout_index_index.xml | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Model/Checkout/LayoutProcessor/Plugin.php b/Model/Checkout/LayoutProcessor/Plugin.php index c675d63..14bb268 100644 --- a/Model/Checkout/LayoutProcessor/Plugin.php +++ b/Model/Checkout/LayoutProcessor/Plugin.php @@ -109,7 +109,7 @@ public function afterProcess( } $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step'] - ['children']['shippingAddress']['children']['shipping-additional']['children']['custom-checkout-form-container'] + ['children']['shippingAddress']['children']['before-form']['children']['custom-checkout-form-container'] ['children']['custom-checkout-form-fieldset']['children'][$field['dataScopeName']] = $formField; } diff --git a/view/frontend/layout/checkout_index_index.xml b/view/frontend/layout/checkout_index_index.xml index c59a1e9..d1f3cfd 100644 --- a/view/frontend/layout/checkout_index_index.xml +++ b/view/frontend/layout/checkout_index_index.xml @@ -13,9 +13,7 @@ - - uiComponent - shippingAdditional + Bodak_CheckoutCustomForm/js/view/checkout/custom-checkout-form From 8dcfb1be7b16c4cf2d0beb1248dd738bb0588195 Mon Sep 17 00:00:00 2001 From: klg Date: Tue, 5 Mar 2019 10:05:39 +0100 Subject: [PATCH 09/25] Made sure translations are used --- Model/Checkout/LayoutProcessor/Plugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Model/Checkout/LayoutProcessor/Plugin.php b/Model/Checkout/LayoutProcessor/Plugin.php index 14bb268..0deefe2 100644 --- a/Model/Checkout/LayoutProcessor/Plugin.php +++ b/Model/Checkout/LayoutProcessor/Plugin.php @@ -95,7 +95,7 @@ public function afterProcess( ], 'provider' => 'checkoutProvider', 'dataScope' => 'customCheckoutForm.' . $field['dataScopeName'], - 'label' => $field['label'], + 'label' => __($field['label']), 'sortOrder' => $sortOrder + 1, 'validation' => [], ]; From c683e5834b212fea63391cd160bb71c14b548645 Mon Sep 17 00:00:00 2001 From: klg Date: Wed, 6 Mar 2019 10:21:11 +0100 Subject: [PATCH 10/25] Added config to disable titles --- Model/Checkout/LayoutProcessor/Plugin.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Model/Checkout/LayoutProcessor/Plugin.php b/Model/Checkout/LayoutProcessor/Plugin.php index 0deefe2..30dfc84 100644 --- a/Model/Checkout/LayoutProcessor/Plugin.php +++ b/Model/Checkout/LayoutProcessor/Plugin.php @@ -47,7 +47,8 @@ class Plugin 'cols' => 15, 'rows' => 2, 'elementTmpl' => 'ui/form/element/textarea', - ] + ], + 'showTitle' => false, ], ]; @@ -85,6 +86,7 @@ public function afterProcess( if ( ! in_array($field['dataScopeName'], $config)) { continue; } + if(!isset($field['showTitle'])) $field['showTitle'] = true; $formField = [ 'component' => 'Magento_Ui/js/form/element/abstract', @@ -95,7 +97,7 @@ public function afterProcess( ], 'provider' => 'checkoutProvider', 'dataScope' => 'customCheckoutForm.' . $field['dataScopeName'], - 'label' => __($field['label']), + 'label' => $field['showTitle'] ? __($field['label']) : '', 'sortOrder' => $sortOrder + 1, 'validation' => [], ]; From 4aa3346f960f415506da53c920bf94855e721097 Mon Sep 17 00:00:00 2001 From: klg Date: Wed, 6 Mar 2019 10:21:19 +0100 Subject: [PATCH 11/25] updated translations --- i18n/de_DE.csv | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/i18n/de_DE.csv b/i18n/de_DE.csv index e450f8c..c59efb6 100644 --- a/i18n/de_DE.csv +++ b/i18n/de_DE.csv @@ -1,4 +1,4 @@ -"Other information","Weitere Informationen" +"Other information","Bestell- / Lieferhinweis" "Buyer name","Käufername" "Buyer email","Käufer-Emailadresse" "Purchase order no.","Bestellnummer" From aeeb0903bc6998574439f367a67e6227d7a21381 Mon Sep 17 00:00:00 2001 From: klg Date: Thu, 7 Mar 2019 09:28:08 +0100 Subject: [PATCH 12/25] Fixed class not found --- Observer/AddCustomFieldsToOrder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Observer/AddCustomFieldsToOrder.php b/Observer/AddCustomFieldsToOrder.php index 7a84658..0c42079 100644 --- a/Observer/AddCustomFieldsToOrder.php +++ b/Observer/AddCustomFieldsToOrder.php @@ -49,7 +49,7 @@ public function execute( ) { $order = $observer->getEvent()->getOrder(); $quote = $observer->getEvent()->getQuote(); - $config = explode(',', $this->_scopeConfig->getValue('bodak/checkout/enabled_fields', Magento\Store\Model\ScopeInterface::SCOPE_STORE)); + $config = explode(',', $this->_scopeConfig->getValue('bodak/checkout/enabled_fields', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)); if (in_array(CustomFieldsInterface::CHECKOUT_BUYER_NAME, $config)) { $order->setData( From b4cbe8d2fec1c5bea40e3e5f145a5cf517a64555 Mon Sep 17 00:00:00 2001 From: klg Date: Thu, 7 Mar 2019 11:13:33 +0100 Subject: [PATCH 13/25] Fixed disabled fields being displayed in order overview and summary --- Api/Data/CustomFieldsInterface.php | 44 +++++++++++ Model/Data/CustomFields.php | 77 +++++++++++++++++++ .../templates/order/view/custom_fields.phtml | 62 ++++++++------- .../templates/order/view/custom_fields.phtml | 39 ++++++---- 4 files changed, 181 insertions(+), 41 deletions(-) diff --git a/Api/Data/CustomFieldsInterface.php b/Api/Data/CustomFieldsInterface.php index 2324c55..30b8fbd 100644 --- a/Api/Data/CustomFieldsInterface.php +++ b/Api/Data/CustomFieldsInterface.php @@ -105,4 +105,48 @@ public function setCheckoutGoodsMark(string $checkoutGoodsMark = null); * @return CustomFieldsInterface */ public function setCheckoutComment(string $comment = null); + + /** + * Checks if a field is enabled + * + * @param string $fieldName + * + * @return bool + */ + public function isFieldEnabled(string $fieldName): bool; + + /** + * Checks if buyer name field is enabled + * + * @return bool + */ + public function isCheckoutBuyerNameEnabled(): bool; + + /** + * Checks if buyer name field is enabled + * + * @return bool + */ + public function isCheckoutBuyerEmailEnabled(): bool; + + /** + * Checks if purchase order field is enabled + * + * @return bool + */ + public function isCheckoutPurchaseOrderNoEnabled(): bool; + + /** + * Checks if goods mark field is enabled + * + * @return bool + */ + public function isCheckoutGoodsMarkEnabled(): bool; + + /** + * Checks if comment field is enabled + * + * @return bool + */ + public function isCheckoutCommentEnabled(): bool; } diff --git a/Model/Data/CustomFields.php b/Model/Data/CustomFields.php index 377a45c..875dd60 100644 --- a/Model/Data/CustomFields.php +++ b/Model/Data/CustomFields.php @@ -21,6 +21,27 @@ */ class CustomFields extends AbstractExtensibleObject implements CustomFieldsInterface { + + /** + * @var null|array + */ + private $enabledFields = null; + + /** + * @var \Magento\Framework\App\Config\ScopeConfigInterface + */ + protected $_scopeConfig; + + /** + * AddCustomFieldsToOrder constructor. + * + * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig + */ + public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig) + { + $this->_scopeConfig = $scopeConfig; + } + /** * Get checkout buyer name * @@ -130,4 +151,60 @@ public function setCheckoutComment(string $comment = null) { return $this->setData(self::CHECKOUT_COMMENT, $comment); } + + /** + * @param string $fieldName + * + * @return bool + */ + public function isFieldEnabled(string $fieldName): bool + { + if($this->enabledFields === null) { + $this->enabledFields = explode(',', $this->_scopeConfig->getValue('bodak/checkout/enabled_fields', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)); + } + + return in_array($fieldName, $this->enabledFields); + } + + /** + * @return bool + */ + public function isCheckoutBuyerNameEnabled(): bool + { + return $this->isFieldEnabled(self::CHECKOUT_BUYER_NAME); + } + + /** + * @return bool + */ + public function isCheckoutBuyerEmailEnabled(): bool + { + return $this->isFieldEnabled(self::CHECKOUT_BUYER_EMAIL); + } + + /** + * @return bool + */ + public function isCheckoutPurchaseOrderNoEnabled(): bool + { + return $this->isFieldEnabled(self::CHECKOUT_PURCHASE_ORDER_NO); + } + + /** + * @return bool + */ + public function isCheckoutGoodsMarkEnabled(): bool + { + return $this->isFieldEnabled(self::CHECKOUT_GOODS_MARK); + } + + /** + * @return bool + */ + public function isCheckoutCommentEnabled(): bool + { + return $this->isFieldEnabled(self::CHECKOUT_COMMENT); + } + + } diff --git a/view/adminhtml/templates/order/view/custom_fields.phtml b/view/adminhtml/templates/order/view/custom_fields.phtml index b5deef3..ad38599 100644 --- a/view/adminhtml/templates/order/view/custom_fields.phtml +++ b/view/adminhtml/templates/order/view/custom_fields.phtml @@ -11,40 +11,50 @@ $customFields = $block->getOrderCustomFields();
-
- -
- escapeHtml($customFields->getCheckoutBuyerName()); ?> + isCheckoutBuyerNameEnabled()): ?> +
+ +
+ escapeHtml($customFields->getCheckoutBuyerName()); ?> +
-
-
- -
- escapeHtml($customFields->getCheckoutBuyerEmail()); ?> + + isCheckoutBuyerEmailEnabled()): ?> +
+ +
+ escapeHtml($customFields->getCheckoutBuyerEmail()); ?> +
-
-
- -
- escapeHtml($customFields->getCheckoutPurchaseOrderNo()); ?> + + isCheckoutPurchaseOrderNoEnabled()): ?> +
+ +
+ escapeHtml($customFields->getCheckoutPurchaseOrderNo()); ?> +
-
-
- -
- escapeHtml($customFields->getCheckoutGoodsMark()); ?> + + isCheckoutGoodsMarkEnabled()): ?> +
+ +
+ escapeHtml($customFields->getCheckoutGoodsMark()); ?> +
-
+
-
-
- -
- escapeHtml($customFields->getCheckoutComment())); ?> + isCheckoutCommentEnabled()): ?> +
+
+ +
+ escapeHtml($customFields->getCheckoutComment())); ?> +
-
+
diff --git a/view/frontend/templates/order/view/custom_fields.phtml b/view/frontend/templates/order/view/custom_fields.phtml index 4305146..3ffbdfb 100644 --- a/view/frontend/templates/order/view/custom_fields.phtml +++ b/view/frontend/templates/order/view/custom_fields.phtml @@ -10,21 +10,30 @@ $customFields = $block->getCustomFields($block->getOrder());
- : - escapeHtml($customFields->getCheckoutBuyerName()); ?>
- : - escapeHtml($customFields->getCheckoutBuyerEmail()); ?>
- : - escapeHtml($customFields->getCheckoutPurchaseOrderNo()); ?>
- : - escapeHtml($customFields->getCheckoutGoodsMark()); ?> -
-
- -
- -
- escapeHtml($customFields->getCheckoutComment())); ?> + isCheckoutBuyerNameEnabled()): ?> + : + escapeHtml($customFields->getCheckoutBuyerName()); ?>
+ + isCheckoutBuyerEmailEnabled()): ?> + : + escapeHtml($customFields->getCheckoutBuyerEmail()); ?>
+ + isCheckoutPurchaseOrderNoEnabled()): ?> + : + escapeHtml($customFields->getCheckoutPurchaseOrderNo()); ?>
+ + isCheckoutGoodsMarkEnabled()): ?> + : + escapeHtml($customFields->getCheckoutGoodsMark()); ?> + + isCheckoutCommentEnabled()): ?> +
+ +
+ escapeHtml($customFields->getCheckoutComment())); ?> +
+
+
From 8cc1b329ce503a03f29b42fa1b16b81d5b3665b7 Mon Sep 17 00:00:00 2001 From: Alexander Menk Date: Wed, 13 Mar 2019 16:12:29 +0100 Subject: [PATCH 14/25] Fix ordering in teh checkout form (it's better below) --- Model/Checkout/LayoutProcessor/Plugin.php | 2 +- view/frontend/layout/checkout_index_index.xml | 25 ++++++++----------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/Model/Checkout/LayoutProcessor/Plugin.php b/Model/Checkout/LayoutProcessor/Plugin.php index 30dfc84..cd75ded 100644 --- a/Model/Checkout/LayoutProcessor/Plugin.php +++ b/Model/Checkout/LayoutProcessor/Plugin.php @@ -111,7 +111,7 @@ public function afterProcess( } $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step'] - ['children']['shippingAddress']['children']['before-form']['children']['custom-checkout-form-container'] + ['children']['shippingAddress']['children']['custom-checkout-form-container'] ['children']['custom-checkout-form-fieldset']['children'][$field['dataScopeName']] = $formField; } diff --git a/view/frontend/layout/checkout_index_index.xml b/view/frontend/layout/checkout_index_index.xml index d1f3cfd..beb5a46 100644 --- a/view/frontend/layout/checkout_index_index.xml +++ b/view/frontend/layout/checkout_index_index.xml @@ -13,21 +13,18 @@ - + + shippingAdditional + Bodak_CheckoutCustomForm/js/view/checkout/custom-checkout-form + checkoutProvider + + Bodak_CheckoutCustomForm/checkout/custom-checkout-form + - - Bodak_CheckoutCustomForm/js/view/checkout/custom-checkout-form - checkoutProvider - - Bodak_CheckoutCustomForm/checkout/custom-checkout-form - - - - uiComponent - custom-checkout-form-fields - - - + + uiComponent + + custom-checkout-form-fields From edcc772c6b6c9e3ab62de1728ccd929848babd4c Mon Sep 17 00:00:00 2001 From: klg Date: Thu, 21 Mar 2019 09:39:02 +0100 Subject: [PATCH 15/25] Revert: Fix ordering in teh checkout form (it's better below) (8cc1b329) --- view/frontend/layout/checkout_index_index.xml | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/view/frontend/layout/checkout_index_index.xml b/view/frontend/layout/checkout_index_index.xml index beb5a46..d1f3cfd 100644 --- a/view/frontend/layout/checkout_index_index.xml +++ b/view/frontend/layout/checkout_index_index.xml @@ -13,18 +13,21 @@ - - shippingAdditional - Bodak_CheckoutCustomForm/js/view/checkout/custom-checkout-form - checkoutProvider - - Bodak_CheckoutCustomForm/checkout/custom-checkout-form - + - - uiComponent - - custom-checkout-form-fields + + Bodak_CheckoutCustomForm/js/view/checkout/custom-checkout-form + checkoutProvider + + Bodak_CheckoutCustomForm/checkout/custom-checkout-form + + + + uiComponent + custom-checkout-form-fields + + + From 0cb51d64554889cd935e6201a5a5e900e445eecd Mon Sep 17 00:00:00 2001 From: klg Date: Thu, 21 Mar 2019 09:39:15 +0100 Subject: [PATCH 16/25] Revert: Fix ordering in teh checkout form (it's better below) --- Model/Checkout/LayoutProcessor/Plugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Model/Checkout/LayoutProcessor/Plugin.php b/Model/Checkout/LayoutProcessor/Plugin.php index cd75ded..30dfc84 100644 --- a/Model/Checkout/LayoutProcessor/Plugin.php +++ b/Model/Checkout/LayoutProcessor/Plugin.php @@ -111,7 +111,7 @@ public function afterProcess( } $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step'] - ['children']['shippingAddress']['children']['custom-checkout-form-container'] + ['children']['shippingAddress']['children']['before-form']['children']['custom-checkout-form-container'] ['children']['custom-checkout-form-fieldset']['children'][$field['dataScopeName']] = $formField; } From 403d4d269303ec81e6ddfbc5061f07c45c6123f2 Mon Sep 17 00:00:00 2001 From: klg Date: Thu, 21 Mar 2019 09:56:46 +0100 Subject: [PATCH 17/25] Reapplied patch --- Model/Checkout/LayoutProcessor/Plugin.php | 2 +- view/frontend/layout/checkout_index_index.xml | 25 ++++++++----------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/Model/Checkout/LayoutProcessor/Plugin.php b/Model/Checkout/LayoutProcessor/Plugin.php index 30dfc84..cd75ded 100644 --- a/Model/Checkout/LayoutProcessor/Plugin.php +++ b/Model/Checkout/LayoutProcessor/Plugin.php @@ -111,7 +111,7 @@ public function afterProcess( } $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step'] - ['children']['shippingAddress']['children']['before-form']['children']['custom-checkout-form-container'] + ['children']['shippingAddress']['children']['custom-checkout-form-container'] ['children']['custom-checkout-form-fieldset']['children'][$field['dataScopeName']] = $formField; } diff --git a/view/frontend/layout/checkout_index_index.xml b/view/frontend/layout/checkout_index_index.xml index d1f3cfd..beb5a46 100644 --- a/view/frontend/layout/checkout_index_index.xml +++ b/view/frontend/layout/checkout_index_index.xml @@ -13,21 +13,18 @@ - + + shippingAdditional + Bodak_CheckoutCustomForm/js/view/checkout/custom-checkout-form + checkoutProvider + + Bodak_CheckoutCustomForm/checkout/custom-checkout-form + - - Bodak_CheckoutCustomForm/js/view/checkout/custom-checkout-form - checkoutProvider - - Bodak_CheckoutCustomForm/checkout/custom-checkout-form - - - - uiComponent - custom-checkout-form-fields - - - + + uiComponent + + custom-checkout-form-fields From 20637d6d3eac2d6d852cab8a56c9682ae6202c78 Mon Sep 17 00:00:00 2001 From: Christoph Fischer Date: Mon, 25 Mar 2019 15:54:50 +0100 Subject: [PATCH 18/25] Add Custom Attributes as Extension Attributes for Sales Order --- Observer/Sales/OrderLoadAfter.php | 47 +++++++++++++++++++++++++++++++ etc/events.xml | 3 ++ etc/extension_attributes.xml | 10 +++++++ 3 files changed, 60 insertions(+) create mode 100644 Observer/Sales/OrderLoadAfter.php create mode 100644 etc/extension_attributes.xml diff --git a/Observer/Sales/OrderLoadAfter.php b/Observer/Sales/OrderLoadAfter.php new file mode 100644 index 0000000..2cb9fdb --- /dev/null +++ b/Observer/Sales/OrderLoadAfter.php @@ -0,0 +1,47 @@ +orderExtension = $orderExtension; + } + + /** + * @param \Magento\Framework\Event\Observer $observer + */ + public function execute(\Magento\Framework\Event\Observer $observer) + { + + $order = $observer->getOrder(); + + $extensionAttributes = $order->getExtensionAttributes(); + + if ($extensionAttributes === null) { + $extensionAttributes = $this->orderExtension; + } + + $extensionAttributes->setCheckoutBuyerName($order->getData(CustomFieldsInterface::CHECKOUT_BUYER_NAME)); + $extensionAttributes->setCheckoutBuyerEmail($order->getData(CustomFieldsInterface::CHECKOUT_BUYER_EMAIL)); + $extensionAttributes->setCheckoutPurchaseOrderNo($order->getData(CustomFieldsInterface::CHECKOUT_PURCHASE_ORDER_NO)); + $extensionAttributes->setCheckoutGoodsMark($order->getData(CustomFieldsInterface::CHECKOUT_GOODS_MARK)); + $extensionAttributes->setCheckoutComment($order->getData(CustomFieldsInterface::CHECKOUT_COMMENT)); + + $order->setExtensionAttributes($extensionAttributes); + } +} diff --git a/etc/events.xml b/etc/events.xml index b6542b3..3c68899 100644 --- a/etc/events.xml +++ b/etc/events.xml @@ -3,4 +3,7 @@ + + + \ No newline at end of file diff --git a/etc/extension_attributes.xml b/etc/extension_attributes.xml new file mode 100644 index 0000000..307f70f --- /dev/null +++ b/etc/extension_attributes.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file From 57a7516f2d271ed58058a070ae1e58d2bf38b359 Mon Sep 17 00:00:00 2001 From: trr Date: Tue, 23 Jul 2019 13:30:34 +0200 Subject: [PATCH 19/25] #45735 - change german translation --- i18n/de_DE.csv | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/i18n/de_DE.csv b/i18n/de_DE.csv index c59efb6..b22122d 100644 --- a/i18n/de_DE.csv +++ b/i18n/de_DE.csv @@ -1,4 +1,4 @@ -"Other information","Bestell- / Lieferhinweis" +"Other information","Kundenreferenz" "Buyer name","Käufername" "Buyer email","Käufer-Emailadresse" "Purchase order no.","Bestellnummer" From f77d4064af8e174e71bed930524a30806a1216d4 Mon Sep 17 00:00:00 2001 From: trr Date: Tue, 23 Jul 2019 16:40:43 +0200 Subject: [PATCH 20/25] #45735 - copy ui element textarea to package --- .../web/template/form/element/textarea.html | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 view/frontend/web/template/form/element/textarea.html diff --git a/view/frontend/web/template/form/element/textarea.html b/view/frontend/web/template/form/element/textarea.html new file mode 100644 index 0000000..119d9f8 --- /dev/null +++ b/view/frontend/web/template/form/element/textarea.html @@ -0,0 +1,21 @@ + + +