1

In magento 2 how to get and save a customer attributes value in db

m

app/code/Cm/CustomerAttribute/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  <type name="Magento\Checkout\Model\ShippingInformationManagement">
         <plugin name="Cm_CustomerAttribute::ShippingInformationManagement" 
 type="Cm\CustomerAttribute\Plugin\Checkout\Model\ShippingInformationManagement" 
 sortOrder="1"/>
  </type>
</config>

app/code/Cm/CustomerAttribute/Plugin/Checkout/Model/ShippingInformationManagement.php

<?php
namespace Cm\CustomerAttribute\Plugin\Checkout\Model;

class ShippingInformationManagement
{
    /**
     * @param \Magento\Checkout\Model\ShippingInformationManagement $subject
     * @param $cartId
     * @param \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
     */
     protected $quoteRepository;
     public function __construct(

    \Cm\CustomerAttribute\Helper\Customerattribute $helper,
     \Magento\Quote\Model\QuoteRepository $quoteRepository
    ) {

        $this->helper = $helper;
         $this->quoteRepository = $quoteRepository;
    }


    public function beforeSaveAddressInformation(
        \Magento\Checkout\Model\ShippingInformationManagement $subject,
        $cartId,
        \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
    ) {
       $shippingAddress = $addressInformation->getShippingAddress();
    $shippingAddressCustomerAttributes = $shippingAddress->getCustomAttributes();
    if ($shippingAddressCustomerAttributes) {
        $customField = $shippingAddressCustomerAttributes->getTestAge();
        $shippingAddress->setTestAge($customField);
     }


    }

Custom field value not saved in db enter image description here

  • Are you getting data which you wanted to save ? – anonymous Jul 25 '19 at 6:49
  • i m posting a value in checkout page prnt.sc/ojqzkl and i m asking how to get a customer attributes values and saved into quote table – divya sekar Jul 25 '19 at 6:57
  • That I know What I'm asking you is are you getting your customer attribute data in conroller,observer etc Whatever you have used !? Or you looking solution how to get those data in file and save to db ? – anonymous Jul 25 '19 at 6:59
  • I'm looking for how to get a data and saved into db – divya sekar Jul 25 '19 at 7:01
  • Did you get all the custom attributes in beforeSaveAddressInformation function? – Ranganathan Jul 25 '19 at 9:50
2

You can Use Observer After Place Order for that

Vendor/Module/etc/frontend/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_onepage_controller_success_action">
        <observer name="custom_frontend_observer" instance="Vendor/Module\Observer\AfterCheckout" />
    </event>
</config>

now in

Vendor\Module\Observer\AfterCheckout.php

/**
 * Class AfterCheckout
 */
class AfterCheckout implements \Magento\Framework\Event\ObserverInterface
{


    /**
     * Get Current Customer from session
     */
    private $customerSession;

    public function __construct(
        \Magento\Customer\Model\Session $customerSession,$collection
    ) {
        $this->customerSession = $customerSession;
    }
    /**
     * add custom layout updates
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return $this
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        //get Customer Id and DO what you want
    }
    /**
     * Returns Current Customer ID
     * @return Integer
     */
    public function getCusId()
    {
        if ($this->customerSession->isLoggedIn()):
            return $this->customerSession->getCustomerId();
        endif;
    }
}

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.