0

I want to Manually change all order address to a specific city. So right now user can just input and city. But I would like to add some validation or change the behavior of save order function. So basically when someone enters a billing address then clicked on the Place Order, I would then change it's city to a default value regardless if they inputted a value in the city. How do I achieve this? I tried using plugin but didn't work. I also tried to override the behavior of SavePaymentInformationPlaceOrder but still now luck below is what I did

in my plugin I have this

 public function beforeSavePaymentInformationAndPlaceOrder(
    \Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject,
    $cartId,
    $email,
    \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
    \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
) {

    $billingAddress->setPersonalMessage("Abc123"); // Personal Message is a custom message that we have
    $billingAddress->save();
    
}

And this is the value of my di.xml

<type name="Magento\Checkout\Api\GuestPaymentInformationManagementInterface">
    <plugin name="validate-billing-information" type="Vendor\Module\Plugin\Checkout\BillingValidation"/>
</type>

The plugin works but when it execute the Save method I got this error

[2020-09-26 15:37:06] main.CRITICAL: Report ID: webapi-5f6f60228b3a3; Message: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`db_database`.`quote_address`, CONSTRAINT `QUOTE_ADDRESS_QUOTE_ID_QUOTE_ENTITY_ID` FOREIGN KEY (`quote_id`) REFERENCES `quote` (`entity_id`) ON DELETE CASCADE), query was: INSERT INTO `quote_address` (`save_in_address_book`, `prefix`, `firstname`, `lastname`, `company`, `street`, `city`, `region`, `postcode`, `country_id`, `telephone`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) {"exception":"[object] (Exception(code: 23000): Report ID: webapi-5f6f60228b3a3; Message: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`db_database`.`quote_address`, CONSTRAINT `QUOTE_ADDRESS_QUOTE_ID_QUOTE_ENTITY_ID` FOREIGN KEY (`quote_id`) REFERENCES `quote` (`entity_id`) ON DELETE CASCADE), query was: INSERT INTO `quote_address` (`save_in_address_book`, `prefix`, `firstname`, `lastname`, `company`, `street`, `city`, `region`, `postcode`, `country_id`, `telephone`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) at /home/vagrant/code/vendor/magento/framework/Webapi/ErrorProcessor.php:208, Zend_Db_Statement_Exception(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`db_database`.`quote_address`, CONSTRAINT `QUOTE_ADDRESS_QUOTE_ID_QUOTE_ENTITY_ID` FOREIGN KEY (`quote_id`) REFERENCES `quote` (`entity_id`) ON DELETE CASCADE), query was: INSERT INTO `quote_address` (`save_in_address_book`, `prefix`, `firstname`, `lastname`, `company`, `street`, `city`, `region`, `postcode`, `country_id`, `telephone`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) at /home/vagrant/code/vendor/magento/framework/DB/Statement/Pdo/Mysql.php:110, PDOException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`db_database`.`quote_address`, CONSTRAINT `QUOTE_ADDRESS_QUOTE_ID_QUOTE_ENTITY_ID` FOREIGN KEY (`quote_id`) REFERENCES `quote` (`entity_id`) ON DELETE CASCADE) at /home/vagrant/code/vendor/magento/framework/DB/Statement/Pdo/Mysql.php:91)"} []

1 Answer 1

0

You can try using below event

VendorName/ModuleName/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
   <event name="checkout_submit_before">
      <observer name="checkout_submit_before_update_billing" instance="VendorName\ModuleName\Observer\QuoteSubmitBeforeUpdateBilling"/>
   </event>
</config>

VendorName/ModuleName/Observer/QuoteSubmitBeforeUpdateBilling.php

<?php

namespace VendorName\ModuleName\Observer;

use Magento\Framework\Event\ObserverInterface;

class QuoteSubmitBeforeUpdateBilling implements ObserverInterface
{
    /**
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        /** @var \Magento\Quote\Model\Quote $quote */
        $quote = $observer->getEvent()->getQuote();
        $quote->getBillingAddress()->setPersonalMessage("Abc123");
    }
}
3
  • already tried this but didn't work Sep 27, 2020 at 8:54
  • @MadzQuestioning Is the event not getting fired at all or getting fired but not updating the value? Sep 27, 2020 at 16:04
  • it's getting fired it's just not updating to the new value I added Sep 27, 2020 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.