Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. $values = array(
  2.  
  3. array( // option group
  4. 'label' => 'Group 1',
  5. 'value' =>
  6. array(
  7. array( // Option 1
  8. 'value' => '1',
  9. 'label' => 'Option 1'
  10. ),
  11. array( // Option 2
  12. 'value' => '2',
  13. 'label' => 'Option 2'
  14. )
  15. )
  16. ),
  17.  
  18. array( // option group
  19. 'label' => 'Group 2',
  20. 'value' =>
  21. array(
  22. array(
  23. 'value' => '3',
  24. 'label' => 'Option 3'
  25. ),
  26. array(
  27. 'value' => '4',
  28. 'label' => 'Option 4'
  29. )
  30. )
  31. ),
  32. );
  33.  
  34. $allowedCurrencies = $this->scopeConfig->getValue('currency/options/allow');
  35.  
  36. <source_model>MagentoConfigModelConfigSourceLocaleCurrency</source_model>
  37.  
  38. use MagentoFrameworkAppConfigScopeConfigInterface;
  39.  
  40. /**
  41. * Class Config
  42. *
  43. * @package NameMyModuleModelConfigBackend
  44. */
  45. class Config implements MagentoFrameworkDataOptionSourceInterface
  46. {
  47. /**
  48. * @var MagentoFrameworkAppHelperContext
  49. */
  50. private $context;
  51.  
  52. /**
  53. * @var ScopeConfigInterface
  54. */
  55. private $scopeConfig;
  56.  
  57. /**
  58. * @param MagentoFrameworkAppHelperContext $context
  59. */
  60. public function __construct(MagentoFrameworkAppHelperContext $context)
  61. {
  62. $this->context = $context;
  63. $this->scopeConfig = $context->getScopeConfig();
  64. }
  65.  
  66. /**
  67. * Get allowed currencies
  68. *
  69. * @return array of allowed Currencies
  70. *
  71. **/
  72. public function getAllowedCurrencies()
  73. {
  74. $allowedCurrencies = $this->scopeConfig->getValue('currency/options/allow');
  75. $allowedCurrencies = explode(",", $allowedCurrencies);
  76.  
  77. $options = [];
  78. foreach($allowedCurrencies as $k=>$v)
  79. {
  80. $options[] = ['value' => $v, 'label' => $v];
  81. }
  82.  
  83. return $options;
  84. }
  85.  
  86.  
  87. /**
  88. * Admin Config action
  89. *
  90. * @return array
  91. */
  92. public function toOptionArray()
  93. {
  94. return $this->getAllowedCurrencies();
  95. }
  96. }
  97.  
  98. <field id="currency" showInDefault="1" showInWebsite="0" showInStore="0" sortOrder="10" translate="label comment" type="multiselect">
  99. <label>Currency</label>
  100. <source_model>NamespaceModulenameModelConfigSourceCurrency</source_model>
  101. </field>
  102.  
  103. <?php
  104. namespace NamespaceModulenameModelConfigSource;
  105.  
  106. use MagentoFrameworkOptionArrayInterface;
  107.  
  108. class Currency implements ArrayInterface {
  109.  
  110. /**
  111. * @var CollectionFactory
  112. */
  113. public $currency;
  114.  
  115. /**
  116. * @param MagentoCurrencySymbolModelSystemCurrencysymbol $currency
  117. */
  118. public function __construct(
  119. MagentoCurrencySymbolModelSystemCurrencysymbol $currency
  120. ) {
  121. $this->currency = $currency;
  122. }
  123.  
  124. /**
  125. * Return currency symbol properties array based on config values
  126. *
  127. * @return array
  128. */
  129. protected function getCurrency()
  130. {
  131. return $this->currency->getCurrencySymbolsData();
  132. }
  133.  
  134. /**
  135. * @return array
  136. */
  137. public function toOptionArray()
  138. {
  139. $currency = $this->getCurrency();
  140. $options = [];
  141. foreach ($this->getCurrency() as $code => $currency) {
  142. $options[$code] = [
  143. 'label' => $currency['displayName'],
  144. 'value' => $code
  145. ];
  146. }
  147.  
  148. return $options;
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement