0

While I am trying to return compare items in custom module phtml file, it is returning zero items but in database table catalog_compare_item showing all the added items to compare.

Class Used : Magento\Catalog\Model\Product\Compare\ListCompare;

Block File:

     public function __construct(
    ....
    ....
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Model\Product\Compare\ListCompare $listCompare,
    array $data = []
    ....
    ) {
    ...
    parent::__construct($context, $data);
    $this->listCompare = $listCompare;
    ...
    }

   public function getObj() {
     return $this->listCompare;
   }

phtml File:

$block->getObj()->getItemCollection(); 

Returning(1000 items where I have added only two products to compare):

array(1000) {
  [0]=>
  array(11) {
    ["row_id"]=>
    string(1) "1"
    ["entity_id"]=>
    string(1) "1"
    ["created_in"]=>
    string(1) "1"
    ["updated_in"]=>
    string(10) "2147483647"
    ["attribute_set_id"]=>
    string(1) "4"
    ["type_id"]=>
    string(6) "simple"
    ["sku"]=>
    string(17) "product_dynamic_1"
    ["has_options"]=>
    string(1) "0"
    ["required_options"]=>
    string(1) "0"
    ["created_at"]=>
    string(19) "2018-03-11 07:45:25"
    ["updated_at"]=>
    string(19) "2018-03-11 07:45:25"
  }

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Also, I have tried with class \Magento\Catalog\CustomerData\CompareProducts

Using the method below mention method.. $ClassObj->getSectionData();

Returning:

array(4) {
  ["count"]=>
  int(0)
  ["countCaption"]=>
  object(Magento\Framework\Phrase)#1858 (2) {
    ["text":"Magento\Framework\Phrase":private]=>
    string(8) "%1 items"
    ["arguments":"Magento\Framework\Phrase":private]=>
    array(1) {
      [0]=>
      int(0)
    }
  }
  ["listUrl"]=>
  string(100) "http://127.0.0.1/m217ee/catalog/product_compare/index/uenc/aHR0cDovLzEyNy4wLjAuMS9tMjE3ZWUvbGVhcm4,/"
  ["items"]=>
  array(0) {
  }
}
  • Try magento.stackexchange.com/a/224991/48355 – PЯINCƏ Oct 14 '18 at 15:15
  • @PRIENCE - I have tried with this .. But getting result as mentioned below. array(4) { ["count"]=> int(0) ["countCaption"]=> object(Magento\Framework\Phrase)#1265 (2) { ["text":"Magento\Framework\Phrase":private]=> string(8) "%1 items" ["arguments":"Magento\Framework\Phrase":private]=> array(1) { [0]=> int(0) } } ["listUrl"]=> string(110) "http://127.0.0.1/mce225/index.php/catalog/product_compare/index/uenc/aHR0cDovLzEyNy4wLjAuMS9tY2UyMjUvbGVhcm4v/" ["items"]=> array(0) { } } – Kausik Dey Oct 15 '18 at 7:30
0

Instead of $this->listCompare->getItemCollection() try $block->getItemCollection()

if you still face issue post your block code and layout file.

  • Hi @rakesh, thanks for replying , I have edited my question, could you please look into it ? – Kausik Dey Oct 13 '18 at 15:12

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.