3

I am getting the following error while trying to add product to cart in Magento 2.2.3.

[2018-05-02 09:52:43] report.CRITICAL: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (magento2.quote_item, CONSTRAINT QUOTE_ITEM_QUOTE_ID_QUOTE_ENTITY_ID FOREIGN KEY (quote_id) REFERENCES quote (entity_id) ON DELETE CASCADE), query was: INSERT INTO quote_item (product_id, store_id, is_virtual, sku, name, is_qty_decimal, weight, qty, product_type, base_cost, subscription_frequency) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) {"exception":"[object] (Zend_Db_Statement_Exception(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (magento2.quote_item, CONSTRAINT QUOTE_ITEM_QUOTE_ID_QUOTE_ENTITY_ID FOREIGN KEY (quote_id) REFERENCES quote (entity_id) ON DELETE CASCADE), query was: INSERT INTO quote_item (product_id, store_id, is_virtual, sku, name, is_qty_decimal, weight, qty, product_type, base_cost, subscription_frequency) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) at C:\xampp\htdocs\mage2\vendor\magento\zendframework1\library\Zend\Db\Statement\Pdo.php:235, PDOException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (magento2.quote_item, CONSTRAINT QUOTE_ITEM_QUOTE_ID_QUOTE_ENTITY_ID FOREIGN KEY (quote_id) REFERENCES quote (entity_id) ON DELETE CASCADE) at C:\xampp\htdocs\mage2\vendor\magento\zendframework1\library\Zend\Db\Statement\Pdo.php:228)"} []

I have added a custom column in quote_item table. Not sure if this is the issue. Kindly help resolve it. TIA

EDIT:

I'm updating my custom using event - checkout_cart_product_add_after . Below is my observer update code.

<?php

namespace Addon\Recur\Observer;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class CheckoutCartProductAddAfterObserver implements ObserverInterface
{
    /**
     * @var \Magento\Framework\View\LayoutInterface
     */
    protected $_layout;
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;
    protected $_request;

    /**
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Framework\View\LayoutInterface $layout
     */
    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\View\LayoutInterface $layout,
        \Magento\Framework\App\RequestInterface $request
     )
    {
        $this->_layout = $layout;
        $this->_storeManager = $storeManager;
        $this->_request = $request;
    }


    /**
     * Add order information into GA block to render on checkout success pages
     *
     * @param EventObserver $observer
     * @return void
     */
   public function execute(\Magento\Framework\Event\Observer $observer) {

        /* @var \Magento\Quote\Model\Quote\Item $item */
        $item = $observer->getQuoteItem();
        $post = $this->_request->getParam('frequency');
        $item->setSubscriptionFrequency($post);
        $item->save();

        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');  
        $logger = new \Zend\Log\Logger();  
        $logger->addWriter($writer);  
        $logger->info($post);
   }
}

1 Answer 1

5

I ran into the same problem when i tried to change data on quote items with the checkout_cart_product_add_after event. This event is fired after every product add, but before the cart is saved. Check the Magento\Checkout\Controller\Cart\Add line 108-113 for reference. So if you want to save the quote item on a new session there is no quote yet and there is no quote_id to reference to.

In my case, i just left the $item->save(); out, because it will be saved afterwards automatically.

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.