ApplyStatusAttributeJoin.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryLowQuantityNotification\Model\ResourceModel\Rss\NotifyStock;
  8. use Magento\Catalog\Api\Data\ProductInterface;
  9. use Magento\Catalog\Model\Product;
  10. use Magento\Catalog\Model\Product\Attribute\Source\Status;
  11. use Magento\Eav\Model\Config as EavConfig;
  12. use Magento\Framework\App\ResourceConnection;
  13. use Magento\Framework\DB\Select;
  14. use Magento\Framework\EntityManager\MetadataPool;
  15. use Magento\Store\Model\Store;
  16. use Magento\Store\Model\StoreManagerInterface;
  17. /**
  18. * Add product attribute 'status' to select.
  19. */
  20. class ApplyStatusAttributeJoin
  21. {
  22. /**
  23. * @var Status
  24. */
  25. private $productStatus;
  26. /**
  27. * @var ResourceConnection
  28. */
  29. private $resourceConnection;
  30. /**
  31. * @var EavConfig
  32. */
  33. private $eavConfig;
  34. /**
  35. * @var StoreManagerInterface
  36. */
  37. private $storeManager;
  38. /**
  39. * @var MetadataPool
  40. */
  41. private $metadataPool;
  42. /**
  43. * @param Status $productStatus
  44. * @param ResourceConnection $resourceConnection
  45. * @param EavConfig $eavConfig
  46. * @param StoreManagerInterface $storeManager
  47. * @param MetadataPool $metadataPool
  48. */
  49. public function __construct(
  50. Status $productStatus,
  51. ResourceConnection $resourceConnection,
  52. EavConfig $eavConfig,
  53. StoreManagerInterface $storeManager,
  54. MetadataPool $metadataPool
  55. ) {
  56. $this->productStatus = $productStatus;
  57. $this->resourceConnection = $resourceConnection;
  58. $this->eavConfig = $eavConfig;
  59. $this->storeManager = $storeManager;
  60. $this->metadataPool = $metadataPool;
  61. }
  62. /**
  63. * @param Select $select
  64. *
  65. * @return void
  66. */
  67. public function execute(Select $select)
  68. {
  69. $storeId = $this->storeManager->getStore()->getId();
  70. $connection = $this->resourceConnection->getConnection();
  71. $valueCondition = 'at_status.value';
  72. $tableName = $this->resourceConnection->getTableName('catalog_product_entity_int');
  73. if ($storeId != Store::DEFAULT_STORE_ID) {
  74. $select->join(
  75. ['at_status_default' => $tableName],
  76. $this->getConditionByAliasAndStoreId(Store::DEFAULT_STORE_ID, 'at_status_default'),
  77. []
  78. );
  79. $valueCondition = $connection->getCheckSql(
  80. 'at_status.value_id > 0',
  81. 'at_status.value',
  82. 'at_status_default.value'
  83. );
  84. }
  85. $select->joinLeft(
  86. ['at_status' => $tableName],
  87. $this->getConditionByAliasAndStoreId((int)$storeId, 'at_status'),
  88. ['status' => $valueCondition]
  89. );
  90. }
  91. /**
  92. * @param int $storeId
  93. * @param string $alias
  94. *
  95. * @return string
  96. */
  97. private function getConditionByAliasAndStoreId(int $storeId, string $alias): string
  98. {
  99. $metadata = $this->metadataPool->getMetadata(ProductInterface::class);
  100. $linkField = $metadata->getLinkField();
  101. $attributeId = $this->eavConfig->getAttribute(Product::ENTITY, ProductInterface::STATUS)->getAttributeId();
  102. $connection = $this->resourceConnection->getConnection();
  103. $statusVisibilityCondition = $connection->prepareSqlCondition(
  104. $alias . '.value',
  105. ['in' => $this->productStatus->getVisibleStatusIds()]
  106. );
  107. return implode(
  108. [
  109. $alias . '.' . $linkField . ' = product.' . $linkField,
  110. $statusVisibilityCondition,
  111. $connection->prepareSqlCondition($alias . '.store_id', $storeId),
  112. $connection->prepareSqlCondition($alias . '.attribute_id', $attributeId),
  113. ],
  114. ' ' . Select::SQL_AND . ' '
  115. );
  116. }
  117. }