I want to override the model for an optional lastname.
di.xml:
<type name="Magento\Customer\Model\Address\AbstractAddress">
<plugin name="aroundAddressValidation" type="Xxx\OptionalLastname\Plugin\Customer\Model\Address\AbstractAddress" sortOrder="10"/>
</type>
AddressAbstract.php:
<?php
namespace Xxx\OptionalLastname\Plugin\Customer\Model\Address;
class AbstractAddress
{
protected $_directoryData = null;
protected $_eavConfig;
public function __construct(
\Magento\Directory\Helper\Data $directoryData, \Magento\Eav\Model\Config $eavConfig
)
{
$this->_directoryData = $directoryData;
$this->_eavConfig = $eavConfig;
}
public function aroundValidate(
\Magento\Customer\Model\Address\AbstractAddress $subject, callable $proceed)
{
$errors = [];
if (!\Zend_Validate::is($subject->getFirstname(), 'NotEmpty')) {
$errors[] = __('%fieldName is a required field.', ['fieldName' => 'firstname']);
}
//
// if (!\Zend_Validate::is($this->getLastname(), 'NotEmpty')) {
// $errors[] = __('%fieldName is a required field.', ['fieldName' => 'lastname']);
// }
if (!\Zend_Validate::is($subject->getStreetLine(1), 'NotEmpty')) {
$errors[] = __('%fieldName is a required field.', ['fieldName' => 'street']);
}
if (!\Zend_Validate::is($subject->getCity(), 'NotEmpty')) {
$errors[] = __('%fieldName is a required field.', ['fieldName' => 'city']);
}
if ($this->isTelephoneRequired()) {
if (!\Zend_Validate::is($subject->getTelephone(), 'NotEmpty')) {
$errors[] = __('%fieldName is a required field.', ['fieldName' => 'telephone']);
}
}
if ($this->isFaxRequired()) {
if (!\Zend_Validate::is($subject->getFax(), 'NotEmpty')) {
$errors[] = __('%fieldName is a required field.', ['fieldName' => 'fax']);
}
}
if ($this->isCompanyRequired()) {
if (!\Zend_Validate::is($subject->getCompany(), 'NotEmpty')) {
$errors[] = __('%fieldName is a required field.', ['fieldName' => 'company']);
}
}
return $errors;
}
/**
* @return bool
* @since 100.2.0
*/
protected function isCompanyRequired()
{
return ($this->_eavConfig->getAttribute('customer_address', 'company')->getIsRequired());
}
/**
* @return bool
* @since 100.2.0
*/
protected function isTelephoneRequired()
{
return ($this->_eavConfig->getAttribute('customer_address', 'telephone')->getIsRequired());
}
/**
* @return bool
* @since 100.2.0
*/
protected function isFaxRequired()
{
return ($this->_eavConfig->getAttribute('customer_address', 'fax')->getIsRequired());
}
}
But it throws Please check the shipping information error. How do I overcome this error? If any modification in my code? If anyone knows please explain me.
Best Answer
try below code