TaxClass.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model\Config;
  7. /**
  8. * TaxClass Config
  9. */
  10. class TaxClass extends \Magento\Framework\App\Config\Value
  11. {
  12. /**
  13. * @var \Magento\Config\Model\ResourceModel\Config
  14. */
  15. protected $resourceConfig;
  16. /**
  17. * @var \Magento\Eav\Model\Entity\AttributeFactory
  18. */
  19. protected $attributeFactory;
  20. /**
  21. * @param \Magento\Framework\Model\Context $context
  22. * @param \Magento\Framework\Registry $registry
  23. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  24. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  25. * @param \Magento\Config\Model\ResourceModel\Config $resourceConfig
  26. * @param \Magento\Eav\Model\Entity\AttributeFactory $attributeFactory
  27. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  28. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  29. * @param array $data
  30. */
  31. public function __construct(
  32. \Magento\Framework\Model\Context $context,
  33. \Magento\Framework\Registry $registry,
  34. \Magento\Framework\App\Config\ScopeConfigInterface $config,
  35. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
  36. \Magento\Config\Model\ResourceModel\Config $resourceConfig,
  37. \Magento\Eav\Model\Entity\AttributeFactory $attributeFactory,
  38. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  39. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  40. array $data = []
  41. ) {
  42. $this->resourceConfig = $resourceConfig;
  43. $this->attributeFactory = $attributeFactory;
  44. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  45. }
  46. /**
  47. * Update the default product tax class
  48. *
  49. * @return $this
  50. */
  51. public function afterSave()
  52. {
  53. $attributeCode = "tax_class_id";
  54. $attribute = $this->attributeFactory->create();
  55. $attribute->loadByCode(\Magento\Catalog\Model\Product::ENTITY, $attributeCode);
  56. if (!$attribute->getId()) {
  57. throw new \Magento\Framework\Exception\LocalizedException(__('Invalid attribute %1', $attributeCode));
  58. }
  59. $attribute->setData("default_value", $this->getData('value'));
  60. $attribute->save();
  61. return parent::afterSave();
  62. }
  63. }