Quantcast
Channel: Insane in the Main Frame
Viewing all articles
Browse latest Browse all 310

Article pages sometimes throw 404 error

$
0
0

A weird problem which I have encountered in two different magento installations (version 1.9.x.x): sometimes it happens that product pages are no longer available, instead a 404 error page is shown. In the log (var/log/exception.log) there is the following error:

  1. Next exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'xxxxx-xxx' for key 'UNQ_REPORT_VIEWED_PRODUCT_INDEX_CUSTOMER_ID_PRODUCT_ID'' in /path/to/shop/lib/Zend/Db/Statement/Pdo.php:234

This bugreport looks like exactly the same problem, seems to be a known issue with magento < 2. The link explains how to fix the bug as well, I recommend the solution by andrr at the bottom, which is to edit the magento core itself in the app/code/core/Mage/Reports/Model/Resource/Product/Index/Abstract.php file, editing the save() method like this:

  1. unset($data[$this->getIdFieldName()]);
  2. //start fix
  3. if(Mage::getSingleton(‘customer/session’)->isLoggedIn()){
  4. $this->updateCustomerFromVisitorByProductId($object);
  5. }
  6. //end fix
  7. $matchFields = array(‘product_id’, ‘store_id’);

And then add updateCustomerFromVisitorByProductId to the class itself:

  1. public function updateCustomerFromVisitorByProductId(Mage_Reports_Model_Product_Index_Abstract $object)
  2. {
  3. /**
  4. * Do nothing if customer not logged in
  5. */
  6. if (!$object->getCustomerId() || !$object->getVisitorId() || !$object->getProductId()) {
  7. return $this;
  8. }
  9. $adapter = $this->_getWriteAdapter();
  10. $select = $adapter->select()
  11. ->from($this->getMainTable())
  12. ->where('visitor_id = ?', $object->getVisitorId())
  13. ->where('product_id = ?', $object->getProductId());
  14. $rowSet = $select->query()->fetchAll();
  15. foreach ($rowSet as $row) {
  16. $select = $adapter->select()
  17. ->from($this->getMainTable())
  18. ->where('customer_id = ?', $object->getCustomerId())
  19. ->where('product_id = ?', $row['product_id']);
  20. $idx = $adapter->fetchRow($select);
  21. if ($idx) {
  22. /* If we are here it means that we have two rows: one with known customer, but second just visitor is set
  23. * One row should be updated with customer_id, second should be deleted
  24. */
  25. $adapter->delete($this->getMainTable(), array('index_id = ?' => $row['index_id']));
  26. $where = array('index_id = ?' => $idx['index_id']);
  27. $data = array(
  28. 'visitor_id' => $object->getVisitorId(),
  29. 'store_id' => $object->getStoreId(),
  30. 'added_at' => Varien_Date::now(),
  31. );
  32. } else {
  33. $where = array('index_id = ?' => $row['index_id']);
  34. $data = array(
  35. 'customer_id' => $object->getCustomerId(),
  36. 'store_id' => $object->getStoreId(),
  37. 'added_at' => Varien_Date::now()
  38. );
  39. }
  40. $adapter->update($this->getMainTable(), $data, $where);
  41. }
  42. return $this;
  43. }

And that worked for me.


Viewing all articles
Browse latest Browse all 310

Trending Articles