Get on Sales Product between dates Magento2
Referene Class is : {magento root}\vendor\magento\module-catalog-widget\Block\Product\ProductsList.php
/** * Get end of day date * * @return string */ public function getEndOfDayDate() { $date = $this->_localeDate->date()->setTime(23, 59, 59)->format('Y-m-d H:i:s'); return $date; } /** * Prepare and return product collection * * @return Collection * @SuppressWarnings(PHPMD.RequestAwareBlockMethod) * @throws LocalizedException */ public function createCollection() { /** @var $collection Collection */ $collection = $this->productCollectionFactory->create(); if ($this->getData('store_id') !== null) { $collection->setStoreId($this->getData('store_id')); } $collection->setVisibility($this->catalogProductVisibility->getVisibleInCatalogIds()); $collection = $this->_addProductAttributesAndPrices($collection) ->addStoreFilter() ->addAttributeToSort('entity_id', 'desc'); $collection->addAttributeToFilter( 'special_from_date', ['date' => true, 'to' => $this->getEndOfDayDate()], 'left' )->addAttributeToFilter( 'special_to_date', ['or' => [0 => ['date' => true, 'from' => $this->getStartOfDayDate()], 1 => ['is' => new \Zend_Db_Expr( 'null' )],]], 'left' )->addAttributeToSort( 'news_from_date', 'desc' ); $collection->setPageSize($this->getPageSize()) ->setCurPage($this->getRequest()->getParam($this->getData('page_var_name'), 1)); $conditions = $this->getConditions(); $conditions->collectValidatedAttributes($collection); $this->sqlBuilder->attachConditionToCollection($collection, $conditions); /** * Prevent retrieval of duplicate records. This may occur when multiselect product attribute matches * several allowed values from condition simultaneously */ $collection->distinct(true); return $collection; } /** * Get best selling products * * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection * * @return \Magento\Catalog\Model\ResourceModel\Product\Collection */ protected function _getBestsellersProducts($collection) { $collection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds()); $collection = $this->_addProductAttributesAndPrices($collection); $collection->getSelect() ->join(['bestsellers' => $collection->getTable('sales_bestsellers_aggregated_yearly')], 'e.entity_id = bestsellers.product_id AND bestsellers.store_id = '.$this->getStoreId(), ['qty_ordered','rating_pos']) ->order('rating_pos'); $collection->addStoreFilter($this->getStoreId()) ->setPageSize($this->getProductsCount()) ->setCurPage(1); return $collection; }