17 use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\RateQueryFactory;
132 private $rateQueryFactory;
148 \Psr\Log\LoggerInterface
$logger,
154 RateQueryFactory $rateQueryFactory,
163 $this->
import = $import;
164 $this->rateQueryFactory = $rateQueryFactory;
174 $this->
_init(
'shipping_tablerate',
'pk');
183 public function getRate(\
Magento\Quote\Model\Quote\Address\RateRequest
$request)
189 $rateQuery = $this->rateQueryFactory->create([
'request' =>
$request]);
191 $rateQuery->prepareSelect(
$select);
192 $bindings = $rateQuery->getBindings();
208 private function deleteByCondition(array $condition)
231 $this->_importedRows += count(
$values);
233 }
catch (\
Magento\Framework\Exception\LocalizedException $e) {
235 throw new \Magento\Framework\Exception\LocalizedException(
__(
'Unable to import data'), $e);
236 }
catch (\Exception $e) {
238 $this->logger->critical($e);
239 throw new \Magento\Framework\Exception\LocalizedException(
240 __(
'Something went wrong while importing table rates.')
257 public function uploadAndImport(\
Magento\Framework\DataObject $object)
262 if (empty($_FILES[
'groups'][
'tmp_name'][
'tablerate'][
'fields'][
'import'][
'value'])) {
265 $filePath = $_FILES[
'groups'][
'tmp_name'][
'tablerate'][
'fields'][
'import'][
'value'];
267 $websiteId = $this->storeManager->getWebsite($object->getScopeId())->getId();
270 $file = $this->getCsvFile($filePath);
275 'condition_name = ?' => $conditionName,
277 $this->deleteByCondition($condition);
279 $columns = $this->
import->getColumns();
281 foreach ($this->import->getData($file,
$websiteId, $conditionName, $conditionFullName) as $bunch) {
282 $this->importData(
$columns, $bunch);
284 }
catch (\Exception $e) {
285 $this->logger->critical($e);
286 throw new \Magento\Framework\Exception\LocalizedException(
287 __(
'Something went wrong while importing table rates.')
293 if ($this->import->hasErrors()) {
295 'We couldn\'t import this file because of these errors: %1',
296 implode(
" \n", $this->import->getErrors())
298 throw new \Magento\Framework\Exception\LocalizedException($error);
309 if ($object->getData(
'groups/tablerate/fields/condition_name/inherit') ==
'1') {
310 $conditionName = (string)$this->coreConfig->getValue(
'carriers/tablerate/condition_name',
'default');
312 $conditionName = $object->getData(
'groups/tablerate/fields/condition_name/value');
314 return $conditionName;
321 private function getCsvFile($filePath)
323 $pathInfo = pathinfo($filePath);
324 $dirName = isset($pathInfo[
'dirname']) ? $pathInfo[
'dirname'] :
'';
325 $fileName = isset($pathInfo[
'basename']) ? $pathInfo[
'basename'] :
'';
327 $directoryRead = $this->filesystem->getDirectoryReadByPath($dirName);
329 return $directoryRead->openFile(
$fileName);
340 if (!isset($this->_conditionFullNames[$conditionName])) {
341 $name = $this->carrierTablerate->getCode(
'condition_name_short', $conditionName);
342 $this->_conditionFullNames[$conditionName] =
$name;
345 return $this->_conditionFullNames[$conditionName];
367 $this->_importedRows += count(
$data);
_getConditionFullName($conditionName)
getConditionName(\Magento\Framework\DataObject $object)
_init($mainTable, $idFieldName)
_saveImportData(array $data)
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Psr\Log\LoggerInterface $logger, \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\OfflineShipping\Model\Carrier\Tablerate $carrierTablerate, \Magento\Framework\Filesystem $filesystem, Import $import, RateQueryFactory $rateQueryFactory, $connectionName=null)
if(!isset($_GET['name'])) $name