Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AddressController.php #34152

Closed
wants to merge 12 commits into from
Closed

Fix AddressController.php #34152

wants to merge 12 commits into from

Conversation

metacreo
Copy link

@metacreo metacreo commented Oct 3, 2023

Questions Answers
Branch? 8.1.x
Description? Duplicate function FillWith call in AddressController.php. Address form filled two times. First time normal, second time with empty country. If default country is US, form shows needed states to enter. Fix bugs: #35013, #34080, #33991
Type? bug fix
Category? FO
BC breaks? no
Deprecations? no
How to test? Indicate how to verify that this change works as expected.
UI Tests Please run UI tests and paste here the link to the run. Read this page to know why and how to use this tool..
Fixed issue or discussion? Fixes #{issue number here}, Fixes #{another issue number here}, Fixes https://github.com/PrestaShop/PrestaShop/discussions/ {#35013 #34080 #33991}

Duplicate call function. Address form filled second time. Fix bugs: 
#34080
and
#33991
@metacreo metacreo requested a review from a team as a code owner October 3, 2023 17:17
@prestonBot
Copy link
Collaborator

Hello @metacreo!

This is your first pull request on the PrestaShop project. Thank you, and welcome to this Open Source community!

@prestonBot
Copy link
Collaborator

Hi, thanks for this contribution!

I found some issues with the Pull Request description:

  • The category should be one of these: BO, CO, FO, IN, LO, ME, PM, TE or WS (Read explanation)

Would you mind having a look at it? This will help us understand how interesting your contribution is, thank you very much!

(Note: this is an automated message, but answering it will reach a real human)

@prestonBot prestonBot added Bug fix Type: Bug fix BC break Type: Introduces a backwards-incompatible break labels Oct 3, 2023
Copy link
Contributor

@mflasquin mflasquin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good, but can you tell us where the address form is already filled in the code ? thanks

@metacreo
Copy link
Author

metacreo commented Oct 4, 2023

@mflasquin
AddressController.php

   public function postProcess()
    {
        $this->context->smarty->assign('editing', false);
        $id_address = (int) Tools::getValue('id_address');
        // Initialize address if an id exists
        if ($id_address) {
            $this->address_form->loadAddressById($id_address);
        }

        // Fill the form with data
        $this->address_form->fillWith(Tools::getAllValues());

CustomerAddressForm.php

   public function loadAddressById($id_address)
    {
        $context = Context::getContext();

        $this->address = new Address($id_address, $this->language->id);

        if ($this->address->id === null) {
            return Tools::redirect('pagenotfound');
        }

        if (!$context->customer->isLogged() && !$context->customer->isGuest()) {
            return Tools::redirect('/index.php?controller=authentication');
        }

        if ($this->address->id_customer != $context->customer->id) {
            return Tools::redirect('pagenotfound');
        }

        $params = get_object_vars($this->address);
        $params['id_address'] = $this->address->id;

        return $this->fillWith($params);
    }

    public function fillWith(array $params = [])
    {

As mentioned above public function fillWith(array $params = []) called twice. Second call destroys country->id
classes/form/CustomerAddressForm.php

    public function fillWith(array $params = [])
    {
$trace = debug_backtrace();
dump("called by {$trace[1]['class']} :: {$trace[1]['function']}");
        // This form is tricky: fields may change depending on which country is being selected!

result:
Received normal address
^ "called by CustomerAddressFormCore :: loadAddressById"
Received same address but with default country.
^ "called by AddressControllerCore :: postProcess"

Fix:
AddressController.php Line 69
//$this->address_form->fillWith(Tools::getAllValues());
No bugs. Everything works.

mflasquin
mflasquin previously approved these changes Oct 4, 2023
Copy link
Contributor

@mflasquin mflasquin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kpodemski
Copy link
Contributor

Hello @metacreo

It looks legit. What I suggest now:

  • rebase into 8.1.x branch, so that we can all have this fix before PrestaShop 9 :)
  • help QA reproduce this issue without using demo.prestashop.com, the origin of the installation shouldn't matter - so kindly provide them with the steps that allow them to reproduce the issue - from what I see the confusion is between adding and editing the address

@metacreo
Copy link
Author

metacreo commented Oct 4, 2023

rebase ok, but I don`t known how to help QA. They cannot reproduce, although I reproduced on 3 production servers and on demo. There no additional steps. Just go to my account and click Update.

@Hlavtox
Copy link
Contributor

Hlavtox commented Oct 4, 2023

Hi @metacreo, if it happens only in some browsers, I don't know how JS error is related to removing something on PHP side?

@metacreo
Copy link
Author

metacreo commented Oct 4, 2023

@Hlavtox Hello.
The fact of the matter is that, for example, on server 1 I only have it with Firefox, on server 2 only with Chrome, and on server 3 on both browsers. PHP versions are identical, PS versions identical. I think that this difference is still related to PHP.

Ok. let to try see what shows dump where no bug in form...

Edit:
Tried. Steel double call
^ "called by CustomerAddressFormCore :: loadAddressById"
^ "called by AddressControllerCore :: postProcess"
but Form is ok, because second address country not US, is my local.
Bump... this is related to country detection from browser... But any way.. second call not needed.

Copy link
Contributor

@matthieu-rolland matthieu-rolland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for your contribution @metacreo 👍

Would you mind changing the base of the PR (setting the base as 8.1.x instead of develop)

@metacreo
Copy link
Author

metacreo commented Oct 4, 2023

Please wait. After 8.1.1 -> 8.1.2 This fix not works.

@prestonBot prestonBot added the 8.1.x Branch label Oct 4, 2023
Initialize address if an id exists and prevent fill form with default country second time. If this is only form request. Add param id_country for CustomerAddressForm class.
Copy link
Author

@metacreo metacreo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need review. Basically need serious rework of this functions in CustomerAddressForm, AbstractForm and AddressControllerCore.
In order to not touch a lot of code, the only solution for now is this.

Copy link
Author

@metacreo metacreo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small optimization. Checked on 8.1.1 and 8.1.2. My account address edit, add. Checkout process edit, add.

@metacreo
Copy link
Author

metacreo commented Oct 6, 2023

Seems to final of PR ;)

mflasquin
mflasquin previously approved these changes Oct 6, 2023
if ($id_address) {
$this->address_form->loadAddressById($id_address);
// If this is only form request. Adding param id_country for CustomerAddressForm class.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a hack honestly, I'll have to look deeper since I'm not sure if we should have to do that there

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kpodemski no... this param needs only for correct form fill.

@metacreo
Copy link
Author

metacreo commented Oct 7, 2023

@kpodemski
this original $this->address_form->fillWith(Tools::getAllValues()); not looks as hack for you ?

tleon
tleon previously approved these changes Nov 22, 2023
@ps-jarvis ps-jarvis added the Waiting for QA Status: action required, waiting for test feedback label Nov 22, 2023
@Hlavtox Hlavtox removed the Waiting for QA Status: action required, waiting for test feedback label Nov 22, 2023
Copy link
Contributor

@Hlavtox Hlavtox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@metacreo Please fill the PR form properly and add some description. Can't process any further without description. :-)

Moreover, #34080 is talking about some javascript issues that were not even reproduced. It should be verified and reproduced first.

@ps-jarvis ps-jarvis added the Waiting for author Status: action required, waiting for author feedback label Nov 22, 2023
@metacreo
Copy link
Author

@Hlavtox Issue reproduced in #33991 See comment 1729389392 Just QA does not have proper qualification to reproduce this bug.
Bug described in details #34080. No need to duplicate description. I fixed everything on my servers a long time ago, and if you don’t want to, don’t accept it... Closing PR...

@prestashop-issue-bot prestashop-issue-bot bot removed the Waiting for author Status: action required, waiting for author feedback label Nov 22, 2023
@Hlavtox
Copy link
Contributor

Hlavtox commented Nov 22, 2023

@metacreo I think I managed to reproduce the issue, I will get back to you.

@kpodemski
Copy link
Contributor

@Hlavtox, I'm trying to do some cleaning in PRs, did you really manage to reproduce the issue? 🤔

@Hlavtox
Copy link
Contributor

Hlavtox commented Dec 28, 2023

@kpodemski Well, somehow - try it #34080 (comment)

@metacreo
Copy link
Author

Screencast.from.28.12.2023.16.30.36.webm

No need second 0 param in getValue, because (int).
@metacreo metacreo dismissed stale reviews from tleon and LouisAUTHIE via a5e8553 January 10, 2024 18:14
@Hlavtox
Copy link
Contributor

Hlavtox commented Jan 10, 2024

@metacreo Could you please check, if the address form uses geolocation when creating new address?

I think that by moving the fillWith call, this logic is not processed at all.

Tools::isCountryFromBrowserAvailable() &&
            Country::getByIso($countryIsoCode = Tools::getCountryIsoCodeFromHeader(), true)

@metacreo
Copy link
Author

metacreo commented Jan 10, 2024

@Hlavtox

I think that by moving the fillWith call, this logic is not processed at all.

Tested. This logic not depend on $this->address_form->fillWith(Tools::getAllValues()); in AddressController.php

function fillWith in real called 3 times in original...
2 when form is called and 1 when submitted .. so 3 at all
when called, one is extra
we need change postProcces fn only.... because via postProcces form init and via postProcces form submit.

Detection works ok, but you need all lang-pack to be installed for "auto" country...
Detection works only with new customer who not have addr. after add addr... default country getting from first created addr.

and one more comment: this is not "geolocation", this functions use browser headers 🙂

@metacreo
Copy link
Author

metacreo commented Jan 10, 2024

@Hlavtox I use Moz FF.
set accept lang header via about:config for example fr-FR or en-US or ru-RU (presta reads first value of header) or all comma separated
make sure lang pack in BO installed and setting "Set default country from browser language" is ON
presta grab lang from accept lang header first locale

CustomerAddressForm.php

    public function fillWith(array $params = [])
    {
@dump(Tools::isCountryFromBrowserAvailable());
@dump(Tools::getCountryIsoCodeFromHeader());

Try move $this->address_form->fillWith(Tools::getAllValues()); in AddressController.php
Try change browser locale (accept-lang).

@Hlavtox we need exactly move $this->address_form->fillWith(Tools::getAllValues()); to after submit. Because postProcess function called not only if form submitted but on form init too. That`s now it works ps FrontController... even ajax call goes over postProcess, thus called $this->address_form->loadAddressById($id_address) to fill form.
In this case, this bug occurs precisely because the function is called one extra time. When this call is made, the form is filled in with the default country.

I don’t understand why you are going into the weeds, starting to rewrite code that has been working perfectly for many years

@metacreo
Copy link
Author

@ChillCode hello,
Please continue here #34636

@metacreo
Copy link
Author

I found out where this whole story came from #27187

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
8.1.x Branch BC break Type: Introduces a backwards-incompatible break Bug fix Type: Bug fix
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

9 participants