Luis
Luis

Reputation: 1060

Magento change shipping price on the fly SQLSTATE[23000]

Hi I am trying to change shipping price on the fly with the following code but it crash when the rate try to save it:

$shippingprice = 20.00;
$address = $quote->getShippingAddress();
$address->setShippingAmount($shippingprice);
$address->setBaseShippingAmount($shippingprice);
$rates = $address->collectShippingRates()
         ->getGroupedAllShippingRates();
$address->setCollectShippingRates(false);
$address->save();
foreach ($rates as $carrier) {
    foreach ($carrier as $rate) {
        $rate->setPrice($shippingprice);
        $rate->save();//Right here it crash 
    }
}

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (sales_flat_quote_shipping_rate, CONSTRAINT FK_B1F177EFB73D3EDF5322BA64AC48D150 FOREIGN KEY (address_id) REFERENCES sales_flat_quote_address (address_id) ), query was: INSERT INTO sales_flat_quote_shipping_rate (created_at, updated_at, carrier, carrier_title, code, method, method_description, price, method_title) VALUES ('2015-11-12 18:22:52', '2015-11-12 18:22:52', ?, ?, ?, ?, ?, ?, ?)

Any Idea of this error?

Thanks

Upvotes: 1

Views: 392

Answers (1)

Sasha Pachev
Sasha Pachev

Reputation: 5326

It appears from the message that you are trying to insert a shipping rate row for a non-existent address. Add some debugging info to dump out the values you are inserting and hopefully that will make the problem obvious.

Upvotes: 1

Related Questions