3

By default, when a user first checks out they are presented with the shipping address form because they have no shipping address. Once they complete an order, the address they used is being saved to their account.

I would like to disable the address being saved to their account so that the user presented with the address form every time they order.

Updated in response to Ramkishan's reply

MyModule\Observer\SalesOrderPlaceAfterObserver.php

    <?php
namespace Company\Vendors\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class SalesOrderPlaceAfterObserver implements ObserverInterface
{
  public function execute(\Magento\Framework\Event\Observer $observer)
      {
    $order = $observer->getOrder();
    $customerAddressId =  $order->getShippingAddress()->getCustomerAddressId();
    // Inject Magento\Customer\Api\AddressRepositoryInterface in constructor and remove customer address.
    $this->customerAddressRepository->deleteById($customerAddressId);
      }
}

Company\MyModule\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="sales_order_place_after">
        <observer name="sales_order_place_after" instance="Company\Vendors\Observer\SalesOrderPlaceAfterObserver" />
    </event>
</config>

This is what I currently have and when I go to complete the order, i get the error just above the "Place Order" button after pressing it but it says nothing at all. No text appears, just the error icon and error colour background

This is my first time creating an observer, where have I gone wrong?

Update

I've just noticed that each time it failed, it was saving the address. As I pressed the button 4 times to try and see an error message, I now have the same address saved 4 times as different addresses to the same user.

2 Answers 2

2

You can try this way if it works for you. Create a observer of sales_order_place_after.

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="adminhtml_company_save_after">
    <observer name="attribute_save" instance="VednorName\ModuleName\Observer\AttributeSave"/>
  </event>
</config>

SalesOrderPlaceAfterObserver

<?php
namespace VednorName\ModuleName\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class SalesOrderPlaceAfterObserver implements ObserverInterface
{
 /**
  * @var \Magento\Customer\Api\AddressRepositoryInterface
  */
  protected $addressRepository;

 /**
  * @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepositoryInterface
  */
  public function __construct(
     \Magento\Customer\Api\AddressRepositoryInterface $addressRepositoryInterface
  ) {
    $this->addressRepository = $addressRepositoryInterface;
  }

 /**
  * @param \Magento\Framework\Event\Observer $observer
  */
  public function execute(\Magento\Framework\Event\Observer $observer)
 {
    $order = $observer->getOrder();
    $customerAddressId =  $order->getShippingAddress()->getCustomerAddressId();
    $this->addressRepository->deleteById($customerAddressId);
  }

}

Note : Please make sure that events.xml is in global scope (etc directory not in frontend directory). Because Magento calls rest api (V1/carts/mine/payment-information) during place order due to this it read files from either global or rest_api scope only.

I hope it will work for you. Let me know if I can help you further.

4
  • Thank you Ramkishan, I've tried your suggestion and I've received an error at checkout. I've edited my original question with the changes I made. Please advise where I've gone wrong.
    – Kai
    Sep 19, 2018 at 1:31
  • Ok let me check @Kai Sep 19, 2018 at 8:37
  • There is no constructor in your class you need to inject addressRpository. Let me update my answer. Sep 19, 2018 at 8:45
  • Hi @Kai, just updated the answer. Sep 19, 2018 at 9:17
-1

In Magento you have to override

vendor\magento\module-quote\Model\QuoteManagement

as in this file you can see a protected method _prepareCustomerQuote($quote) this method calls when order is submitting, so this method is actually preparing quote for customer order submit.

In this method code is written to check if customer already have default shipping address or not, if customer don't have any default shipping address Magento save that's order's shipping address as default shipping address of customer i.e

if (!$hasDefaultShipping) {
                //Make provided address as default shipping address
                $shippingAddress->setIsDefaultShipping(true);
                $hasDefaultShipping = true;
            }

so you have to modify this code by overriding the above mentioned file. To override this I think you have to use preferences You can refer this magento2 how to override model file?

1
  • Hi aman, I tried this approach following the guide you linked to and when I went to flush the cache it crashed the admin and frontend stopped loading. So clearly I had something wrong in my code. Could you please suggest what text you would put in the overriding QuoteManagement.php file?
    – Kai
    Sep 19, 2018 at 3:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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