2

I want to get all sale products (which have specila price and special from date and special to date) in a .phtml file. Which i use in any where in store.specially in list page.

2
  • you want to is_saleable product? Mar 27, 2019 at 13:19
  • which i apply special price special date to 2 form, those products i want. Mar 27, 2019 at 13:21

1 Answer 1

4

create your own block file and get collection below way..

<?php

    namespace Vendor\SaleProducts\Block\Home;

    use Magento\Catalog\Api\CategoryRepositoryInterface;

    class SaleList extends \Magento\Catalog\Block\Product\ListProduct {

        protected $_collection;

        protected $categoryRepository;

        protected $_resource;

        public function __construct(
        \Magento\Catalog\Block\Product\Context $context, 
                \Magento\Framework\Data\Helper\PostHelper $postDataHelper, 
                \Magento\Catalog\Model\Layer\Resolver $layerResolver, 
                CategoryRepositoryInterface $categoryRepository,
                \Magento\Framework\Url\Helper\Data $urlHelper, 
                \Magento\Catalog\Model\ResourceModel\Product\Collection $collection, 
                \Magento\Framework\App\ResourceConnection $resource,
                array $data = []
        ) {
            $this->categoryRepository = $categoryRepository;
            $this->_collection = $collection;
            $this->_resource = $resource;

            parent::__construct($context, $postDataHelper, $layerResolver, $categoryRepository, $urlHelper, $data);
        }

        public function getProducts() {
            $count = $this->getProductCount();
            $category_id = $this->getData("category_id");
            $collection = clone $this->_collection;
            $collection->clear()->getSelect()->reset(\Magento\Framework\DB\Select::WHERE)->reset(\Magento\Framework\DB\Select::ORDER)->reset(\Magento\Framework\DB\Select::LIMIT_COUNT)->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET)->reset(\Magento\Framework\DB\Select::GROUP);

            if(!$category_id) {
                $category_id = $this->_storeManager->getStore()->getRootCategoryId();
            }
            $category = $this->categoryRepository->get($category_id);
            $now = date('Y-m-d');
            if(isset($category) && $category) {
                $collection->addMinimalPrice()
                    ->addFinalPrice()
                    ->addTaxPercents()
                    ->addAttributeToSelect('name')
                    ->addAttributeToSelect('image')
                    ->addAttributeToSelect('small_image')
                    ->addAttributeToSelect('thumbnail')
                    ->addAttributeToSelect('special_from_date')
                    ->addAttributeToSelect('special_to_date')
                    ->addAttributeToFilter('special_price', ['neq' => ''])
                    ->addAttributeToFilter([
                        [
                            'attribute' => 'special_from_date',
                            'lteq' => date('Y-m-d G:i:s', strtotime($now)),
                            'date' => true,
                        ],
                        [
                            'attribute' => 'special_to_date',
                            'gteq' => date('Y-m-d G:i:s', strtotime($now)),
                            'date' => true,
                        ]
                    ])
                    ->addAttributeToFilter('is_saleable', 1, 'left');
            } else {
                $collection->addMinimalPrice()
                    ->addFinalPrice()
                    ->addTaxPercents()
                    ->addAttributeToSelect('name')
                    ->addAttributeToSelect('image')
                    ->addAttributeToSelect('small_image')
                    ->addAttributeToSelect('thumbnail')
                    ->addAttributeToFilter('special_price', ['neq' => ''])
                    ->addAttributeToSelect('special_from_date')
                    ->addAttributeToSelect('special_to_date')
                    ->addAttributeToFilter([
                        [
                            'attribute' => 'special_from_date',
                            'lteq' => date('Y-m-d G:i:s', strtotime($now)),
                            'date' => true,
                        ],
                        [
                            'attribute' => 'special_to_date',
                            'gteq' => date('Y-m-d G:i:s', strtotime($now)),
                            'date' => true,
                        ]
                    ])
                    ->addAttributeToFilter('is_saleable', 1, 'left');
            }

            $collection->getSelect()
                    ->limit($count);

            return $collection;
        }

        public function getLoadedProductCollection() {
            return $this->getProducts();
        }

        public function getProductCount() {
            $limit = $this->getData("product_count");
            if(!$limit)
                $limit = 10;
            return $limit;
        }
    }

Then you can use in the template this:

<?php foreach ($block->getLoadedProductCollection() as $product) : ?>
    <!-- do something with $product -->
<?php endforeach;?>
9
  • Sir ? $logger->debug('query is '. $collection->getSelect()->__toString()); it return a query in debug.log file. and i past this query into mysql it return 0 row.
    – HaFiz Umer
    May 3, 2019 at 6:47
  • First i get this collection in .phtml file and no effect. then i debug the query it return 0 row Sir. I want to get all the discounted products can you please Help me Sir ?
    – HaFiz Umer
    May 3, 2019 at 6:49
  • you can see in this screenshot screenshots.firefox.com/4oL1HpUJtrED4cqH/dev.marketplace.com
    – HaFiz Umer
    May 3, 2019 at 6:57
  • i get the discounted product collection and set on page. But here 1 issue . this collection return just all those product in which we add Advance Price when add new Product . But when we set discount via marketing promotion -> Catalog Price rule then above code don't return those all product of this rule how can get also Catalog Price Rule collection as well advance price product get.
    – HaFiz Umer
    May 6, 2019 at 5:30
  • above code is mate special price collection May 6, 2019 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.