clockworkgeek / Magento-Guest-Cookies

Record cart contents and viewing history in cookies for anonymous guests.
11 stars 6 forks source link

Exception on Viewed->addProductIds - Trying to add product_id as NULL #5

Open vinidade opened 4 years ago

vinidade commented 4 years ago

Hello!

I always used this extension and it worked like a charm (thank you by the way). But since yesterday I'm getting a lot of exceptions:

Next Zend_Db_Statement_Exception: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (database.report_viewed_product_index, CONSTRAINT FK_REPORT_VIEWED_PRD_IDX_PRD_ID_CAT_PRD_ENTT_ENTT_ID FOREIGN KEY (product_id) REFERENCES catalog_product_entity (entity_id) ON DELE), query was: INSERT INTO report_viewed_product_index (visitor_id,customer_id,product_id,store_id,added_at) VALUES (\?, \?, \?, \?, '2020-10-18 12:50:08') ON DUPLICATE KEY UPDATE visitor_id = VALUES(visitor_id), customer_id = VALUES(customer_id), product_id = VALUES(product_id), store_id = VALUES(store_id), added_at = VALUES(added_at) in /home/website/public_html/lib/Zend/Db/Statement/Pdo.php:235

I have disabled the viewed part in the setting, but the readViewedCookie()function is triggered "regardless of admin setting" (as stated in the method)

I tried to clean the report_viewed_product_index database, but the exceptions keep appearing.

What can I do to fix this?

Thank you!

clockworkgeek commented 3 years ago

Hello vinidade,

In the file app/code/community/Clockworkgeek/Guestcookies/Model/Viewed.php is this crucial part.

    if ($ids) {
        foreach ($ids as $id) {
            try {
            Mage::getModel('reports/product_index_viewed')
            ->setProductId($id)
            ->save();
            }
            catch (Exception $e) {
                // do nothing if product ID is missing
            }
        }
    }

So all exceptions are suppressed and you shouldn't be seeing any - except you clearly are. Something else must be having an influence but I struggle to imagine what that could be. Even developer mode doesn't cause exceptions to break out of catch blocks like that.

You can make the above code respect that certain admin setting by changing if ($ids) to if ($ids && Mage::getStoreConfigFlag('web/guestcookies/viewed'))