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

Fixed erroneous call to getText() method in Mage_Usa_Model_Shipping_Carrier_Ups::getAllowedMethods() #4013

Merged
merged 2 commits into from
May 28, 2024

Conversation

ragnese
Copy link
Contributor

@ragnese ragnese commented May 27, 2024

We ran into an error when getting shipping methods,

[Fri May 24 17:07:36.650694 2024] [:error] [pid 29360] [client 209.215.171.162:7451] PHP Fatal error: Uncaught Error: Call to a member function getText() on string in /opt/data/sites/
www.hubindustrial.com/.modman/hub-src/app/code/local/Mage/Usa/Model/Shipping/Carrier/Ups.php:1344\nStack

The value in question can't ever be anything besides a string from what I see.

@github-actions github-actions bot added the Component: Usa Relates to Mage_Usa label May 27, 2024
@fballiano
Copy link
Contributor

@ragnese do you know how to reproduce the problem? because I'm trying to but it seems that the getAllowedMethods() is never called :-\ at least in the core

@ragnese
Copy link
Contributor Author

ragnese commented May 28, 2024

Hmm. Yeah, in our case this code was called by a custom shipping-related module/plugin that we have.

So, I'm not sure if/where it is used by Core. Since it's just a type error (and not a business logic bug), it would be easy to just insert a call to it anywhere you want and watch it crash. Like, you could literally just add,

$allowedMethods = (new Mage_Usa_Model_Shipping_Carrier_Ups())->getAllowedMethods();
var_dump($allowedMethods);

to any code path that you know is going to be called, and you will see the error.

The bug and fix are also very obvious. The for-loop in that method was copy+pasted from the Magento2 code base, but the getCode() method is moved in Magento2 to a helper class (Magento/Ups/Helper/Config) and the getCode() method on that class does not return (arrays of) strings, but instead returns instances of Magento/Framework/Phrase, which has a method getText().

It's clear that the getCode() in OpenMage only returns (arrays of) strings, and therefore will never return anything that has a getText() method.

@fballiano
Copy link
Contributor

@ragnese can you please make the PR editable?

I think the whole method should be this:

    public function getAllowedMethods()
    {
        $allowedMethods = explode(',', (string)$this->getConfigData('allowed_methods'));
        $availableByTypeMethods = $this->getCode('originShipment', $this->getConfigData('origin_shipment'));

        $methods = [];
        foreach ($availableByTypeMethods as $methodCode => $methodData) {
            if (in_array($methodCode, $allowedMethods)) {
                $methods[$methodCode] = $methodData;
            }
        }

        return $methods;
    }

because checkin for isXML or isRest is totally useless

@ragnese ragnese force-pushed the fix-ups-getAllowedMethods branch from a4f0ff1 to 254a847 Compare May 28, 2024 20:49
@ragnese
Copy link
Contributor Author

ragnese commented May 28, 2024

@fballiano Unfortunately, I'm not sure what it means to make the PR editable... What do I need to do, exactly?

As far as the weird ($isUpsXml || $isUpsRest) check, I agree that it's a little weird. It's how it's done in Magento 2, and I think it's done that way because they still have three options for the 'type' config: "UPS", "UPS_XML", and "UPS_REST". The "UPS" type used the old CGI methods. But we don't have that as an option anymore, so I agree that we can change the logic in this method.

But, I do wonder what's going to happen for users that used to have the type set to "UPS" before upgrading to the new version. I don't think it'll be smooth for them- there will probably be errors as it tries to use the REST API calls with now configured auth keys and stuff. I'm not sure if we should've tried to make the upgrade transition smoother...

@fballiano
Copy link
Contributor

it doesn't really matter if they have UPS in the configuration, UPS is the CGI API which doesn't work since months...

@ragnese ragnese force-pushed the fix-ups-getAllowedMethods branch from 4a57732 to 23878f8 Compare May 28, 2024 21:22
@ragnese ragnese force-pushed the fix-ups-getAllowedMethods branch from 23878f8 to 6ef89e1 Compare May 28, 2024 21:25
@ragnese
Copy link
Contributor Author

ragnese commented May 28, 2024

it doesn't really matter if they have UPS in the configuration, UPS is the CGI API which doesn't work since months...

Yeah, M2 opens a popup in this case, instead of actually using the CGI API:

    protected function _getCgiTracking($trackings)
    {
        //ups no longer support tracking for data streaming version
        //so we can only reply the popup window to ups.
        $result = $this->_trackFactory->create();
        foreach ($trackings as $tracking) {
            $status = $this->_trackStatusFactory->create();
            $status->setCarrier('ups');
            $status->setCarrierTitle($this->getConfigData('title'));
            $status->setTracking($tracking);
            $status->setPopup(1);
            $status->setUrl(
                "http://wwwapps.ups.com/WebTracking/processInputRequest?HTMLVersion=5.0&error_carried=true" .
                "&tracknums_displayed=5&TypeOfInquiryNumber=T&loc=en_US&InquiryNumber1={$tracking}" .
                "&AgreeToTermsAndConditions=yes"
            );
            $result->append($status);
        }

        $this->_result = $result;

        return $result;
    }

But, whatever. That's getting off topic from this specific PR, anyway.

Also, I think there may be a permission mismatch with my Github account and our organization, because I don't see the option to make the PR editable as shown in the link you posted. I'll have to see if I need more permissions for our org. :/

Copy link
Contributor

@fballiano fballiano left a comment

Choose a reason for hiding this comment

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

perfect

@fballiano fballiano changed the title Fix erroneous call to getText() method in Mage_Usa_Model_Shipping_Carrier_Ups::getAllowedMethods Fixed erroneous call to getText() method in Mage_Usa_Model_Shipping_Carrier_Ups::getAllowedMethods() May 28, 2024
@fballiano fballiano merged commit 1f32c70 into OpenMage:main May 28, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Usa Relates to Mage_Usa
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants