ApplyNameAttributeJoin.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\Eav\Model\Config as EavConfig;
  11. use Magento\Framework\App\ResourceConnection;
  12. use Magento\Framework\DB\Select;
  13. use Magento\Framework\EntityManager\MetadataPool;
  14. use Magento\Store\Model\Store;
  15. use Magento\Store\Model\StoreManagerInterface;
  16. /**
  17. * Add product attribute 'name' to select.
  18. */
  19. class ApplyNameAttributeJoin
  20. {
  21. /**
  22. * @var ResourceConnection
  23. */
  24. private $resourceConnection;
  25. /**
  26. * @var EavConfig
  27. */
  28. private $eavConfig;
  29. /**
  30. * @var StoreManagerInterface
  31. */
  32. private $storeManager;
  33. /**
  34. * @var MetadataPool
  35. */
  36. private $metadataPool;
  37. /**
  38. * @param ResourceConnection $resourceConnection
  39. * @param EavConfig $eavConfig
  40. * @param StoreManagerInterface $storeManager
  41. * @param MetadataPool $metadataPool
  42. */
  43. public function __construct(
  44. ResourceConnection $resourceConnection,
  45. EavConfig $eavConfig,
  46. StoreManagerInterface $storeManager,
  47. MetadataPool $metadataPool
  48. ) {
  49. $this->resourceConnection = $resourceConnection;
  50. $this->storeManager = $storeManager;
  51. $this->eavConfig = $eavConfig;
  52. $this->metadataPool = $metadataPool;
  53. }
  54. /**
  55. * @param Select $select
  56. *
  57. * @return void
  58. */
  59. public function execute(Select $select)
  60. {
  61. $storeId = $this->storeManager->getStore()->getId();
  62. $connection = $this->resourceConnection->getConnection();
  63. $valueCondition = 'at_name.value';
  64. $tableName = $this->resourceConnection->getTableName('catalog_product_entity_varchar');
  65. if ($storeId != Store::DEFAULT_STORE_ID) {
  66. $select->join(
  67. ['at_name_default' => $tableName],
  68. $this->getConditionByAliasAndStoreId(Store::DEFAULT_STORE_ID, 'at_name_default'),
  69. []
  70. );
  71. $valueCondition = $connection->getCheckSql(
  72. 'at_name.value_id > 0',
  73. 'at_name.value',
  74. 'at_name_default.value'
  75. );
  76. }
  77. $select->joinLeft(
  78. ['at_name' => $tableName],
  79. $this->getConditionByAliasAndStoreId((int)$storeId, 'at_name'),
  80. ['name' => $valueCondition]
  81. );
  82. }
  83. /**
  84. * @param int $storeId
  85. * @param string $alias
  86. *
  87. * @return string
  88. */
  89. private function getConditionByAliasAndStoreId(int $storeId, string $alias): string
  90. {
  91. $metadata = $this->metadataPool->getMetadata(ProductInterface::class);
  92. $linkField = $metadata->getLinkField();
  93. $attributeId = $this->eavConfig->getAttribute(Product::ENTITY, ProductInterface::NAME)->getAttributeId();
  94. $connection = $this->resourceConnection->getConnection();
  95. return implode(
  96. [
  97. $alias . '.' . $linkField . ' = product.' . $linkField,
  98. $connection->prepareSqlCondition($alias . '.store_id', $storeId),
  99. $connection->prepareSqlCondition($alias . '.attribute_id', $attributeId),
  100. ],
  101. ' ' . Select::SQL_AND . ' '
  102. );
  103. }
  104. }