Magento – How to save quote billing address in magento 2

magento-2.1magento-2.1.3sales-quote

For our project we have customized theme in Magento2. This has billing and shipping address pages. We have to save the billing address details in quote address table. But we didn't come across any specific function to save the billing address in Magento 2.

Anybody worked on this kind of billing save in Magento2?

Best Answer

In controller:

namespace Vendor\Module\Controller\MyControler;

class MyAction extends \Magento\Framework\App\Action\Action {

    /**
     * Quote
     */
    private $quote = null;

    /**
     * Constructor
     *
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Checkout\Model\Session $session
     *
     * @return void
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Checkout\Model\Session $session
    ) {
        $this->quote = $session->getQuote();

        parent::__construct($context);
    }

    /**
     * Execute action
     *
     * @return void
     */
    public function execute()
    {
        $this->quote->getBillingAddress()->setRegionId('18');  // Florida
        $this->quote->getBillingAddress()->setCity('Orlando'); 

        $this->quote->getBillingAddress()->setCountryId('US');
        $this->quote->getBillingAddress()->setPostcode('32825');
        $this->quote->getBillingAddress()->setCity('Orlando');
        $this->quote->getBillingAddress()->setStreet('1234 John Young pkwy' . PHP_EOL . 'apt 123');
        $this->quote->getBillingAddress()->setTelephone('5555');
        $this->quote->getBillingAddress()->setFirstName('John');
        $this->quote->getBillingAddress()->setLastName('Young');

        $this->quote->save();

        var_dump($this->quote->getBillingAddress()->getData());
    }

}
Related Topic