From f2e9622c56e71cc73e37eb8b0ff3b54a9f2b46b6 Mon Sep 17 00:00:00 2001 From: Oliverio Date: Wed, 11 Mar 2020 04:02:55 -0300 Subject: [PATCH] #13 Document option were added to system --- Helper/Data.php | 61 +++++++++++++++++-- Model/Config/Source/Types.php | 22 +++++++ Plugin/CidConfiguration.php | 26 ++++++-- etc/adminhtml/system.xml | 12 +++- etc/config.xml | 2 + view/frontend/layout/checkout_index_index.xml | 9 +-- 6 files changed, 119 insertions(+), 13 deletions(-) create mode 100644 Model/Config/Source/Types.php diff --git a/Helper/Data.php b/Helper/Data.php index 43feaf7..e17807a 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -12,11 +12,12 @@ namespace Mugar\CustomerIdentificationDocument\Helper; use Magento\Framework\App\Config\ScopeConfigInterface; -use Magento\Framework\Exception\NoSuchEntityException; -use Magento\Store\Model\ScopeInterface; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; +use Magento\Framework\Exception\NoSuchEntityException; +use Magento\Store\Model\ScopeInterface; use Magento\Store\Model\StoreManagerInterface; +use Mugar\CustomerIdentificationDocument\Model\Config\Source\Types; /** * Helper to get system config @@ -32,6 +33,8 @@ class Data extends AbstractHelper { const SHIPPING_ENABLED = 'checkout/cid/shipping_enabled'; const BILLING_ENABLED = 'checkout/cid/billing_enabled'; + const SHIPPING_DOCUMENT_TYPES = 'checkout/cid/shipping_types'; + const BILLING_DOCUMENT_TYPES = 'checkout/cid/billing_types'; /** * Magento\Framework\App\Config\ScopeConfigInterface @@ -45,6 +48,8 @@ class Data extends AbstractHelper */ private $_storeManager; + private $_types; + /** * Data constructor. * @param Context $context @@ -54,11 +59,13 @@ class Data extends AbstractHelper public function __construct( Context $context, ScopeConfigInterface $scopeConfigInterface, - StoreManagerInterface $storeManagerInterface + StoreManagerInterface $storeManagerInterface, + Types $types ) { parent::__construct($context); $this->_scopeConfig = $scopeConfigInterface; $this->_storeManager = $storeManagerInterface; + $this->_types = $types; } /** @@ -87,6 +94,52 @@ public function isBillingEnabled($store = null) return $this->getConfigValue(self::BILLING_ENABLED, $storeId); } + /** + * get Shipping Document Types + * + * @param null $store + * @return mixed + * @throws NoSuchEntityException + */ + public function getShippingDocumentTypes($store = null) + { + $optionsSelected = []; + $storeId = is_null($store) ? $this->getStoreId() : $store->getId(); + $options = $this->_types->toOptionArray(); + $systemOptionsSelected = explode(',', $this->getConfigValue(self::SHIPPING_DOCUMENT_TYPES, $storeId)); + + foreach ($options as $option) { + if (in_array($option['value'], $systemOptionsSelected)) { + $optionsSelected[$option['value']] = $option['label']->getText(); + } + } + + return $optionsSelected; + } + + /** + * get Shipping Document Types + * + * @param null $store + * @return mixed + * @throws NoSuchEntityException + */ + public function getBillingDocumentTypes($store = null) + { + $optionsSelected = []; + $storeId = is_null($store) ? $this->getStoreId() : $store->getId(); + $options = $this->_types->toOptionArray(); + $systemOptionsSelected = explode(',', $this->getConfigValue(self::BILLING_DOCUMENT_TYPES, $storeId)); + + foreach ($options as $option) { + if (in_array($option['value'], $systemOptionsSelected)) { + $optionsSelected[$option['value']] = $option['label']->getText(); + } + } + + return $optionsSelected; + } + /** * Get configuration value * @@ -112,4 +165,4 @@ public function getStoreId() { return $this->_storeManager->getStore()->getId(); } -} \ No newline at end of file +} diff --git a/Model/Config/Source/Types.php b/Model/Config/Source/Types.php new file mode 100644 index 0000000..5b2b3a4 --- /dev/null +++ b/Model/Config/Source/Types.php @@ -0,0 +1,22 @@ + 1, 'label' => __('DNI')], + ['value' => 2, 'label' => __('CI')], + ['value' => 3, 'label' => __('Passport')] + ]; + } +} diff --git a/Plugin/CidConfiguration.php b/Plugin/CidConfiguration.php index 9cc2243..fbcaf2e 100644 --- a/Plugin/CidConfiguration.php +++ b/Plugin/CidConfiguration.php @@ -12,10 +12,10 @@ namespace Mugar\CustomerIdentificationDocument\Plugin; use Magento\Checkout\Block\Checkout\LayoutProcessor; -use Magento\Framework\Exception\NoSuchEntityException; use Mugar\CustomerIdentificationDocument\Helper\Data; -class CidConfiguration { +class CidConfiguration +{ /** * Mugar\CustomerIdentificationDocument\Helper\Data @@ -36,7 +36,11 @@ public function __construct( public function afterProcess( LayoutProcessor $processor, array $jsLayout - ){ + ) { + $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/oliverio.log'); + $logger = new \Zend\Log\Logger(); + $logger->addWriter($writer); + if (!$this->helper->isShippingEnabled()) { $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children'] ['shippingAddress']['children']['cid-shipping-form']['config']['componentDisabled'] = true; @@ -47,6 +51,20 @@ public function afterProcess( ['payment']['children']['cid-billing-form']['config']['componentDisabled'] = true; } + $currentOptions = $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children'] + ['shippingAddress']['children']['cid-shipping-form']['children']['cid-shipping-form-container']['children']['cid-shipping-form-fieldset']['children']['shipping_cid_type']['options']; + + foreach ($this->helper->getShippingDocumentTypes() as $k => $v) { + $currentOptions[] = ['value' => $k, 'label' => $v]; + } + + $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children'] + ['shippingAddress']['children']['cid-shipping-form']['children']['cid-shipping-form-container']['children']['cid-shipping-form-fieldset']['children']['shipping_cid_type']['options'] = $currentOptions; + + $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children'] + ['payment']['children']['cid-billing-form']['children']['cid-billing-form-container']['children']['cid-billing-form-fieldset']['children']['billing_cid_type']['options'] = $currentOptions; + + $logger->info(print_r($currentOptions, true)); return $jsLayout; } -} \ No newline at end of file +} diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 10a66c5..7b79caa 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -8,10 +8,20 @@ Magento\Config\Model\Config\Source\Yesno - + + + Mugar\CustomerIdentificationDocument\Model\Config\Source\Types + required-entry validate-select + + Magento\Config\Model\Config\Source\Yesno + + + Mugar\CustomerIdentificationDocument\Model\Config\Source\Types + required-entry validate-select + diff --git a/etc/config.xml b/etc/config.xml index be5ab05..82844ce 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -5,6 +5,8 @@ 0 0 + 1 + 1 diff --git a/view/frontend/layout/checkout_index_index.xml b/view/frontend/layout/checkout_index_index.xml index 858f7b8..d1744da 100644 --- a/view/frontend/layout/checkout_index_index.xml +++ b/view/frontend/layout/checkout_index_index.xml @@ -45,7 +45,7 @@ Please select value - + checkoutProvider cidShippingForm.shipping_cid_type @@ -121,7 +122,7 @@ uiComponent cid-billing-form-fields - + Magento_Ui/js/form/element/select @@ -158,7 +159,7 @@ true true - + Magento_Ui/js/form/element/abstract @@ -203,4 +204,4 @@ - \ No newline at end of file +