3

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.

  • Can you please provide the solution that worked for you.!! – Manashvi Birla Jul 19 '18 at 12:51
  • can u please specify ur magento version – Mano M Jul 19 '18 at 12:54
  • I am using version 2.2.3 – Manashvi Birla Jul 19 '18 at 12:57
  • I mentioned the answer below in this page @Manashvi Birla – Mano M Jul 19 '18 at 13:00
3

try below code

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Customer\Model\Address\AbstractAddres" type="Xxx\Customer\Model\Address\AbstractAddres" />
 </config> 
  • 1
    its not a syntax for using plugin in M2 – Mohamed El Mrabet Nov 24 '17 at 9:28
  • 1
    also tried preference.but no change in it – Mano M Nov 24 '17 at 9:35
2

Try this

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($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']);
        }
    }

    if (empty($errors) || $this->getShouldIgnoreValidation()) {
        return true;
    }
    return $errors;
}
  • it throws same error like required lastname – Mano M Nov 24 '17 at 9:34
  • Ok, in the original class if you comment lines that's woks? – Mohamed El Mrabet Nov 24 '17 at 9:48
  • yes.it's working in core........ – Mano M Nov 24 '17 at 9:49
  • if plugin don't work you can try with preference, tell me if you nedd any help i'm here we can chat if you need – Mohamed El Mrabet Nov 24 '17 at 9:52
  • I'm already using preference .i can't override this file.it throws "last name required".In plugin method throws "Please check shipping information" – Mano M Nov 24 '17 at 9:56
1

Your di.xml is wrong if you want to override a class.

In Magento there are two types of overriding:

  • One is Plugin
  • Another is preference

PLUGIN:

Is used for changing the behavior of a method. See the details at Magento developer documentation

Syntax:

<type name="{OriginalClass}">
    <plugin name="{Name}" type="{PluginClasss}" sortOrder="{sortorder}"/>
</type>

Class preference:

The old-fashioned way of overriding classes is what we got used to, but this is slightly different... See the details at the Inchoo blog.

Syntax:

<preference for="{OriginalClass}" type="{OverriderClassClasss}" />

As per as, you want to override a function/method: Magento\Customer\Model\Address\AbstractAddress

  • Preference method also not working for me.if any changes in my di.xmi.please specify that – Mano M Nov 24 '17 at 10:07
0

In Model file...

use Magento\Directory\Helper\Data;
use Magento\Eav\Model\Config;

class AbstractAddress
{

    protected $_directoryData = null;
    protected $_eavConfig;

    public function __construct(
    Data $directoryData, 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']);
        }
        //OX:Removed  validation  related to 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']);
            }
        }
        $_havingOptionalZip = $this->_directoryData->getCountriesWithOptionalZip();
        if (!in_array(
                        $subject->getCountryId(), $_havingOptionalZip
                ) && !\Zend_Validate::is(
                        $subject->getPostcode(), 'NotEmpty'
                )
        ) {
            $errors[] = __('%fieldName is a required field.', ['fieldName' => 'postcode']);
        }

        if (!\Zend_Validate::is($subject->getCountryId(), 'NotEmpty')) {
            $errors[] = __('%fieldName is a required field.', ['fieldName' => 'countryId']);
        }

        if ($subject->getCountryModel()->getRegionCollection()->getSize() && !\Zend_Validate::is(
                        $subject->getRegionId(), 'NotEmpty'
                ) && $this->_directoryData->isRegionRequired(
                        $subject->getCountryId()
                )
        ) {
            $errors[] = __('%fieldName is a required field.', ['fieldName' => 'regionId']);
        }

        if (empty($errors) || $subject->getShouldIgnoreValidation()) {
            return true;
        }

        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());
    }

di.xml

<type name="Magento\Customer\Model\Address\AbstractAddress">
        <plugin name="aroundAddressValidation" type="XXX\OptionalLastname\Plugin\Customer\Model\Address\AbstractAddress" sortOrder="10"/>
    </type>
  • Thanks for your answer. i am facing this strange issue. i want to change the line elseif ($regionId && !in_array($regionId, $allowedRegions, true)) but the execution is not going till that line – Manashvi Birla Jul 19 '18 at 13:12
  • Can u mention the error? – Mano M Jul 19 '18 at 13:19
  • no error, but the control redirects to cart page. Initially i was getting error Please check the shipping address information. Invalid value of "533" provided for the regionId field thats the reason i created plugin – Manashvi Birla Jul 19 '18 at 13:25
0

to override any class u need to add prefrence in di.xml, if this class is for Adminhtml create di.xml in etc -> adminhtml -> di.xml and add `

<preference for="Magento\Review\Block\Adminhtml\Edit\Form" type="vendor\Module\Block\Adminhtml\Edit\Form" />

`

and create same file in given directory.

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.