last day (15 days later) » 

10:17 AM
2
A: overriding model in magento2?

Ronak ChauhanCreate a plugin to override functionality of the method: Step 1: di.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Customer\Model\Address\Abstract...

 
still it was loading page.....
 
remove cache and then try it.
 
page loading..........wt can i do
 
check console... or just die on function's 1st line to test.
 
just die on function's 1st line to test.after trying it.the condition die.flow correct.but ..
 
10:17 AM
now die on last line of the function.
 
again loading....
_directoryData in above code?
 
Replace $this with $subject in whole function. Sorry I was just missed to replace it, Answer updated
Initialize _directoryData into constructor, Answer Updated
 
Again loading..............
 
Hey Mahalakshmi.... have you initialize that directoryData?
 
<?php

namespace Xxx\OptionalLastname\Plugin\Customer\Model\Address;

class AbstractAddress
{
protected $_directoryData = null;

public function __construct(
\Magento\Framework\Model\Context $context, \Magento\Directory\Helper\Data $directoryData
)
{
$this->_directoryData = $directoryData;

parent::__construct(
$context

);
}

public function aroundValidate(
\Magento\Customer\Model\Address\AbstractAddress $subject,
callable $proceed)
{

$errors = [];
if (!\Zend_Validate::is($subject->getFirstname(), 'NotEmpty')) {
 
10:19 AM
remove parent:__constructor
and the replace $this->_directoryData with $subject->_directoryData
 
protected $_directoryData = null;

public function __construct(
\Magento\Directory\Helper\Data $directoryData
)
{
$subject->_directoryData = $directoryData;

}
But,still it was loading.. wt can i do
 
not here ,
in constructor it shoyld be $this->_directoryData
 
Before initialize directoryData.it throw telephone no reqiured error
 
but in function it should be $this->_directoryData not $subject->_directoryData
 
but already given that
 
10:26 AM
you have written $subject->_directoryData
and if it returns before whole function execute then fill all other data except last name
 
need to change in all other place $subject->_directoryData to $this->_directoryData
 
because now we are using our own directoryData object not our parent's
$subject is parent object
 
<?php

namespace Xxx\OptionalLastname\Plugin\Customer\Model\Address;

class AbstractAddress
{
protected $_directoryData = null;

public function __construct(
\Magento\Directory\Helper\Data $directoryData
)
{
$this->_directoryData = $directoryData;

}

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']);
 
10:36 AM
one more you missed to replace
see
<?php

namespace Xxx\OptionalLastname\Plugin\Customer\Model\Address;

class AbstractAddress
{
protected $_directoryData = null;

public function __construct(
\Magento\Directory\Helper\Data $directoryData
)
{
$this->_directoryData = $directoryData;

}

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']);
last condition has directoryData
 
<?php

namespace Xxx\OptionalLastname\Plugin\Customer\Model\Address;

class AbstractAddress
{
protected $_directoryData = null;

public function __construct(
\Magento\Directory\Helper\Data $directoryData
)
{
$this->_directoryData = $directoryData;

}

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']);
sorry,page was loading..........
 
now it should work
try this if it's working or not:

<?php

namespace Xxx\OptionalLastname\Plugin\Customer\Model\Address;

class AbstractAddress
{
protected $_directoryData = null;

public function __construct(
\Magento\Directory\Helper\Data $directoryData
)
{
$this->_directoryData = $directoryData;

}

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']);
 
it's throw Invalid method Magento\Quote\Model\Quote\Address\Interceptor::isTelephoneRequired
but i'm given phone no
 
remove that telephone condition also
 
10:51 AM
ok
 
may be your field name is different than this
 
Invalid method Magento\Quote\Model\Quote\Address\Interceptor::isCompanyRequired
 
add this two methods to avoid these 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());
}
and you have to initialize $this->_eavConfig in constructor
 
it throws is fax required?so,i'm remove fax validation before that company required error
 
no dear this all methods are protected in parent class so copy this methods from parent class
/**
* @return bool
* @since 100.2.0
*/
protected function isFaxRequired()
{
return ($this->_eavConfig->getAttribute('customer_address', 'fax')->getIsRequired());
}
 
10:58 AM
ok
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']);
is it correct
 
correct
 
butt again page loading
wt can i do
 
what is the error now?
 
just page was loading........
still now the page was loading
 
you have to test it proper dear
die on last line
 
11:04 AM
Ok
after di complie it throws Invalid method Magento\Quote\Model\Quote\Address\Interceptor::isTelephoneRequired
 
replace $this->isCompanyRequired() with $subject->isCompanyRequired()
and same as others
telephone and fax also need to replace $this with $subject
 
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']);
is it ok
 
sorry...again it show error............Please check the shipping address information.
 
what is the error?
 
11:23 AM
.Please check the shipping address information.
 
this is not related to our function
test with die in last line of the function
 
12:19 PM
again loading
 

last day (15 days later) »