public function getCountriesForMethodsList($methods_arr = false) { $method_ids_arr = false; if (!empty($methods_arr) and is_array($methods_arr)) { $method_ids_arr = array(); foreach ($methods_arr as $method_id) { $method_id = intval($method_id); if (empty($method_id)) { continue; } $method_ids_arr[] = $method_id; } } $collection = $this->getCollection(); if (!empty($method_ids_arr)) { $collection->addFieldToFilter('main_table.method_id', array('in' => $method_ids_arr)); } $country_collection = $this->_countryFactory->create()->getCollection(); $collection->getSelect()->join($country_collection->getMainTable(), 'main_table.country_id = ' . $country_collection->getMainTable() . '.country_id'); $return_arr = array(); $return_arr['all'] = array(); $return_arr['methods'] = array(); while ($country_method_obj = $collection->fetchItem() and $country_method_arr = $country_method_obj->getData()) { if (empty($country_method_arr['country_id'])) { continue; } if (!isset($return_arr['all'][$country_method_arr['country_id']])) { $return_arr['all'][$country_method_arr['country_id']] = array('code' => $country_method_arr['code'], 'name' => $country_method_arr['name']); } $return_arr['methods'][$country_method_arr['method_id']][$country_method_arr['code']] = $country_method_arr['name']; } return $return_arr; }
/** * @param int $method_id ID of method to get all configurations for * @param bool|array $params * * @return array $return_arr[{country_code}]['surcharge'], $return_arr[{country_ids}]['fixed_amount'], ... */ public function getConfiguredMethodDetails($method_id, $params = false) { $method_id = intval($method_id); if (empty($method_id)) { return false; } if (empty($params) or !is_array($params)) { $params = array(); } if (!isset($params['only_active'])) { $params['only_active'] = true; } if (!isset($params['country_code'])) { $params['country_code'] = ''; } $our_country_id = 0; if (!empty($params['country_code'])) { $params['country_code'] = strtoupper(trim($params['country_code'])); if (strlen($params['country_code']) != 2) { return false; } $c_collection = $this->_countryFactory->create()->getCollection(); $c_collection->addFieldToSelect('*'); $c_collection->addFieldToFilter('main_table.code', $params['country_code']); $c_collection->getSelect()->limit(1); if ($country_obj = $c_collection->fetchItem() and $country_arr = $country_obj->getData()) { $our_country_id = $country_arr['country_id']; } if (empty($our_country_id)) { return false; } } $collection = $this->getCollection(); $collection->addFieldToSelect('*'); $collection->addFieldToFilter('main_table.method_id', $method_id); if (!empty($our_country_id)) { $collection->addFieldToFilter(['main_table.country_id', 'main_table.country_id'], [0, $our_country_id]); $collection->setOrder('main_table.country_id', 'DESC'); } $method_collection = $this->_methodFactory->create()->getCollection(); $collection->getSelect()->join($method_collection->getMainTable(), 'main_table.method_id = ' . $method_collection->getMainTable() . '.method_id'); if (!empty($params['only_active'])) { $collection->addFieldToFilter($method_collection->getMainTable() . '.active', 1); } $data_arr = array(); $countries_ids_arr = array(); while ($configured_method_obj = $collection->fetchItem() and $configured_method_arr = $configured_method_obj->getData()) { if (!empty($configured_method_arr['country_id'])) { $countries_ids_arr[] = $configured_method_arr['country_id']; } $data_arr[$configured_method_arr['country_id']] = $configured_method_arr; } if (empty($data_arr)) { return false; } $ids_to_codes_arr = [0 => Country::INTERNATIONAL_CODE]; if (!empty($countries_ids_arr)) { //! TODO: query countries table to obtain country codes from ids // ATM all method settings are same for all countries... } $return_arr = array(); foreach ($data_arr as $country_id => $details_arr) { if (!isset($ids_to_codes_arr[$country_id])) { continue; } $return_arr[$ids_to_codes_arr[$country_id]] = $details_arr; } return !empty($return_arr) ? $return_arr : false; }