4

This doesn't seem to be related to any additional modules as I've been testing it a lot.

When someone logs in it all goes to the same controller, but if the $_POST variable context is set to checkout, alternative code is run to tweak/adjust the quote/basket in progress. This normally occurs from one page step 1 for a logged out user.

When turning on the right debugging, this results in:

Customer Login Failed Reg: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`somedbname`.`sales_flat_quote_shipping_rate`, CONSTRAINT `FK_B1F177EFB73D3EDF5322BA64AC48D150` FOREIGN KEY (`address_id`) REFERENCES `sales_flat_quote_address` (`address_id`) ON DELETE CASC)

Digging thru the code, it runs setQuoteGuest when the context is set, from line 437 of public_html/app/code/core/Mage/Persistent/Model/Observer.php

This deletes the quote addresses and then updates them. Of course the update fails and the code trips thru the rest of the code till it tries to insert into sales_flat_quote_shipping_rate which is where the integrity constraint comes in.

Queries:

2014-03-31T16:10:06+00:00 DEBUG (7): DELETE FROM `sales_flat_quote_address` WHERE (address_id='1')
2014-03-31T16:10:06+00:00 DEBUG (7): DELETE FROM `sales_flat_quote_address` WHERE (address_id='2')
2014-03-31T16:10:07+00:00 DEBUG (7): UPDATE `sales_flat_quote_address` SET `quote_id` = ?, `updated_at` = '2014-03-31 16:10:07', `customer_id` = ?, `address_type` = ?, `country_id` = ?, `collect_shipping_rates` = ?, `subtotal` = ?, `base_subtotal` = ?, `grand_total` = ?, `base_grand_total` = ? WHERE (address_id='1')

and in full

## 2014-03-31 16:10:06
## 41801 ## QUERY
SQL: SELECT `main_table`.* FROM `sales_flat_quote_address` AS `main_table` WHERE (quote_id = '1')
AFF: 2
TIME: 0.0007

## 2014-03-31 16:10:06
## 41801 ## TRANSACTION BEGIN
TIME: 0.0001

## 2014-03-31 16:10:06
## 41801 ## QUERY
SQL: DELETE FROM `sales_flat_quote_address` WHERE (address_id='1')
AFF: 1
TIME: 0.0331

## 2014-03-31 16:10:06
## 41801 ## TRANSACTION COMMIT
TIME: 0.0563

## 2014-03-31 16:10:06
## 41801 ## TRANSACTION BEGIN
TIME: 0.0001

## 2014-03-31 16:10:06
## 41801 ## QUERY
SQL: DELETE FROM `sales_flat_quote_address` WHERE (address_id='2')
AFF: 1
TIME: 0.0008

## 2014-03-31 16:10:06
## 41801 ## TRANSACTION COMMIT
TIME: 0.1874

## 2014-03-31 16:10:07
## 41801 ## TRANSACTION BEGIN
TIME: 0.0001

## 2014-03-31 16:10:07
## 41801 ## QUERY
SQL: UPDATE `sales_flat_quote_address` SET `quote_id` = ?, `updated_at` = '2014-03-31 16:10:07', `customer_id` = ?, `address_type` = ?, `country_id` = ?, `collect_shipping_rates` = ?, `subtotal` = ?, `base_subtotal` = ?, `grand_total` = ?, `base_grand_total` = ? WHERE (address_id='1')
BIND: array (
  0 => 1,
  1 => NULL,
  2 => 'billing',
  3 => 'GB',
  4 => 1,
  5 => 0,
  6 => 0,
  7 => 0,
  8 => 0,
)
AFF: 0

So, have I stumbled on a Magento bug?

Line 447 of said observer

        $quote->getPaymentsCollection()->walk('delete');
        $quote->getAddressesCollection()->walk('delete');
        $this->_setQuotePersistent = false;
        $quote
            ->setIsActive(true)
            ->setCustomerId(null)
            ->setCustomerEmail(null)
            ->setCustomerFirstname(null)
            ->setCustomerLastname(null)
            ->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID)
            ->setIsPersistent(false)
            ->removeAllAddresses();
        //Create guest addresses
        $quote->getShippingAddress();
        $quote->getBillingAddress();
        $quote->collectTotals()->save();

Seems to delete the addresses and then try to update the addresses it just deleted. \o/

I've traced as much as I can and now I'm stuck. I'm about to setup a proper blank install of Magento to compare. But thought I'd seek the knowledge of the hive. As I have no idea if it's me of Magento at fault!

Update: It's works fine on a plain Vanilla Magento.

Update 2: Looks like it's something in WebShop Apps. Checking which part now…

Update 3: It's not WebShop Apps. It's something derpy

1 Answer 1

8

Solution found!

If you have a module that does anything regarding the quote and addressing running on a observer you need to add additional code to check if login, is occurring at that step.

As found via: http://www.rapidcommerce.eu/blog/2012/04/allways-show-shipping-costs-in-magento/ from Thomas on December 4, 2013.

Thanks to Pieter, who dropped a email to me about this Mageoverflow and found the above.

Essentially add:

    $pageIdentifier = Mage::app()->getFrontController()->getAction()->getFullActionName();
    if ($pageIdentifier == 'customer_account_loginPost') {
        return $observer;
    }

to your observer.

The observer in fault for me was running on the event "sales_quote_address_collect_totals_before" for anyone else, it could be any event related to quotes, their addresses and total collection

4
  • 1
    Finally, this one saved my day.
    – Lemonade
    Sep 14, 2014 at 16:50
  • 1
    on Magento CE 1.9 A custom RapidECommerce Default Destination extension, to display default location and shipping in checkout/cart, broke my onepage/checkout/index loginPostAction. But only after reading your answer I understood, that its observer causes a otherwise successful login to just reload checkout/onepage because of the integrity constraint. Thanks alot !!
    – Lemonade
    Sep 14, 2014 at 17:00
  • Yeah, I was having the same problem RapidECommerce Default Destination and added the code above to the Observer.php. Wham! fix it.
    – Freejoy
    Jun 14, 2017 at 8:15
  • Finally, this one saved my day Nov 20, 2018 at 19:00

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.