AjaxSave.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Tax\Controller\Adminhtml\Rate;
  8. use Magento\Framework\Controller\ResultFactory;
  9. class AjaxSave extends \Magento\Tax\Controller\Adminhtml\Rate
  10. {
  11. /**
  12. * Save Tax Rate via AJAX
  13. *
  14. * @return \Magento\Framework\Controller\Result\Json
  15. * @throws \InvalidArgumentException
  16. */
  17. public function execute()
  18. {
  19. try {
  20. $rateData = $this->_processRateData($this->getRequest()->getPostValue());
  21. /** @var \Magento\Tax\Api\Data\TaxRateInterface $taxRate */
  22. $taxRate = $this->_taxRateConverter->populateTaxRateData($rateData);
  23. $this->_taxRateRepository->save($taxRate);
  24. $responseContent = [
  25. 'success' => true,
  26. 'error_message' => '',
  27. 'tax_calculation_rate_id' => $taxRate->getId(),
  28. 'code' => htmlspecialchars($taxRate->getCode()),
  29. ];
  30. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  31. $responseContent = [
  32. 'success' => false,
  33. 'error_message' => $e->getMessage(),
  34. 'tax_calculation_rate_id' => '',
  35. 'code' => '',
  36. ];
  37. } catch (\Exception $e) {
  38. $responseContent = [
  39. 'success' => false,
  40. 'error_message' => __('We can\'t save this rate right now.'),
  41. 'tax_calculation_rate_id' => '',
  42. 'code' => '',
  43. ];
  44. }
  45. /** @var \Magento\Framework\Controller\Result\Json $resultJson */
  46. $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
  47. $resultJson->setData($responseContent);
  48. return $resultJson;
  49. }
  50. }