3

I want to Create a new address type "neighbors address / alternate address " . To assign it to customer i have created new customer attribute ( just like "default_shipping" and "default_billing" ) "alternate_address" and saved address_id against it . But how to integrate it with Quote and order . They only save a string "shipping or billing" in address_type column . Where i would like to have it as alternate

  • Hello did you find any workaround to create an extra address set because I also want to do the same thing! Thanks in advance – dandrits Apr 12 '19 at 9:43
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. - From Review – fmsthird Apr 12 '19 at 10:04
  • I know that this is now a question but I don't really have the reputation needed. – dandrits Apr 12 '19 at 10:22
  • @dandrits Check answer . – Vishwas Bhatnagar Apr 12 '19 at 11:52
  • @VishwasBhatnagar I created new address type using installer script. Now how can I add it in frontend and save in table ? – Kishan Patadia Sep 16 '19 at 12:21
1

To solve the issue i created a plugin

<type name="Magento\Sales\Model\Order\Address\Validator">
    <plugin  name="Mycompany_Sales_after_validator" sortOrder="10" type="MyCompany\Sales\Plugin\Magento\Sales\Model\Order\Address\Validator"/>
</type>

And in plugin class

namespace MyCompany\Sales\Plugin\Magento\Sales\Model\Order\Address;

use Magento\Sales\Model\Order\Address;
use Magento\Sales\Model\Order\Address\Validator as parentValidator;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Validator\EmailAddress;

/**
 * Class Validator
 * @package MyCompany\Sales\Plugin\Magento\Sales\Model\Order\Address
 */
class Validator
{
    const REGISTRED_ADDRESS = 'alternate';
    /**
     * @var array
     */
    protected $required = [
        'parent_id' => 'Parent Order Id',
        'postcode' => 'Zip code',
        'lastname' => 'Last name',
        'street' => 'Street',
        'city' => 'City',
        'email' => 'Email',
        'country_id' => 'Country',
        'firstname' => 'First Name',
        'address_type' => 'Address Type',
    ];


    /**
     * @param Address\Validator $subject subject
     * @param callable          $proceed Closure
     * @param Address           $address Address
     * @return array
     * @throws LocalizedException
     * @throws \Zend_Validate_Exception
     */
    public function aroundValidate(
        parentValidator $subject,
        callable $proceed,
        Address $address
    ) {
        $warnings = [];
        foreach ($this->required as $code => $label) {
            if (!$address->hasData($code)) {
                $warnings[] = sprintf('%s is a required field', $label);
            }
        }

        if (!\Zend_Validate::is($address->getEmail(), EmailAddress::class)) {
            $warnings[] = 'Email has a wrong format';
            throw new LocalizedException(__('Please enter a valid email address.'));
        }
        if (!in_array($address->getAddressType(), [
                                                   Address::TYPE_BILLING,
                                                   Address::TYPE_SHIPPING,
                                                   self::REGISTRED_ADDRESS,
            ])) {
            $warnings[] = 'Address type doesn\'t match required options';
        }

        return $warnings;
    }//end aroundValidate()


}//end class

This is old implementation(In after plugin there were no input parameter) and might not be best implementation. Try if you can avoid around plugin and also call proceed .

| improve this answer | |
  • I created new address type using installer script. Now how can I add it in frontend and save in table ? – Kishan Patadia Sep 16 '19 at 12:38
  • Yes just save the address id against the attribute value for customer – Vishwas Bhatnagar Sep 16 '19 at 12:50
  • Can you tell me in which table I need to save address id and from where I can find address id ? – Kishan Patadia Sep 16 '19 at 13:02

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.