Skip to content

Fix: use session's customer groups correctly#1453

Merged
alecritson merged 3 commits intolunarphp:0.7from
se09deluca:fix/storefront-session-customer-groups
Jan 11, 2024
Merged

Fix: use session's customer groups correctly#1453
alecritson merged 3 commits intolunarphp:0.7from
se09deluca:fix/storefront-session-customer-groups

Conversation

@se09deluca
Copy link
Contributor

Previously saved customerGroups are not used

This occurs because the condition present in the second if is always true when the previous one is false and the latest return instruction is never reached.
This always involves using the default customerGroup, ignoring customer groups from the session.

public function initCustomerGroups()
{
    $groupHandles = collect(
        $this->sessionManager->get(
            $this->getSessionKey().'_customer_groups'
        )
    );

    if ($this->customerGroups?->count()) {
        if (! $groupHandles) {
            return $this->setCustomerGroups(
                $this->customerGroups
            );
        }

        return $this->customerGroups;
    }

    if (! $this->customerGroups?->count()) {
        return $this->setCustomerGroups(
            collect([
                CustomerGroup::getDefault(),
            ])
        );
    }

    return $this->setCustomerGroups(
        CustomerGroup::whereIn('handle', $groupHandles)->get()
    );
}

this PR uses the $groupHandles (session's customer group) to initialise the current customer groups, if something is present, otherwise it uses the default one.

public function initCustomerGroups()
{
    $groupHandles = collect(
        $this->sessionManager->get(
            $this->getSessionKey().'_customer_groups'
        )
    );

    if ($this->customerGroups?->count()) {
        if (! $groupHandles) {
            return $this->setCustomerGroups(
                $this->customerGroups
            );
        }

        return $this->customerGroups;
    }

    if (count($groupHandles) > 0) {
        return $this->customerGroups = CustomerGroup::whereIn('handle', $groupHandles)->get();
    }

    return $this->setCustomerGroups(
        collect([
            CustomerGroup::getDefault(),
        ])
    );
}

PS: the if (count($groupHandles) > 0) doesn't use the setCustomerGroups method because the session already contain those values, overwriting them is useless.

@vercel
Copy link

vercel bot commented Jan 7, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lunar-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 11, 2024 9:28am

@se09deluca se09deluca changed the title fix: use session's customer groups correctly Fix: use session's customer groups correctly Jan 7, 2024
@alecritson alecritson self-requested a review January 11, 2024 08:47
Copy link
Collaborator

@alecritson alecritson left a comment

Choose a reason for hiding this comment

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

Good spot and thanks for the PR :)

@alecritson alecritson merged commit 53571f8 into lunarphp:0.7 Jan 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants