Esempio n. 1
0
 /**
  * Add join for catalog in stock field to product collection
  *
  * @param \Magento\Catalog\Model\Resource\Product\Collection $productCollection
  * @param array $columns
  * @return $this
  */
 public function addCatalogInventoryToProductCollection($productCollection, $columns = null)
 {
     if ($columns === null) {
         $adapter = $this->_getReadAdapter();
         $isManageStock = (int) $this->_scopeConfig->getValue(\Magento\CatalogInventory\Model\Stock\Item::XML_PATH_MANAGE_STOCK, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
         $stockExpr = $adapter->getCheckSql('cisi.use_config_manage_stock = 1', $isManageStock, 'cisi.manage_stock');
         $stockExpr = $adapter->getCheckSql("({$stockExpr} = 1)", 'cisi.is_in_stock', '1');
         $columns = array('is_saleable' => new \Zend_Db_Expr($stockExpr), 'inventory_in_stock' => 'is_in_stock');
     }
     $productCollection->joinTable(array('cisi' => 'cataloginventory_stock_item'), 'product_id=entity_id', $columns, null, 'left');
     return $this;
 }
Esempio n. 2
0
 /**
  * Add low stock filter to product collection
  *
  * @param \Magento\Catalog\Model\Resource\Product\Collection $collection
  * @param array $fields
  * @return $this
  */
 public function addLowStockFilter(\Magento\Catalog\Model\Resource\Product\Collection $collection, $fields)
 {
     $this->_initConfig();
     $adapter = $collection->getSelect()->getAdapter();
     $qtyIf = $adapter->getCheckSql('invtr.use_config_notify_stock_qty > 0', $this->_configNotifyStockQty, 'invtr.notify_stock_qty');
     $conditions = [[$adapter->prepareSqlCondition('invtr.use_config_manage_stock', 1), $adapter->prepareSqlCondition($this->_isConfigManageStock, 1), $adapter->prepareSqlCondition('invtr.qty', ['lt' => $qtyIf])], [$adapter->prepareSqlCondition('invtr.use_config_manage_stock', 0), $adapter->prepareSqlCondition('invtr.manage_stock', 1)]];
     $where = [];
     foreach ($conditions as $k => $part) {
         $where[$k] = join(' ' . \Zend_Db_Select::SQL_AND . ' ', $part);
     }
     $where = $adapter->prepareSqlCondition('invtr.low_stock_date', ['notnull' => true]) . ' ' . \Zend_Db_Select::SQL_AND . ' ((' . join(') ' . \Zend_Db_Select::SQL_OR . ' (', $where) . '))';
     $collection->joinTable(['invtr' => 'cataloginventory_stock_item'], 'product_id = entity_id', $fields, $where);
     return $this;
 }