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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\ConfigurableProduct\Pricing\Price;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\ResourceModel\Product\LinkedProductSelectBuilderInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Store\Model\StoreManagerInterface;
/**
* Retrieve list of products where each product contains lower price than others at least for one possible price type
*/
class LowestPriceOptionsProvider implements LowestPriceOptionsProviderInterface
{
/**
* @var ResourceConnection
*/
private $resource;
/**
* @var LinkedProductSelectBuilderInterface
*/
private $linkedProductSelectBuilder;
/**
* @var CollectionFactory
*/
private $collectionFactory;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* Key is product id and store id. Value is array of prepared linked products
*
* @var array
*/
private $linkedProductMap;
/**
* @param ResourceConnection $resourceConnection
* @param LinkedProductSelectBuilderInterface $linkedProductSelectBuilder
* @param CollectionFactory $collectionFactory
* @param StoreManagerInterface $storeManager
*/
public function __construct(
ResourceConnection $resourceConnection,
LinkedProductSelectBuilderInterface $linkedProductSelectBuilder,
CollectionFactory $collectionFactory,
StoreManagerInterface $storeManager
) {
$this->resource = $resourceConnection;
$this->linkedProductSelectBuilder = $linkedProductSelectBuilder;
$this->collectionFactory = $collectionFactory;
$this->storeManager = $storeManager;
}
/**
* {@inheritdoc}
*/
public function getProducts(ProductInterface $product)
{
$key = $this->storeManager->getStore()->getId() . '-' . $product->getId();
if (!isset($this->linkedProductMap[$key])) {
$productIds = $this->resource->getConnection()->fetchCol(
'(' . implode(') UNION (', $this->linkedProductSelectBuilder->build($product->getId())) . ')'
);
$this->linkedProductMap[$key] = $this->collectionFactory->create()
->addAttributeToSelect(
['price', 'special_price', 'special_from_date', 'special_to_date', 'tax_class_id']
)
->addIdFilter($productIds)
->getItems();
}
return $this->linkedProductMap[$key];
}
}