2

I have a plugin to reorder the address lines in Commerce. Basically to move the Phone Number to the bottom as it drives all my clients nuts.

While the final array, addressLines is the correct array and all looks ok it does not seem to be passing $addressLines back into getAddressLines function in /src/models/Address.php Am I missing the obvious here or is there a bug?

Event::on(
            Address::class,
            Address::EVENT_DEFINE_ADDRESS_LINES,
            function(DefineAddressLinesEvent $event) {
                // @var array $addressLines
                $addressLines = $event->addressLines;
                // Modify default address lines array
                $addressLinesNewOrder = [
                    'attention',
                    'name',
                    'fullName',
                    'address1',
                    'address2',
                    'address3',
                    'city',
                    'zipCode',
                    'stateText',
                    'countryText',
                    'phone',
                    'alternativePhone',
                    'label',
                    'notes',
                    'businessName',
                    'businessTaxId',
                    'custom1',
                    'custom2',
                    'custom3' ,
                    'custom4',
            ];
            uksort($addressLines, function($x, $y) use ($addressLinesNewOrder) {
                return array_search($x, $addressLinesNewOrder) > array_search($y, $addressLinesNewOrder);
          });

        return $addressLines;

    });

https://craftcms.com/docs/commerce/3.x/addresses.html#address-lines https://craftcms.com/docs/commerce/3.x/extend/events.html#defineaddresslines

1

1 Answer 1

1

I have return $addressLines; when it should be $event->addressLines = $addressLines;. In an event the idea is to manipulate the event variables, these will then be used by the code later on. My full event code would look like:

Event::on(Address::class, Address::EVENT_DEFINE_ADDRESS_LINES, function(DefineAddressLinesEvent $event) {
    // @var array $addressLines
    $addressLines = $event->addressLines;
    // Modify default address lines array
    $addressLinesNewOrder = [
        'attention',
        'name',
        'fullName',
        'address1',
        'address2',
        'address3',
        'city',
        'zipCode',
        'stateText',
        'countryText',
        'phone',
        'alternativePhone',
        'label',
        'notes',
        'businessName',
        'businessTaxId',
        'custom1',
        'custom2',
        'custom3',
        'custom4',
    ];
    uksort($addressLines, function($x, $y) use ($addressLinesNewOrder) {
        return array_search($x, $addressLinesNewOrder) > array_search($y, $addressLinesNewOrder);
    });

    $event->addressLines = $addressLines;
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.