netresearch / dhl-module-ui

Module for providing the presentation layer within the Magento application for custom functionality
Open Software License 3.0
3 stars 0 forks source link

Cannot set parcel notification as default value #11

Closed template-provider closed 2 years ago

template-provider commented 2 years ago

Hi, as in #8 mentioned I try to add the default value of the parcel notification to enable. For this I created a class with a function like this:

    public function process(
        string $carrierCode,
        array $shippingOptions,
        int $storeId,
        string $countryCode,
        string $postalCode,
        ShipmentInterface $shipment = null
    ): array {
        foreach ($shippingOptions as $option) {
            if ($option->getCode() === 'parcelAnnouncement') {
                $inputArray = $option->getInputs();

                foreach ($inputArray as $input) {
                    if ($input->getCode() === 'enabled' && $input->getInputType() === 'checkbox') {
                        $input->setDefaultValue('1');
                    }
                }

                $option->setInputs($inputArray);
            }
        }

        return $shippingOptions;
    }

So far this works in the frontend. The checkbox is checked and the customer orders. But the parcel notification is not realy active, with creating the shipping label the notification is disabled. However, if I disable in the frontend the checkbox and enable it again, it works.

I digged a little into the code and found, that with the checking of the checkox the local storage gets refreshed with that entry:

        "cachedShippingOptionSelections": {
            "dhlpaket": {
                "parcelAnnouncement": {
                    "enabled": true
                }
            }
        },

So with an initial default value that "cachedShippingOptionSelections" is not created. Does anybody have an idea how to solve this? thx

mam08ixo commented 2 years ago

Do you use some one-step-checkout solution? In regular checkout, all selections made in the service box are persisted when proceeding to the payment step.

template-provider commented 2 years ago

@mam08ixo Yes, it is a one page checkout. You can check it yourself. https://shop.icletta.com/ Do you have any ideas to persist it after the loading? I am not a javascript guru ;-)

mam08ixo commented 2 years ago

If the service selection does not get cached and persisted in your custom checkout implementation, then you could try patching/replacing the service input component to add an initialize function that sets the current (default) selection already on load:

initialize: function () {
    this._super();

    if (this.value() !== '') {
        selections.addSelection(
            this.shippingOption.code,
            this.shippingOptionInput.code,
            this.value()
        );
    }
}
template-provider commented 2 years ago

@mam08ixo did work like a charm ... thx for the help