0

My goal is to delete the default shipping address of all customer. The code looks something like this:

foreach ($customerCollection as $customer) {

        $customer->load($customer->getId());
        $shippingAddress = $customer->getDefaultShippingAddress();
        $shippingAddress->delete();

        $output->writeln("<info>{$id} Has been deleted..</info>");    
    }

I always get "Area code is not set" error. I already set the area on the constructor of the object. The weird part is this error only occurs when calling the delete method. The save() method works fine without any error. The command will work fine if I remove:

         $shippingAddress->delete();

Anyone can enlightened what's happening here?

0

Can you try with below code

$bootstraps = Bootstrap::create(BP, $_SERVER);
$obj = $bootstraps->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
  • Should I add that in the constructor of the command object? – Eric Libay Sep 11 '17 at 9:40

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.