/
Grouped.php
132 lines (123 loc) · 4.56 KB
/
Grouped.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* Grouped Products Price Indexer Resource model
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\GroupedProduct\Model\ResourceModel\Product\Indexer\Price;
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\DefaultPrice;
use Magento\Catalog\Api\Data\ProductInterface;
class Grouped extends DefaultPrice implements GroupedInterface
{
/**
* Reindex temporary (price result data) for all products
*
* @throws \Exception
* @return \Magento\GroupedProduct\Model\ResourceModel\Product\Indexer\Price\Grouped
*/
public function reindexAll()
{
$this->tableStrategy->setUseIdxTable(true);
$this->beginTransaction();
try {
$this->_prepareGroupedProductPriceData();
$this->commit();
} catch (\Exception $e) {
$this->rollBack();
throw $e;
}
return $this;
}
/**
* Reindex temporary (price result data) for defined product(s)
*
* @param int|array $entityIds
* @return \Magento\GroupedProduct\Model\ResourceModel\Product\Indexer\Price\Grouped
*/
public function reindexEntity($entityIds)
{
$this->_prepareGroupedProductPriceData($entityIds);
return $this;
}
/**
* Calculate minimal and maximal prices for Grouped products
* Use calculated price for relation products
*
* @param int|array $entityIds the parent entity ids limitation
* @return \Magento\GroupedProduct\Model\ResourceModel\Product\Indexer\Price\Grouped
*/
protected function _prepareGroupedProductPriceData($entityIds = null)
{
if (!$this->hasEntity() && empty($entityIds)) {
return $this;
}
$connection = $this->getConnection();
$table = $this->getIdxTable();
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
$select = $connection->select()->from(
['e' => $this->getTable('catalog_product_entity')],
'entity_id'
)->joinLeft(
['l' => $this->getTable('catalog_product_link')],
'e.' . $linkField . ' = l.product_id AND l.link_type_id=' .
\Magento\GroupedProduct\Model\ResourceModel\Product\Link::LINK_TYPE_GROUPED,
[]
)->join(
['cg' => $this->getTable('customer_group')],
'',
['customer_group_id']
);
$this->_addWebsiteJoinToSelect($select, true);
$this->_addProductWebsiteJoinToSelect($select, 'cw.website_id', 'e.entity_id');
$minCheckSql = $connection->getCheckSql('le.required_options = 0', 'i.min_price', 0);
$maxCheckSql = $connection->getCheckSql('le.required_options = 0', 'i.max_price', 0);
$select->columns(
'website_id',
'cw'
)->joinLeft(
['le' => $this->getTable('catalog_product_entity')],
'le.entity_id = l.linked_product_id',
[]
)->joinLeft(
['i' => $table],
'i.entity_id = l.linked_product_id AND i.website_id = cw.website_id' .
' AND i.customer_group_id = cg.customer_group_id',
[
'tax_class_id' => $this->getConnection()->getCheckSql(
'MIN(i.tax_class_id) IS NULL',
'0',
'MIN(i.tax_class_id)'
),
'price' => new \Zend_Db_Expr('NULL'),
'final_price' => new \Zend_Db_Expr('NULL'),
'min_price' => new \Zend_Db_Expr('MIN(' . $minCheckSql . ')'),
'max_price' => new \Zend_Db_Expr('MAX(' . $maxCheckSql . ')'),
'tier_price' => new \Zend_Db_Expr('NULL'),
]
)->group(
['e.entity_id', 'cg.customer_group_id', 'cw.website_id']
)->where(
'e.type_id=?',
$this->getTypeId()
);
if ($entityIds !== null) {
$select->where('e.entity_id IN(?)', $entityIds);
}
/**
* Add additional external limitation
*/
$this->_eventManager->dispatch(
'catalog_product_prepare_index_select',
[
'select' => $select,
'entity_field' => new \Zend_Db_Expr('e.entity_id'),
'website_field' => new \Zend_Db_Expr('cw.website_id'),
'store_field' => new \Zend_Db_Expr('cs.store_id')
]
);
$query = $select->insertFromSelect($table);
$connection->query($query);
return $this;
}
}