Question

How to get the VAT value into checkout page from my account information

when customer register they enter the VAT number that will save in account information page but same VAT number not shown in checkout page

can anyone please help me.

No correct solution

OTHER TIPS

I assume that you set the customer attribute on registration, not the customer_address attribute.

You have to create a default address for the new customer and add the VAT-ID to this address. Then it will show in the checkout page.

Have a look at the eav_attribute table:

SELECT * FROM eav_attribute WHERE attribute_code LIKE "%vat%";

You will see:

  • attribute taxvat for entity type customer (I assume you're currently using this)
  • attribute vat_id for entity type customer_address

Use the 2nd.

If you have following setting enabled the VAT Input field should be display in checkout:

Stores -> Configuration _> Customers -> Customer Configuration -> Create New Account Options- > Show VAT Number on Storefront

this code should help you:

<?php
declare(strict_types=1);

namespace Test\VatId\Model;

use Magento\Customer\Model\ResourceModel\Address\CollectionFactory;

class GetVatIdList
{
/**
 * @var CollectionFactory
 */
private $collectionFactory;

/**
 * GetVatIdList constructor.
 * @param CollectionFactory $collectionFactory
 */
public function __construct (
    CollectionFactory $collectionFactory
) {
    $this->collectionFactory = $collectionFactory;
}

public function execute($customerId)
{
    $addressCollection = $this->collectionFactory->create();
    $addressCollection->addAttributeToSelect('vat_id');
    $addressCollection->addAttributeToFilter('parent_id', $customerId);
    $addressCollection->setOrder('updated_at', \Magento\Framework\Data\Collection::SORT_ORDER_DESC);
    $addressCollection->getFirstItem();
    return $addressCollection;
}
}

This code takes the tax vat saved in the customer addresses and provides the value of the most recent one.

Cheers

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top