|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\ConfigurableProduct\Model; |
| 8 | + |
| 9 | +use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; |
| 10 | +use Magento\Framework\App\ScopeResolverInterface; |
| 11 | +use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute; |
| 12 | +use Magento\Framework\App\ScopeInterface; |
| 13 | +use Magento\Framework\DB\Select; |
| 14 | +use Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionProvider; |
| 15 | + |
| 16 | +class AttributeOptionProvider implements AttributeOptionProviderInterface |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var ScopeResolverInterface |
| 20 | + */ |
| 21 | + private $scopeResolver; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var Attribute |
| 25 | + */ |
| 26 | + private $attributeResource; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var OptionProvider |
| 30 | + */ |
| 31 | + private $attributeOptionProvider; |
| 32 | + |
| 33 | + /** |
| 34 | + * @param Attribute $attributeResource |
| 35 | + * @param OptionProvider $attributeOptionProvider , |
| 36 | + * @param ScopeResolverInterface $scopeResolver |
| 37 | + */ |
| 38 | + public function __construct( |
| 39 | + Attribute $attributeResource, |
| 40 | + OptionProvider $attributeOptionProvider, |
| 41 | + ScopeResolverInterface $scopeResolver |
| 42 | + ) { |
| 43 | + $this->attributeResource = $attributeResource; |
| 44 | + $this->attributeOptionProvider = $attributeOptionProvider; |
| 45 | + $this->scopeResolver = $scopeResolver; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * {@inheritdoc} |
| 50 | + */ |
| 51 | + public function getAttributeOptions(AbstractAttribute $superAttribute, $productId) |
| 52 | + { |
| 53 | + $scope = $this->scopeResolver->getScope(); |
| 54 | + $select = $this->getAttributeOptionsSelect($superAttribute, $productId, $scope); |
| 55 | + |
| 56 | + $data = $this->attributeResource->getConnection()->fetchAll($select); |
| 57 | + if ($superAttribute->getSourceModel()) { |
| 58 | + foreach ($data as $key => $value) { |
| 59 | + $optionText = $superAttribute->getSource()->getOptionText($value['value_index']); |
| 60 | + $data[$key]['default_title'] = $optionText; |
| 61 | + $data[$key]['option_title'] = $optionText; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return $data; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Get load options for attribute select |
| 70 | + * |
| 71 | + * @param AbstractAttribute $superAttribute |
| 72 | + * @param int $productId |
| 73 | + * @param ScopeInterface $scope |
| 74 | + * @return Select |
| 75 | + */ |
| 76 | + private function getAttributeOptionsSelect(AbstractAttribute $superAttribute, $productId, ScopeInterface $scope) |
| 77 | + { |
| 78 | + $select = $this->attributeResource->getConnection()->select()->from( |
| 79 | + ['super_attribute' => $this->attributeResource->getTable('catalog_product_super_attribute')], |
| 80 | + [ |
| 81 | + 'sku' => 'entity.sku', |
| 82 | + 'product_id' => 'product_entity.entity_id', |
| 83 | + 'attribute_code' => 'attribute.attribute_code', |
| 84 | + 'value_index' => 'entity_value.value', |
| 85 | + 'super_attribute_label' => 'attribute_label.value', |
| 86 | + ] |
| 87 | + )->joinInner( |
| 88 | + ['product_entity' => $this->attributeResource->getTable('catalog_product_entity')], |
| 89 | + "product_entity.{$this->attributeOptionProvider->getProductEntityLinkField()} = super_attribute.product_id", |
| 90 | + [] |
| 91 | + )->joinInner( |
| 92 | + ['product_link' => $this->attributeResource->getTable('catalog_product_super_link')], |
| 93 | + 'product_link.parent_id = super_attribute.product_id', |
| 94 | + [] |
| 95 | + )->joinInner( |
| 96 | + ['attribute' => $this->attributeResource->getTable('eav_attribute')], |
| 97 | + 'attribute.attribute_id = super_attribute.attribute_id', |
| 98 | + [] |
| 99 | + )->joinInner( |
| 100 | + ['entity' => $this->attributeResource->getTable('catalog_product_entity')], |
| 101 | + 'entity.entity_id = product_link.product_id', |
| 102 | + [] |
| 103 | + )->joinInner( |
| 104 | + ['entity_value' => $superAttribute->getBackendTable()], |
| 105 | + implode( |
| 106 | + ' AND ', |
| 107 | + [ |
| 108 | + 'entity_value.attribute_id = super_attribute.attribute_id', |
| 109 | + 'entity_value.store_id = 0', |
| 110 | + "entity_value.{$this->attributeOptionProvider->getProductEntityLinkField()} = " |
| 111 | + . "entity.{$this->attributeOptionProvider->getProductEntityLinkField()}", |
| 112 | + ] |
| 113 | + ), |
| 114 | + [] |
| 115 | + )->joinLeft( |
| 116 | + ['attribute_label' => $this->attributeResource->getTable('catalog_product_super_attribute_label')], |
| 117 | + implode( |
| 118 | + ' AND ', |
| 119 | + [ |
| 120 | + 'super_attribute.product_super_attribute_id = attribute_label.product_super_attribute_id', |
| 121 | + 'attribute_label.store_id = ' . \Magento\Store\Model\Store::DEFAULT_STORE_ID, |
| 122 | + ] |
| 123 | + ), |
| 124 | + [] |
| 125 | + )->where( |
| 126 | + 'super_attribute.product_id = ?', |
| 127 | + $productId |
| 128 | + )->where( |
| 129 | + 'attribute.attribute_id = ?', |
| 130 | + $superAttribute->getAttributeId() |
| 131 | + ); |
| 132 | + |
| 133 | + if (!$superAttribute->getSourceModel()) { |
| 134 | + $select->columns( |
| 135 | + [ |
| 136 | + 'option_title' => $this->attributeResource->getConnection()->getIfNullSql( |
| 137 | + 'option_value.value', |
| 138 | + 'default_option_value.value' |
| 139 | + ), |
| 140 | + 'default_title' => 'default_option_value.value', |
| 141 | + ] |
| 142 | + )->joinLeft( |
| 143 | + ['option_value' => $this->attributeResource->getTable('eav_attribute_option_value')], |
| 144 | + implode( |
| 145 | + ' AND ', |
| 146 | + [ |
| 147 | + 'option_value.option_id = entity_value.value', |
| 148 | + 'option_value.store_id = ' . $scope->getId(), |
| 149 | + ] |
| 150 | + ), |
| 151 | + [] |
| 152 | + )->joinLeft( |
| 153 | + ['default_option_value' => $this->attributeResource->getTable('eav_attribute_option_value')], |
| 154 | + implode( |
| 155 | + ' AND ', |
| 156 | + [ |
| 157 | + 'default_option_value.option_id = entity_value.value', |
| 158 | + 'default_option_value.store_id = ' . \Magento\Store\Model\Store::DEFAULT_STORE_ID, |
| 159 | + ] |
| 160 | + ), |
| 161 | + [] |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + return $select; |
| 166 | + } |
| 167 | +} |
0 commit comments