Magento – Get products in the compare list in the current session from outside magento

comparemagento-1.5

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

Best Answer

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...
Related Topic