Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Minsaleqty.php
Go to the documentation of this file.
1 <?php
8 
13 
18 {
24  protected $scopeConfig;
25 
29  protected $mathRandom;
30 
34  protected $groupManagement;
35 
39  private $serializer;
40 
44  private $minSaleQtyCache = [];
45 
52  public function __construct(
54  \Magento\Framework\Math\Random $mathRandom,
56  Json $serializer = null
57  ) {
58  $this->scopeConfig = $scopeConfig;
59  $this->mathRandom = $mathRandom;
60  $this->groupManagement = $groupManagement;
61  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
62  }
63 
70  protected function fixQty($qty)
71  {
72  return !empty($qty) ? (float) $qty : null;
73  }
74 
81  protected function serializeValue($value)
82  {
83  if (is_numeric($value)) {
84  $data = (float) $value;
85  return (string) $data;
86  } elseif (is_array($value)) {
87  $data = [];
88  foreach ($value as $groupId => $qty) {
89  if (!array_key_exists($groupId, $data)) {
90  $data[$groupId] = $this->fixQty($qty);
91  }
92  }
93  if (count($data) == 1 && array_key_exists($this->getAllCustomersGroupId(), $data)) {
94  return (string) $data[$this->getAllCustomersGroupId()];
95  }
96  return $this->serializer->serialize($data);
97  } else {
98  return '';
99  }
100  }
101 
108  protected function unserializeValue($value)
109  {
110  if (is_numeric($value)) {
111  return [$this->getAllCustomersGroupId() => $this->fixQty($value)];
112  } elseif (is_string($value) && !empty($value)) {
113  return $this->serializer->unserialize($value);
114  } else {
115  return [];
116  }
117  }
118 
125  protected function isEncodedArrayFieldValue($value)
126  {
127  if (!is_array($value)) {
128  return false;
129  }
130  unset($value['__empty']);
131  foreach ($value as $row) {
132  if (!is_array($row)
133  || !array_key_exists('customer_group_id', $row)
134  || !array_key_exists('min_sale_qty', $row)
135  ) {
136  return false;
137  }
138  }
139  return true;
140  }
141 
148  protected function encodeArrayFieldValue(array $value)
149  {
150  $result = [];
151  foreach ($value as $groupId => $qty) {
152  $resultId = $this->mathRandom->getUniqueHash('_');
153  $result[$resultId] = ['customer_group_id' => $groupId, 'min_sale_qty' => $this->fixQty($qty)];
154  }
155  return $result;
156  }
157 
164  protected function decodeArrayFieldValue(array $value)
165  {
166  $result = [];
167  unset($value['__empty']);
168  foreach ($value as $row) {
169  if (!is_array($row)
170  || !array_key_exists('customer_group_id', $row)
171  || !array_key_exists('min_sale_qty', $row)
172  ) {
173  continue;
174  }
175  $groupId = $row['customer_group_id'];
176  $qty = $this->fixQty($row['min_sale_qty']);
177  $result[$groupId] = $qty;
178  }
179  return $result;
180  }
181 
189  public function getConfigValue($customerGroupId, $store = null)
190  {
191  $key = $customerGroupId . '-' . $store;
192  if (!isset($this->minSaleQtyCache[$key])) {
193  $value = $this->scopeConfig->getValue(
194  \Magento\CatalogInventory\Model\Configuration::XML_PATH_MIN_SALE_QTY,
196  $store
197  );
198  $value = $this->unserializeValue($value);
199  if ($this->isEncodedArrayFieldValue($value)) {
201  }
202  $result = null;
203  foreach ($value as $groupId => $qty) {
204  if ($groupId == $customerGroupId) {
205  $result = $qty;
206  break;
207  } elseif ($groupId == $this->getAllCustomersGroupId()) {
208  $result = $qty;
209  }
210  }
211  $this->minSaleQtyCache[$key] = $this->fixQty($result);
212  }
213  return $this->minSaleQtyCache[$key];
214  }
215 
222  public function makeArrayFieldValue($value)
223  {
224  $value = $this->unserializeValue($value);
225  if (!$this->isEncodedArrayFieldValue($value)) {
227  }
228  return $value;
229  }
230 
238  {
239  if ($this->isEncodedArrayFieldValue($value)) {
241  }
242  $value = $this->serializeValue($value);
243  return $value;
244  }
245 
251  protected function getAllCustomersGroupId()
252  {
253  return $this->groupManagement->getAllCustomersGroup()->getId();
254  }
255 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Math\Random $mathRandom, GroupManagementInterface $groupManagement, Json $serializer=null)
Definition: Minsaleqty.php:52
getConfigValue($customerGroupId, $store=null)
Definition: Minsaleqty.php:189
$value
Definition: gender.phtml:16