netresearch / dhl-module-shipping-m2

This extension is in legacy status since 04/2020 and will run out of maintenance and support after a short transition period. You can find the official replacement extension here https://github.com/netresearch/dhl-shipping-m2. It includes the latest and greatest possible range of functions that DHL is currently offering.
30 stars 20 forks source link

feature request: "street.nr" #36

Closed MarcusWolschon closed 6 years ago

MarcusWolschon commented 6 years ago

Feature description "xyzstrasse 12" is supported. The format "xyzstr.12" should also be supported.

Reason It is a format users in Germany like to enter and this module failed to find the separation between street-name and house number

ngolatka commented 6 years ago

@MarcusWolschon : please see the rather lengthy discussion in issue #20 about this topic. You asked the same question there, and it was also answered by another user.

As I already explained in issue #20, keep in mind that even if we somehow fixed the address that is sent to DHL, it would still remain incorrect in the Magento order, and potentially cause issues for other modules that want to process it (e.g. payment modules that do fraud detection). It is not the module's responsibility to compensate for a customer's laziness.

MarcusWolschon commented 6 years ago

For anyone who finds this issue. Here is a very simple plugin that intercept the ShippingInformationManagementPlugin of the DHL plugin that itself intercepts the Magento ShippingInformationManagement and normalizes the address. This way DHL can parse it and make out street name and house number, yet the original address in the order is completly untouched.

<?php
namespace Wolschon\DHLAddress\Plugin\Checkout;

use Dhl\Shipping\Api\QuoteAddressExtensionRepositoryInterface;
use Dhl\Shipping\Model\ShippingInfo\AbstractAddressExtension;
use Dhl\Shipping\Model\ShippingInfoBuilder;
use Dhl\Shipping\Model\ShippingInfo\QuoteAddressExtensionFactory;
use Magento\Checkout\Api\Data\ShippingInformationInterface;
use Magento\Checkout\Model\ShippingInformationManagement;
use Dhl\Shipping\Plugin\Checkout\ShippingInformationManagementPlugin;
use Magento\Quote\Api\Data\AddressInterface;

class NormalizeShippingInformationPlugin {

        public function beforebeforeSaveAddressInformation(
                ShippingInformationManagementPlugin $subject,
                ShippingInformationManagement $management,
                $cartId,
                ShippingInformationInterface $addressInformation) {

         // \Magento\Quote\Api\Data\AddressInterface
                $address = $addressInformation->getShippingAddress();
                $street = $address->getStreet();

                $street = str_replace(array("Str.", "str."), array("Str. ", "str. "), $street);
                $street = str_replace(array("  "), array(" "), $street);

                $address = clone $address;
                $address->setStreet($street);
                $addressInformation = clone $addressInformation;
                $addressInformation->setShippingAddress($address);
         return array($management, $cartId, $addressInformation);
        }
}

?>