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

Support magento2.4 #36

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
01484be
Started making the fields configurable
Feb 26, 2019
00ac7ab
Fixed comment field being an input instead of a textarea
Feb 26, 2019
c26212b
Added header
Feb 26, 2019
cc7e4cf
Added strings to translation file
Feb 26, 2019
e9317a8
Updated Readme
Feb 26, 2019
545af11
Move block to other information
amenk Mar 1, 2019
7890bdd
Added german translations
Mar 4, 2019
740371f
Fixed fields not showing up in the form
Mar 5, 2019
8dcfb1b
Made sure translations are used
Mar 5, 2019
c683e58
Added config to disable titles
Mar 6, 2019
4aa3346
updated translations
Mar 6, 2019
aeeb090
Fixed class not found
Mar 7, 2019
b4cbe8d
Fixed disabled fields being displayed in order overview and summary
Mar 7, 2019
8cc1b32
Fix ordering in teh checkout form (it's better below)
amenk Mar 13, 2019
edcc772
Revert: Fix ordering in teh checkout form (it's better below) (8cc1b329)
Mar 21, 2019
0cb51d6
Revert: Fix ordering in teh checkout form (it's better below)
Mar 21, 2019
403d4d2
Reapplied patch
Mar 21, 2019
20637d6
Add Custom Attributes as Extension Attributes for Sales Order
Toffelcore Mar 25, 2019
57a7516
#45735 - change german translation
UB3RL33T Jul 23, 2019
f77d406
#45735 - copy ui element textarea to package
UB3RL33T Jul 23, 2019
d6ac82e
#45735 - add maxlength to area element
UB3RL33T Jul 23, 2019
d1ab3f6
Merge pull request #1 from iMi-digital/develop-trr
nachtsheim Jul 24, 2019
de3cd81
Add configurable input length validation
DanieliMi Jan 24, 2020
b2f9219
Use zend class instead of implementing functionality again
DanieliMi Jan 27, 2020
8651d7a
Remove wrapper function
DanieliMi Jan 27, 2020
f8eda12
Merge pull request #2 from iMi-digital/feature/configurable-input-len…
amenk Jan 27, 2020
0b19f31
Replace php requirement with module requirements
DanieliMi Sep 13, 2021
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
52 changes: 52 additions & 0 deletions Api/Data/CustomFieldsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ interface CustomFieldsInterface
const CHECKOUT_GOODS_MARK = 'checkout_goods_mark';
const CHECKOUT_COMMENT = 'checkout_comment';

const ATTRIBUTES = [
self::CHECKOUT_BUYER_EMAIL,
self::CHECKOUT_BUYER_NAME,
self::CHECKOUT_COMMENT,
self::CHECKOUT_GOODS_MARK,
self::CHECKOUT_PURCHASE_ORDER_NO,
];

/**
* Get checkout buyer name
*
Expand Down Expand Up @@ -105,4 +113,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;
}
77 changes: 77 additions & 0 deletions Helper/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Bodak\CheckoutCustomForm\Helper;


use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;

class Config
{
const LIMIT_NOT_SET = -1;

/**
* @var ScopeConfigInterface
*/
private $config;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var LoggerInterface
*/
private $logger;

/**
* Config constructor.
*
* @param ScopeConfigInterface $config
* @param StoreManagerInterface $storeManager
* @param LoggerInterface $logger
*/
public function __construct(
ScopeConfigInterface $config,
StoreManagerInterface $storeManager,
LoggerInterface $logger
) {
$this->config = $config;
$this->storeManager = $storeManager;
$this->logger = $logger;
}

/**
* @return array
*/
public function getEnabledFields()
{
return explode(',',
$this->config->getValue('bodak/checkout/enabled_fields',
ScopeInterface::SCOPE_STORE));
}

/**
* @param $attribute
*
* @return int
*/
public function getAllowedLength($attribute)
{
$configPath = 'checkout/general/' . $attribute . '_limit';
try {
$allowedLength = $this->config->getValue($configPath, ScopeInterface::SCOPE_STORES,
$this->storeManager->getStore()->getId());
} catch (NoSuchEntityException $e) {
$this->logger->error('Cannot get allowed length for custom field. Falling back to default scope value.');
$this->logger->error($e->getMessage(), ['exception' => $e]);
$allowedLength = $this->config->getValue($configPath);
}

return (int)$allowedLength ?: self::LIMIT_NOT_SET;
}
}
140 changes: 140 additions & 0 deletions Model/Checkout/LayoutProcessor/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

/**
* @package Bodak\CheckoutCustomForm
* @author Konrad Langenberg <k.langenberg@imi.de>
* @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;
use Bodak\CheckoutCustomForm\Helper\Config;

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

/**
* @var array
*/
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,
'maxlength' => null,
'elementTmpl' => 'Bodak_CheckoutCustomForm/form/element/textarea',
],
'showTitle' => false,
],
];

/**
* LayoutProcessor constructor.
*
* @param Config $config
*/
public function __construct(Config $config)
{
$this->config = $config;
}

/**
* @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
* @param array $jsLayout
*
* @return array
* @see \Magento\Checkout\Block\Checkout\LayoutProcessor::process
*/
public function afterProcess(
\Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
array $jsLayout
) {
$config = $this->config->getEnabledFields();

$this->applyLengthLimitToFields();

foreach ($this->fields as $sortOrder => $field) {
if (!in_array($field['dataScopeName'], $config)) {
continue;
}
if (!isset($field['showTitle'])) {
$field['showTitle'] = true;
}

$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['showTitle'] ? __($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']['custom-checkout-form-container']
['children']['custom-checkout-form-fieldset']['children'][$field['dataScopeName']] = $formField;
}

return $jsLayout;
}

private function applyLengthLimitToFields()
{
foreach ($this->fields as $key => $field) {
$fieldName = $field['dataScopeName'];
$allowedLength = $this->config->getAllowedLength($fieldName);
if ($allowedLength === Config::LIMIT_NOT_SET) {
continue;
}
$this->fields[$key]['config']['maxlength'] = $allowedLength;
$this->fields[$key]['validation']['max_text_length'] = $allowedLength;
}
}
}
29 changes: 29 additions & 0 deletions Model/Config/Source/Option.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* @package Bodak\CheckoutCustomForm
* @author Konrad Langenberg <k.langenberg@imi.de>
* @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;
use \Bodak\CheckoutCustomForm\Api\Data\CustomFieldsInterface;

class Option implements ArrayInterface
{
public function toOptionArray()
{
return [
['value' => 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'],
];
}
}
Loading