1

I want to get the product collection of those that have news_from_date field.

This is my block file

<?php
namespace Company\Mod\Block\Theme;
class Productnew extends \Magento\Framework\View\Element\Template
{
public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $productCollectionFactory,
        array $data = []
        ) {

        $this->_productCollectionFactory = $productCollectionFactory;
        parent::__construct($context, $data);
    }


    public function getProductCollection()
    {       
        //echo 
        $collection = $this->_productCollectionFactory->create();
        $todayDate  = date('Y-m-d', time());
        $collection->addAttributeToFilter('news_from_date', array('date' => true, 'to' => $todayDate));

        return $collection;
    }



}

In my phtml

<?php 

 $test = $block->getProductCollection();
 print_r($test);exit;

But it returns a blank page. How do I get product collection correctly?

2
  • If you view the source it will often give you the error message. Or make sure developer mode is turned on.
    – Adam Moss
    Mar 24, 2017 at 9:27
  • @V.P please try with below solutions. Mar 24, 2017 at 9:28

1 Answer 1

2
public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
    array $data = []
) {
    $this->_productCollectionFactory = $productCollectionFactory;
    $this->_catalogProductVisibility = $catalogProductVisibility;
    parent::__construct(
        $context,
        $data
    );
}

protected function getProductCollection()
    {
       $todayDate = date('Y-m-d');
       $collection = $this->_productCollectionFactory->create();
       $collection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());

        $collection->addAttributeToFilter(
            'news_from_date',
            [
                'or' => [
                    0 => ['date' => true, 'to' => $todayDate],
                    1 => ['is' => new \Zend_Db_Expr('null')],
                ]
            ])->addAttributeToFilter(
            [
                ['attribute' => 'news_from_date', 'is' => new \Zend_Db_Expr('not null')],
            ]
        );

        return $collection;
    }

Inside template file,

<?php 

 $test = $block->getProductCollection();
 foreach($test as $product){
    echo $product->getName();
    echo $product->getPrice();
    print_r($product->getData());
 }

Remove var folder and clear cache.

9
  • In my phtml,I have used this $test = $block->getProductCollection();echo 'yyyyyyy12'.count($test);But it gives yyyyyyy120 as result,which means count is zero.Please help
    – Vindhuja
    Mar 24, 2017 at 10:29
  • have you set product as new date from backend? Mar 24, 2017 at 10:32
  • yes i have set. Also,for testing I have returned $todayDate .It gives null value
    – Vindhuja
    Mar 24, 2017 at 10:36
  • use only, $todayDate = date('Y-m-d'); and check Mar 24, 2017 at 10:42
  • I got today's date.I have cleared the cache as well.I got the product count also.Can you please help me to get the product name and price in that phtml for that collection. I have used $test->getName(); .But giving error.
    – Vindhuja
    Mar 24, 2017 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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