0

I have created a customer multi-selected attribute but I am not able to get its value on checkout can you please guide me on how can I get it. I try these two solutions but it not work.

$customer = $customerSession->getCustomer(); 
echo $customer->getResource()->getAttribute('payment_method')->getFrontend()->getValue($customer);

2nd

$customer = $customerSession->getCustomer();
$paymentValue = $customer->getCustomAttributeText('payment_method')->getValue();

Thanks in advance

0

Use CustomerRepositoryInterface as below

protected $customerSession;

protected $customerRepositoryInterface;

public function __construct(
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
) {
    $this->customerSession = $customerSession;
    $this->customerRepositoryInterface = $customerRepositoryInterface;
}

public function getCustomer() {
    if($this->customerSession->getCustomer()->getId()){
        $customer = $this->customerRepositoryInterface->getById($this->customerSession->getCustomer()->getId());
    }else{
        $customer = null;
    }
    return $customer;
}

public function getCustomerPaymentMethod() {
    if($this->getCustomer() != null){
        $customer = $this->getCustomer();
        if(!empty($customer->getCustomAttribute('payment_method'))){
            $customerPaymentMethod = $customer->getCustomAttribute('payment_method')->getValue();
            // use explode if it is saved as comma separated in DB
            // $customerPaymentMethod = explode(',',$customerPaymentMethod);
        }else{
            $customerPaymentMethod = null;
        }
    }else{
        $customerPaymentMethod = null;
    }

    return $customerPaymentMethod;
}

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.