LowestPriceOptionsProvider.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ConfigurableProduct\Pricing\Price;
  7. use Magento\Catalog\Api\Data\ProductInterface;
  8. use Magento\Catalog\Model\ResourceModel\Product\LinkedProductSelectBuilderInterface;
  9. use Magento\Framework\App\ResourceConnection;
  10. use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
  11. use Magento\Store\Model\StoreManagerInterface;
  12. /**
  13. * Retrieve list of products where each product contains lower price than others at least for one possible price type
  14. */
  15. class LowestPriceOptionsProvider implements LowestPriceOptionsProviderInterface
  16. {
  17. /**
  18. * @var ResourceConnection
  19. */
  20. private $resource;
  21. /**
  22. * @var LinkedProductSelectBuilderInterface
  23. */
  24. private $linkedProductSelectBuilder;
  25. /**
  26. * @var CollectionFactory
  27. */
  28. private $collectionFactory;
  29. /**
  30. * @var StoreManagerInterface
  31. */
  32. private $storeManager;
  33. /**
  34. * Key is product id and store id. Value is array of prepared linked products
  35. *
  36. * @var array
  37. */
  38. private $linkedProductMap;
  39. /**
  40. * @param ResourceConnection $resourceConnection
  41. * @param LinkedProductSelectBuilderInterface $linkedProductSelectBuilder
  42. * @param CollectionFactory $collectionFactory
  43. * @param StoreManagerInterface $storeManager
  44. */
  45. public function __construct(
  46. ResourceConnection $resourceConnection,
  47. LinkedProductSelectBuilderInterface $linkedProductSelectBuilder,
  48. CollectionFactory $collectionFactory,
  49. StoreManagerInterface $storeManager
  50. ) {
  51. $this->resource = $resourceConnection;
  52. $this->linkedProductSelectBuilder = $linkedProductSelectBuilder;
  53. $this->collectionFactory = $collectionFactory;
  54. $this->storeManager = $storeManager;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getProducts(ProductInterface $product)
  60. {
  61. $key = $this->storeManager->getStore()->getId() . '-' . $product->getId();
  62. if (!isset($this->linkedProductMap[$key])) {
  63. $productIds = $this->resource->getConnection()->fetchCol(
  64. '(' . implode(') UNION (', $this->linkedProductSelectBuilder->build($product->getId())) . ')'
  65. );
  66. $this->linkedProductMap[$key] = $this->collectionFactory->create()
  67. ->addAttributeToSelect(
  68. ['price', 'special_price', 'special_from_date', 'special_to_date', 'tax_class_id']
  69. )
  70. ->addIdFilter($productIds)
  71. ->getItems();
  72. }
  73. return $this->linkedProductMap[$key];
  74. }
  75. }