OptionSelectBuilder.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ConfigurableProduct\Model\ResourceModel\Attribute;
  7. use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute;
  8. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  9. use Magento\Framework\App\ScopeInterface;
  10. use Magento\Framework\DB\Select;
  11. /**
  12. * Build select object for retrieving configurable options.
  13. */
  14. class OptionSelectBuilder implements OptionSelectBuilderInterface
  15. {
  16. /**
  17. * Configurable Attribute Resource Model.
  18. *
  19. * @var Attribute
  20. */
  21. private $attributeResource;
  22. /**
  23. * Option Provider.
  24. *
  25. * @var OptionProvider
  26. */
  27. private $attributeOptionProvider;
  28. /**
  29. * @param Attribute $attributeResource
  30. * @param OptionProvider $attributeOptionProvider
  31. */
  32. public function __construct(Attribute $attributeResource, OptionProvider $attributeOptionProvider)
  33. {
  34. $this->attributeResource = $attributeResource;
  35. $this->attributeOptionProvider = $attributeOptionProvider;
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function getSelect(AbstractAttribute $superAttribute, int $productId, ScopeInterface $scope)
  41. {
  42. $select = $this->attributeResource->getConnection()->select()->from(
  43. ['super_attribute' => $this->attributeResource->getTable('catalog_product_super_attribute')],
  44. [
  45. 'sku' => 'entity.sku',
  46. 'product_id' => 'product_entity.entity_id',
  47. 'attribute_code' => 'attribute.attribute_code',
  48. 'value_index' => 'entity_value.value',
  49. 'super_attribute_label' => 'attribute_label.value',
  50. ]
  51. )->joinInner(
  52. ['product_entity' => $this->attributeResource->getTable('catalog_product_entity')],
  53. "product_entity.{$this->attributeOptionProvider->getProductEntityLinkField()} = super_attribute.product_id",
  54. []
  55. )->joinInner(
  56. ['product_link' => $this->attributeResource->getTable('catalog_product_super_link')],
  57. 'product_link.parent_id = super_attribute.product_id',
  58. []
  59. )->joinInner(
  60. ['attribute' => $this->attributeResource->getTable('eav_attribute')],
  61. 'attribute.attribute_id = super_attribute.attribute_id',
  62. []
  63. )->joinInner(
  64. ['entity' => $this->attributeResource->getTable('catalog_product_entity')],
  65. 'entity.entity_id = product_link.product_id',
  66. []
  67. )->joinInner(
  68. ['entity_value' => $superAttribute->getBackendTable()],
  69. implode(
  70. ' AND ',
  71. [
  72. 'entity_value.attribute_id = super_attribute.attribute_id',
  73. 'entity_value.store_id = 0',
  74. "entity_value.{$this->attributeOptionProvider->getProductEntityLinkField()} = "
  75. . "entity.{$this->attributeOptionProvider->getProductEntityLinkField()}",
  76. ]
  77. ),
  78. []
  79. )->joinLeft(
  80. ['attribute_label' => $this->attributeResource->getTable('catalog_product_super_attribute_label')],
  81. implode(
  82. ' AND ',
  83. [
  84. 'super_attribute.product_super_attribute_id = attribute_label.product_super_attribute_id',
  85. 'attribute_label.store_id = ' . \Magento\Store\Model\Store::DEFAULT_STORE_ID,
  86. ]
  87. ),
  88. []
  89. )->joinLeft(
  90. ['attribute_option' => $this->attributeResource->getTable('eav_attribute_option')],
  91. 'attribute_option.option_id = entity_value.value',
  92. []
  93. )->order(
  94. 'attribute_option.sort_order ASC'
  95. )->where(
  96. 'super_attribute.product_id = ?',
  97. $productId
  98. )->where(
  99. 'attribute.attribute_id = ?',
  100. $superAttribute->getAttributeId()
  101. );
  102. if (!$superAttribute->getSourceModel()) {
  103. $select->columns(
  104. [
  105. 'option_title' => $this->attributeResource->getConnection()->getIfNullSql(
  106. 'option_value.value',
  107. 'default_option_value.value'
  108. ),
  109. 'default_title' => 'default_option_value.value',
  110. ]
  111. )->joinLeft(
  112. ['option_value' => $this->attributeResource->getTable('eav_attribute_option_value')],
  113. implode(
  114. ' AND ',
  115. [
  116. 'option_value.option_id = entity_value.value',
  117. 'option_value.store_id = ' . $scope->getId(),
  118. ]
  119. ),
  120. []
  121. )->joinLeft(
  122. ['default_option_value' => $this->attributeResource->getTable('eav_attribute_option_value')],
  123. implode(
  124. ' AND ',
  125. [
  126. 'default_option_value.option_id = entity_value.value',
  127. 'default_option_value.store_id = ' . \Magento\Store\Model\Store::DEFAULT_STORE_ID,
  128. ]
  129. ),
  130. []
  131. );
  132. }
  133. return $select;
  134. }
  135. }