I have purchased the Geissweb VAT checker, But it doesn’t verify.
If the ZIP code from the VAT matches the ZIP code from the delivery address.
So, I created this code, but when it doesn’t match, the form still gets submitted.
Here’s my code ->
<?php
/**
* @var Template $block
* @var RemarktCustomerViewModelCustomerMenu $viewModel
*/
use MagentoFrameworkAppBootstrap;
use MagentoFrameworkAppFilesystemDirectoryList;
use MagentoFrameworkAppResourceConnection;
use MagentoFrameworkViewElementTemplate;
try {
require BP . '/app/bootstrap.php';
$params = $_SERVER;
$params[Bootstrap::PARAM_REQUIRE_MAINTENANCE] = true;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$objectManager->get('MagentoFrameworkAppState')->setAreaCode('frontend');
$connection = $objectManager->get(ResourceConnection::class)->getConnection();
// Replace 'vat_validation' with the actual table name
$tableName = $connection->getTableName('vat_validation');
// Replace 'vat_id' with the actual column name
$selectVatId = $connection->select()
->from($tableName, 'vat_id')
->limit(1);
$selectVatAddress = $connection->select()
->from($tableName, 'vat_trader_address')
->limit(1);
$vatId = $connection->fetchOne($selectVatId);
$VatAddress = $connection->fetchOne($selectVatAddress);
$VatAddress = str_replace(array("r", "n"), ' ', $VatAddress);
} catch (Exception $e) {
// Handle any exceptions that occur
}
?>
<script>
document.getElementById("form-validate").addEventListener("submit", function(event) {
event.preventDefault(); // Prevent form submission
var BTWnummer = document.querySelector('[name="vat_id"]').value;
var postcode = document.getElementById("zip").value;
console.log("btw +", BTWnummer);
if (BTWnummer !== "" && postcode !== "") {
var match = compareFormData(BTWnummer, postcode);
var resultElement = document.getElementById("result");
if (match) {
console.log('fo423t');
return resultDiv.innerHTML = "fo423t";
} else {
console.log("<div class='mage-error'>Postcode van het BTW nummer komt niet overeen met het ingevulde postcode</div>");
return resultDiv.innerHTML = "<div class='mage-error'>Postcode van het BTW nummer komt niet overeen met het ingevulde postcode</div>";
}
}
});
function compareFormData(formBTWnummer, formPostcode) {
// JavaScript code for comparison
var phpName = "<?php echo $vatId; ?>";
var phpPostcode = "<?php echo $VatAddress; ?>";
var containsSubstring1 = phpName.includes(formBTWnummer);
var containsSubstring3 = phpPostcode.includes(formPostcode);
if (containsSubstring1 && containsSubstring3) {
console.log('goe5d');
return true;
} else {
console.log('fo7ut');
return false;
}
}
</script>