AssignBackendModelToAttributeObserver.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Observer;
  7. use Magento\Framework\Event\ObserverInterface;
  8. class AssignBackendModelToAttributeObserver implements ObserverInterface
  9. {
  10. /**
  11. * @var \Magento\Catalog\Model\Product\Type
  12. */
  13. protected $productType;
  14. /**
  15. * @var \Magento\Catalog\Model\ProductTypes\ConfigInterface
  16. */
  17. protected $productTypeConfig;
  18. /**
  19. * @param \Magento\Catalog\Model\Product\Type $productType
  20. * @param \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig
  21. */
  22. public function __construct(
  23. \Magento\Catalog\Model\Product\Type $productType,
  24. \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig
  25. ) {
  26. $this->productType = $productType;
  27. $this->productTypeConfig = $productTypeConfig;
  28. }
  29. /**
  30. * Automatically assign backend model to weee attributes
  31. *
  32. * @param \Magento\Framework\Event\Observer $observer
  33. * @return $this
  34. */
  35. public function execute(\Magento\Framework\Event\Observer $observer)
  36. {
  37. $backendModel = \Magento\Weee\Model\Attribute\Backend\Weee\Tax::getBackendModelName();
  38. /** @var $object \Magento\Eav\Model\Entity\Attribute\AbstractAttribute */
  39. $object = $observer->getEvent()->getAttribute();
  40. if ($object->getFrontendInput() == 'weee') {
  41. $object->setBackendModel($backendModel);
  42. if (!$object->getApplyTo()) {
  43. $applyTo = [];
  44. foreach ($this->productType->getOptions() as $option) {
  45. if ($this->productTypeConfig->isProductSet($option['value'])) {
  46. continue;
  47. }
  48. $applyTo[] = $option['value'];
  49. }
  50. $object->setApplyTo($applyTo);
  51. }
  52. }
  53. return $this;
  54. }
  55. }