cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2.4.1 problem - Notice: Undefined variable: qty in

Magento 2.4.1 problem - Notice: Undefined variable: qty in

Hi there,

 

I encounter this problem suddenly in few weeks before. The system have no extension update.

Most interesting is it only appear in one store view, the other store don't have such issue.

There has been an error processing your request

Notice: Undefined variable: qty in /magento/vendor/amasty/color-swatches-pro/Model/ResourceModel/Inventory.php on line 185

Error log record number: fd1ead955e0746d2b1af2d1c761c7e77a75bdc6e4b809bd45a481635cad73545

 

Here is the code and line 185 is "return $qty;"

 

    public function getMsiQty($productSku, $websiteCode)
    {   
        if (is_array($productSku)) {
            $itemQty = $this->getItemsQty($productSku, $websiteCode); 
            $reservationsQty = $this->getReservationsQty($productSku, $this->getStockId($websiteCode));
            foreach ($productSku as $sku) {
                $qty[$sku] = ($itemQty[$sku] ?? 0) + ($reservationsQty[$sku] ?? 0);         
            }
        } else {
            if (!isset($this->qty[$websiteCode][$productSku])) {
                $this->qty[$websiteCode][$productSku] = $this->getItemQty($productSku, $websiteCode)
                    + $this->getReservationQty($productSku, $this->getStockId($websiteCode));
            }
            $qty = $this->qty[$websiteCode][$productSku];
        }

        return $qty;
    }

I have no idea why it only happens on Chinese language store view but not English one.

 

Looking forward to any comments. Thank you.

 

Regards,

kf

 

3 REPLIES 3

Re: Magento 2.4.1 problem - Notice: Undefined variable: qty in

Hi @kf_luk,

 

Look at your function getMsiQty(), when running into the if round, the variable $qty has not been declared if it does not go to the foreach round then $qty does not exist so when you return $ qty at the end of the function it will display error message as you have seen.

 

To fix the problem you can declare $qty = 0 at the beginning of the function

public function getMsiQty($productSku, $websiteCode, $qty = 0)
    {
        //--------code here--------//
        return $qty;
    }

Hope this can help you! Let me know if you need further assistance.

_________

If issue solved, Click Kudos & Accept as Solution.

Marcus | LitExtension Expert
https://litextension.com/

Re: Magento 2.4.1 problem - Notice: Undefined variable: qty in

Hi Marcus

 

Thanks for your comments.

After upgrade to 2.4.2 and update the extensions. The error gone.

 

Regards,

kf

Re: Magento 2.4.1 problem - Notice: Undefined variable: qty in

Hi @kf_luk,

 

Great to hear that! If you need further assistance, please let me know.

Marcus | LitExtension Expert
https://litextension.com/