diff --git a/src/ViewModel/CheckoutConfigProvider.php b/src/ViewModel/CheckoutConfigProvider.php index 6b823335..43686bf7 100644 --- a/src/ViewModel/CheckoutConfigProvider.php +++ b/src/ViewModel/CheckoutConfigProvider.php @@ -4,9 +4,12 @@ namespace Hyva\ReactCheckout\ViewModel; use Magento\Checkout\Model\CompositeConfigProvider; +use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Framework\DataObject; use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\View\Element\Block\ArgumentInterface; use Magento\Framework\Locale\ResolverInterface as LocaleResolverInterface; +use Magento\Store\Model\ScopeInterface; use Magento\Framework\Event\ManagerInterface as EventManager; class CheckoutConfigProvider implements ArgumentInterface @@ -31,9 +34,17 @@ class CheckoutConfigProvider implements ArgumentInterface */ private $currencyProvider; + + /** + * @var ScopeConfigInterface + */ + private $scopeConfig; + + /** * @var EventManager */ + private $eventManager; /** @@ -42,20 +53,23 @@ class CheckoutConfigProvider implements ArgumentInterface * @param SerializerInterface $serializer * @param LocaleResolverInterface $localeResolver * @param CompositeConfigProvider $compositeConfigProvider - * @param CurrencyProvider $currencyProvider + * @param ScopeConfigInterface $scopeConfig * @param EventManager $eventManager */ public function __construct( - SerializerInterface $serializer, + SerializerInterface $serializer, LocaleResolverInterface $localeResolver, CompositeConfigProvider $compositeConfigProvider, - CurrencyProvider $currencyProvider, - EventManager $eventManager - ) { + CurrencyProvider $currencyProvider, + ScopeConfigInterface $scopeConfig, + EventManager $eventManager + ) + { $this->serializer = $serializer; $this->localeResolver = $localeResolver; $this->compositeConfigProvider = $compositeConfigProvider; $this->currencyProvider = $currencyProvider; + $this->scopeConfig = $scopeConfig; $this->eventManager = $eventManager; } @@ -78,7 +92,7 @@ public function getConfig(): string $storeCode = $checkoutConfig['storeCode']; $checkoutConfig['payment']['restUrlPrefix'] = "/rest/$storeCode/V1/"; - $transport = new \Magento\Framework\DataObject([ + $transport = new DataObject([ 'checkoutConfig' => $checkoutConfig, 'output' => [ 'storeCode' => $storeCode, @@ -86,11 +100,26 @@ public function getConfig(): string 'language' => $this->localeResolver->getLocale(), 'currency' => $this->currencyProvider->getConfig(), 'defaultCountryId' => $checkoutConfig['defaultCountryId'], + 'address' => $this->getAddressConfig() ] ]); - $this->eventManager->dispatch('hyva_react_checkout_config', ['transport' => $transport]); - return $this->serializer->serialize($transport->getData('output')); } + + private function getAddressConfig() + { + + return [ + 'company' => $this->getValue("customer/address/company_show") + ]; + } + + public function getValue($field, $storeId = null) + { + return $this->scopeConfig->getValue($field, + ScopeInterface::SCOPE_STORE, + $storeId + ); + } } diff --git a/src/reactapp/src/components/billingAddress/components/BillingAddressForm.jsx b/src/reactapp/src/components/billingAddress/components/BillingAddressForm.jsx index 664c8a32..8a21f0da 100644 --- a/src/reactapp/src/components/billingAddress/components/BillingAddressForm.jsx +++ b/src/reactapp/src/components/billingAddress/components/BillingAddressForm.jsx @@ -14,6 +14,7 @@ import useAddressWrapper from '../../address/hooks/useAddressWrapper'; import useBillingAddressAppContext from '../hooks/useBillingAddressAppContext'; import useFormValidateThenSubmit from '../../../hook/useFormValidateThenSubmit'; import useBillingAddressFormikContext from '../hooks/useBillingAddressFormikContext'; +import RootElement from '../../../utils/rootElement'; function BillingAddressForm() { const { @@ -32,6 +33,7 @@ function BillingAddressForm() { validationSchema, setSelectedAddress, } = useBillingAddressFormikContext(); + const config = RootElement.getAddressConfig(); const { isLoggedIn } = useBillingAddressAppContext(); const { reCalculateMostRecentAddressOptions } = useAddressWrapper(); const formSubmitHandler = useFormValidateThenSubmit({ @@ -97,14 +99,16 @@ function BillingAddressForm() { return ( <>
- + {config.company && ( + + )}
- + {config.company && ( + + )}