concretecms-community-store / community_store

An open, free and community developed eCommerce system for Concrete CMS
https://concretecms-community-store.github.io/community_store/
MIT License
106 stars 66 forks source link

Undefined array key "variation" #702

Closed ajenkins-dev closed 1 year ago

ajenkins-dev commented 1 year ago

This is on the latest releases of ConcreteCMS 9.1.3 and Community Store, running on PHP 8.0.25.

The error occurs when adding an item to the cart that has no variations and when it's stock is limited.

Undefined array key "variation" /packages/community_store/src/CommunityStore/Cart/Cart.php

if ($stockLimited) {
                        if ($cartitem['product']['variation']) {
                            if (!isset($stockTotals['variations'][$cartitem['product']['variation']])) {
                                $stockTotals['variations'][$cartitem['product']['variation']] = ['cartQuantity' => $cartitem['product']['qty'], 'maxQuantity' => $maxQuantity];
                            } else {
                                $stockTotals['variations'][$cartitem['product']['variation']]['cartQuantity'] += $cartitem['product']['qty'];
                            }
                        } else {
                            if (!isset($stockTotals['products'][$cartitem['product']['pID']])) {
                                $stockTotals['products'][$cartitem['product']['pID']] = ['cartQuantity' => $cartitem['product']['qty'], 'maxQuantity' => $maxQuantity];
                            } else {
                                $stockTotals['products'][$cartitem['product']['pID']]['cartQuantity'] += $cartitem['product']['qty'];
                            }
                        }
                    }

I changed it to:

if ($stockLimited) {
                        if (isset($cartitem['product']['variation'])) {
                            if (!isset($stockTotals['variations'][$cartitem['product']['variation']])) {
                                $stockTotals['variations'][$cartitem['product']['variation']] = ['cartQuantity' => $cartitem['product']['qty'], 'maxQuantity' => $maxQuantity];
                            } else {
                                $stockTotals['variations'][$cartitem['product']['variation']]['cartQuantity'] += $cartitem['product']['qty'];
                            }
                        } else {
                            if (!isset($stockTotals['products'][$cartitem['product']['pID']])) {
                                $stockTotals['products'][$cartitem['product']['pID']] = ['cartQuantity' => $cartitem['product']['qty'], 'maxQuantity' => $maxQuantity];
                            } else {
                                $stockTotals['products'][$cartitem['product']['pID']]['cartQuantity'] += $cartitem['product']['qty'];
                            }
                        }
                    }

and it seems to have resolved the issue.

Mesuva commented 1 year ago

Thank you for this, wasn't one I was going to pick up easily. I've pushed up a fix.