1

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.

2
  • what are you trying to accomplish? Do you want to show the VAT in the checkout page or just get the value?
    – Joao71
    Commented Aug 10, 2020 at 12:48
  • get the VAT value from my account information page and display the checkout page address Commented Aug 11, 2020 at 5:45

3 Answers 3

1

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.

4
  • It really puzzles me why there are two fields for the VAT ID. It is really confusing for the customers.
    – Webninja
    Commented Oct 26, 2020 at 14:36
  • 1
    Yes, same. I think the customer-attribute ("taxvat") is nonsense. It makes more sense to have the VAT ID at the customer's address, because different addresses may require different tax calculations. Commented Oct 26, 2020 at 14:40
  • I agree completely. I guess it is because of the automatic customer group selection at the registration phase, but I'm sure this could have done in a more sensible way. Maybe set the customer taxvat as the default VAT ID of the address if no other is given or something like that. Was it like this in M1 as I have a migration case where migrated registered customers are all of a sudden ordering with tax included as they expect things to work as they worked in M1?
    – Webninja
    Commented Oct 26, 2020 at 14:50
  • 1
    Yes, M1 also had 2 attributes IIRC. But I think the separation of customer and customer_address wasn't so strict back then, or you might have had a customization in M1 that handled this. Haven't worked with M1 a lot lately, so this is from memory obviously. Commented Oct 26, 2020 at 15:03
0

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.