daily pastebin goal
35%
SHARE
TWEET

Untitled

a guest Feb 20th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. $collection = $this->productCollectionFactory->create();
  4. $collection->setVisibility($this->catalogProductVisibility->getVisibleInCatalogIds());
  5. $collection = $this->_addProductAttributesAndPrices($collection);
  6.  
  7. return $collection;
  8.      
  9. $collection = $this->productCollectionFactory->create();
  10. $collection->setVisibility($this->catalogProductVisibility->getVisibleInCatalogIds());
  11. $collection = $this->_addProductAttributesAndPrices($collection);
  12. $this->stockFilter->addInStockFilterToCollection($collection);
  13.      
  14. protected $_linkFactory;
  15.  
  16. public function __construct(
  17.     MagentoCatalogModelProductLinkFactory $linkFactory
  18. ) {
  19.     $this->_linkFactory = $linkFactory;
  20. }
  21.      
  22. $collection = $this->_linkFactory->create()
  23.         ->getProductCollection()->addAttributeToSelect('*')
  24.         ->addAttributeToFilter(
  25.             'status', array('eq' => MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED)
  26.         )
  27.     ;
  28.  
  29.     $collection->joinField(
  30.         'qty', 'cataloginventory_stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left'
  31.     );
  32.      
  33. /**
  34.  * @var MagentoCatalogInventoryHelperStock
  35.  */
  36. protected $_stockFilter;
  37.      
  38. public function __construct(
  39.     .....
  40.      MagentoCatalogInventoryHelperStock $stockFilter
  41.     .....
  42. ) {
  43.      $this->_stockFilter = $stockFilter;
  44. }
  45.      
  46. public function getProducts(){
  47. ..........
  48.  $this->_stockFilter->addInStockFilterToCollection($collection);
  49. ..........
  50. }
  51.      
  52. addInStockFilterToCollection($collection)
  53.      
  54. addIsInStockFilterToCollection($collection)
  55.      
  56. /**
  57.      * @param MagentoCatalogModelResourceModelProductCollection $collection
  58.      * @param bool $isFilterInStock
  59.      * @return MagentoCatalogModelResourceModelProductCollection $collection
  60.      */
  61.     public function addStockDataToCollection($collection, $isFilterInStock)
  62.     {
  63.         $websiteId = $this->getStockConfiguration()->getDefaultScopeId();
  64.         $joinCondition = $this->getConnection()->quoteInto(
  65.             'e.entity_id = stock_status_index.product_id' . ' AND stock_status_index.website_id = ?',
  66.             $websiteId
  67.         );
  68.  
  69.         $joinCondition .= $this->getConnection()->quoteInto(
  70.             ' AND stock_status_index.stock_id = ?',
  71.             Stock::DEFAULT_STOCK_ID
  72.         );
  73.         $method = $isFilterInStock ? 'join' : 'joinLeft';
  74.         $collection->getSelect()->$method(
  75.             ['stock_status_index' => $this->getMainTable()],
  76.             $joinCondition,
  77.             ['is_salable' => 'stock_status']
  78.         );
  79.  
  80.         if ($isFilterInStock) {
  81.             $collection->getSelect()->where(
  82.                 'stock_status_index.stock_status = ?',
  83.                 StockStatus::STATUS_IN_STOCK
  84.             );
  85.         }
  86.         return $collection;
  87.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top