Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StockItemCriteriaMapper.php
Go to the documentation of this file.
1 <?php
8 
14 use Psr\Log\LoggerInterface as Logger;
17 
24 {
28  private $stockConfiguration;
29 
34  private $storeManager;
35 
44  public function __construct(
45  Logger $logger,
49  StoreManagerInterface $storeManager,
50  Select $select = null
51  ) {
52  $this->storeManager = $storeManager;
54  }
55 
59  protected function init()
60  {
61  $this->initResource(\Magento\CatalogInventory\Model\ResourceModel\Stock\Item::class);
62  $this->map['qty'] = ['main_table', 'qty', 'qty'];
63  }
64 
68  public function mapInitialCondition()
69  {
70  $this->getSelect()->join(
71  ['cp_table' => $this->getTable('catalog_product_entity')],
72  'main_table.product_id = cp_table.entity_id',
73  ['type_id']
74  );
75  }
76 
80  public function mapStockFilter($stock)
81  {
82  if ($stock instanceof \Magento\CatalogInventory\Api\Data\StockInterface) {
83  $stock = $stock->getId();
84  }
85  $this->addFieldToFilter('main_table.stock_id', $stock);
86  }
87 
91  public function mapWebsiteFilter($website)
92  {
93  if ($website instanceof \Magento\Store\Model\Website) {
94  $website = $website->getId();
95  }
96  $this->addFieldToFilter('main_table.website_id', $website);
97  }
98 
102  public function mapProductsFilter($products)
103  {
104  $productIds = [];
105  if (!is_array($products)) {
106  $products = [$products];
107  }
108  foreach ($products as $product) {
109  if ($product instanceof \Magento\Catalog\Model\Product) {
110  $productIds[] = $product->getId();
111  } else {
112  $productIds[] = $product;
113  }
114  }
115  if (empty($productIds)) {
116  $productIds[] = false;
117  }
118  $this->addFieldToFilter('main_table.product_id', ['in' => $productIds]);
119  }
120 
125  public function mapStockStatus($storeId = null)
126  {
127  $websiteId = $this->getStockConfiguration()->getDefaultScopeId();
128  $this->getSelect()->joinLeft(
129  ['status_table' => $this->getTable('cataloginventory_stock_status')],
130  'main_table.product_id=status_table.product_id' .
131  ' AND main_table.stock_id=status_table.stock_id' .
132  $this->connection->quoteInto(
133  ' AND status_table.website_id=?',
134  $websiteId
135  ),
136  ['stock_status']
137  );
138  }
139 
143  public function mapManagedFilter($isStockManagedInConfig)
144  {
145  if ($isStockManagedInConfig) {
146  $this->getSelect()->where('(manage_stock = 1 OR use_config_manage_stock = 1)');
147  } else {
148  $this->addFieldToFilter('manage_stock', 1);
149  }
150  }
151 
156  public function mapQtyFilter($comparisonMethod, $qty)
157  {
158  $methods = ['<' => 'lt', '>' => 'gt', '=' => 'eq', '<=' => 'lteq', '>=' => 'gteq', '<>' => 'neq'];
159  if (!isset($methods[$comparisonMethod])) {
160  throw new \Magento\Framework\Exception\LocalizedException(
161  __('%1 is not a correct comparison method.', $comparisonMethod)
162  );
163  }
164  $this->addFieldToFilter('main_table.qty', [$methods[$comparisonMethod] => $qty]);
165  }
166 
172  private function getStockConfiguration()
173  {
174  if ($this->stockConfiguration === null) {
175  $this->stockConfiguration = \Magento\Framework\App\ObjectManager::getInstance()
176  ->get(\Magento\CatalogInventory\Api\StockConfigurationInterface::class);
177  }
178  return $this->stockConfiguration;
179  }
180 }
map(CriteriaInterface $criteria)
addFieldToFilter($field, $condition=null)
$storeManager
__()
Definition: __.php:13
$methods
Definition: billing.phtml:71
__construct(Logger $logger, FetchStrategyInterface $fetchStrategy, ObjectFactory $objectFactory, MapperFactory $mapperFactory, StoreManagerInterface $storeManager, Select $select=null)
$stock