2

I am trying to get the products in the compare list from outside magento, but I am having problems with it.

The intention is to create a small php program that generates this list and can be used for a 3rd party company in their hosted search.

The code seems to be running fine inside a .phtml template. But when ran outside magento the product list either creates an exception (duplicate keys) or brings other products which are not in the list of the current website visitor session.

I am new to magento and php, and I wonder if I am missing something? The code uses nearly the same code than in magento's default template. As the getItemCollection method sometimes returns items from other visitors ID, I wonder if there is something else that needs to be done before calling the method.

<?php
require_once( 'app/Mage.php' );
umask(0);
Mage::app('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));

    header('Content-Type: application/json');

    $_helper = Mage::helper('catalog/product_compare'); 

    $count = $_helper->getItemCount();

    echo '"compare":{';

    echo '"summary":{"count": ';
    echo $count;
    echo ' },"items": [';

    $total=0;

    if ( $count > 0 ) {

          try {          
               $_productCollection = $_helper->getItemCollection();
                        } catch (Exception $e) {
                                    echo 'Caught exception: ',  $e->getMessage(), "\n";
                        }

          foreach ($_productCollection as $_index => $product) {

              $data = $product->getData();
                            print_r($data['visitor_id']);
                            if (true) {
                       if ($total <> 0) echo ',';
                   $total += 1;
                   echo '{ "name": "';
                   $name = $product->getName();
                   echo $name;
                   echo '", "url": "';
                   $url = $product->getProductUrl();
                   echo $url;
                   echo '", "remove_url": "';
                   $remove = $_helper->getRemoveUrl($product);
                   echo $remove;
                   echo '"}';
                                }
              } 
  } 
echo ']}';
echo '}}';
    echo ')';
?>

If the user is logged in all seems to work fine.

If the user is not logged in there are problems:

  • If a product is added, removed and added again then getItemCollection throws an exception. This can be cleared by emptying the compare list database for example.

  • Sometimes more products are in the list than what is supposed to be in the session.

I examined the contents of getItemCollection, and sometimes the items correspond to other sessions (they have different visitor Ids).

Thanks, Webster

2
  • I had a look at the table in the database 'catalog_compare_item' and getItemCollection() in the .php seems to bring data from this table regardless of the visitor_id. So if there are two entries with the same product but different visitor_id (two people loaded the same product in the compare list) it throws an exception. Inside the .phtml it seems to be working alright, the issue happens when called from the .php Jun 23, 2015 at 2:45
  • probably have a very valid reason but why are you manually generating JSON structure instead of just json_encode?
    – zanderwar
    Apr 6, 2016 at 6:51

1 Answer 1

1

Try this:

try {
    $_productCollection = $_helper->getItemCollection();
    $collection->setVisitorId(Mage::getSingleton('log/visitor')->getId());
    // other code...

This will filter the collection to show products from a specific visitor.

If you are logged in, then use this instead:

try {
    $_productCollection = $_helper->getItemCollection();
    $collection->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId());
    // other code...
2
  • I tried it, but it makes no difference. Jun 23, 2015 at 2:35
  • I see... By calling $count = $_helper->getItemCount();, it is already doing the filter. Jun 23, 2015 at 2:53

Your Answer

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

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