AttributeOptionProvider.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ConfigurableProduct\Model;
  7. use Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionSelectBuilderInterface;
  8. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  9. use Magento\Framework\App\ScopeResolverInterface;
  10. use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute;
  11. use Magento\Framework\DB\Select;
  12. /**
  13. * Provider for retrieving configurable options.
  14. */
  15. class AttributeOptionProvider implements AttributeOptionProviderInterface
  16. {
  17. /**
  18. * @var ScopeResolverInterface
  19. */
  20. private $scopeResolver;
  21. /**
  22. * @var Attribute
  23. */
  24. private $attributeResource;
  25. /**
  26. * @var OptionSelectBuilderInterface
  27. */
  28. private $optionSelectBuilder;
  29. /**
  30. * @param Attribute $attributeResource
  31. * @param ScopeResolverInterface $scopeResolver,
  32. * @param OptionSelectBuilderInterface $optionSelectBuilder
  33. */
  34. public function __construct(
  35. Attribute $attributeResource,
  36. ScopeResolverInterface $scopeResolver,
  37. OptionSelectBuilderInterface $optionSelectBuilder
  38. ) {
  39. $this->attributeResource = $attributeResource;
  40. $this->scopeResolver = $scopeResolver;
  41. $this->optionSelectBuilder = $optionSelectBuilder;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function getAttributeOptions(AbstractAttribute $superAttribute, $productId)
  47. {
  48. $scope = $this->scopeResolver->getScope();
  49. $select = $this->optionSelectBuilder->getSelect($superAttribute, $productId, $scope);
  50. $data = $this->attributeResource->getConnection()->fetchAll($select);
  51. if ($superAttribute->getSourceModel()) {
  52. $options = $superAttribute->getSource()->getAllOptions(false);
  53. $optionLabels = [];
  54. foreach ($options as $option) {
  55. $optionLabels[$option['value']] = $option['label'];
  56. }
  57. foreach ($data as $key => $value) {
  58. $optionText = isset($optionLabels[$value['value_index']])
  59. ? $optionLabels[$value['value_index']]
  60. : false;
  61. $data[$key]['default_title'] = $optionText;
  62. $data[$key]['option_title'] = $optionText;
  63. }
  64. }
  65. return $data;
  66. }
  67. }