Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RuleProductsSelectBuilder.php
Go to the documentation of this file.
1 <?php
8 
12 
17 {
21  private $resource;
22 
26  private $eavConfig;
27 
31  private $storeManager;
32 
36  private $metadataPool;
37 
41  private $activeTableSwitcher;
42 
46  private $tableSwapper;
47 
56  public function __construct(
57  \Magento\Framework\App\ResourceConnection $resource,
58  \Magento\Eav\Model\Config $eavConfig,
59  \Magento\Store\Model\StoreManagerInterface $storeManager,
60  \Magento\Framework\EntityManager\MetadataPool $metadataPool,
61  ActiveTableSwitcher $activeTableSwitcher,
62  TableSwapper $tableSwapper = null
63  ) {
64  $this->eavConfig = $eavConfig;
65  $this->storeManager = $storeManager;
66  $this->metadataPool = $metadataPool;
67  $this->resource = $resource;
68  $this->activeTableSwitcher = $activeTableSwitcher;
69  $this->tableSwapper = $tableSwapper ??
70  ObjectManager::getInstance()->get(TableSwapper::class);
71  }
72 
81  public function build(
82  $websiteId,
83  \Magento\Catalog\Model\Product $product = null,
84  $useAdditionalTable = false
85  ) {
86  $connection = $this->resource->getConnection();
87  $indexTable = $this->resource->getTableName('catalogrule_product');
88  if ($useAdditionalTable) {
89  $indexTable = $this->resource->getTableName(
90  $this->tableSwapper->getWorkingTableName('catalogrule_product')
91  );
92  }
93 
104  $select = $connection->select()->from(
105  ['rp' => $indexTable]
106  )->order(
107  ['rp.website_id', 'rp.customer_group_id', 'rp.product_id', 'rp.sort_order', 'rp.rule_id']
108  );
109 
110  if ($product && $product->getEntityId()) {
111  $select->where('rp.product_id=?', $product->getEntityId());
112  }
113 
117  $priceAttr = $this->eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, 'price');
118  $priceTable = $priceAttr->getBackend()->getTable();
119  $attributeId = $priceAttr->getId();
120 
121  $linkField = $this->metadataPool
122  ->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class)
123  ->getLinkField();
124  $select->join(
125  ['e' => $this->resource->getTableName('catalog_product_entity')],
126  sprintf('e.entity_id = rp.product_id'),
127  []
128  );
129  $joinCondition = '%1$s.' . $linkField . '=e.' . $linkField . ' AND (%1$s.attribute_id='
130  . $attributeId
131  . ') and %1$s.store_id=%2$s';
132 
133  $select->join(
134  ['pp_default' => $priceTable],
135  sprintf($joinCondition, 'pp_default', \Magento\Store\Model\Store::DEFAULT_STORE_ID),
136  []
137  );
138 
139  $website = $this->storeManager->getWebsite($websiteId);
140  $defaultGroup = $website->getDefaultGroup();
141  if ($defaultGroup instanceof \Magento\Store\Model\Group) {
142  $storeId = $defaultGroup->getDefaultStoreId();
143  } else {
145  }
146 
147  $select->joinInner(
148  ['product_website' => $this->resource->getTableName('catalog_product_website')],
149  'product_website.product_id=rp.product_id '
150  . 'AND product_website.website_id = rp.website_id '
151  . 'AND product_website.website_id='
152  . $websiteId,
153  []
154  );
155 
156  $tableAlias = 'pp' . $websiteId;
157  $select->joinLeft(
158  [$tableAlias => $priceTable],
159  sprintf($joinCondition, $tableAlias, $storeId),
160  []
161  );
162  $select->columns([
163  'default_price' => $connection->getIfNullSql($tableAlias . '.value', 'pp_default.value'),
164  ]);
165 
166  return $connection->query($select);
167  }
168 }
$storeManager
$resource
Definition: bulk.php:12
__construct(\Magento\Framework\App\ResourceConnection $resource, \Magento\Eav\Model\Config $eavConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\EntityManager\MetadataPool $metadataPool, ActiveTableSwitcher $activeTableSwitcher, TableSwapper $tableSwapper=null)
build( $websiteId, \Magento\Catalog\Model\Product $product=null, $useAdditionalTable=false)
$connection
Definition: bulk.php:13