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

Only create a cart if at least one product was added #280

Closed
tobias-kuendig opened this issue Aug 7, 2019 · 6 comments
Closed

Only create a cart if at least one product was added #280

tobias-kuendig opened this issue Aug 7, 2019 · 6 comments
Assignees

Comments

@tobias-kuendig
Copy link
Member

    public static function putSession($sessionId)
    {
        Cookie::queue('cart_session_id', $sessionId, 9e6);
        Session::put('cart_session_id', $sessionId);
    }

    public static function createSession(): Cart
    {
        $sessionId = Session::get('cart_session_id') ?? Cookie::get('cart_session_id') ?? str_random(100);
        self::putSession($sessionId);
        return self::orderBy('created_at', 'DESC')->firstOrCreate(['session_id' => $sessionId]);
    }

    protected static function fromSession(): ?Cart
    {
        $sessionId = Session::get('cart_session_id') ?? Cookie::get('cart_session_id');
        if (Session::get('cart_session_id')) {
            self::putSession($sessionId);
        }
        return self::where('session_id', $sessionId)->orderBy('created_at', 'DESC')->first();
    }

Credits to samuell on Slack

@tobias-kuendig tobias-kuendig self-assigned this Aug 7, 2019
@RobertCh2
Copy link

Is this implemented? My table offline_mall_carts is getting very big with time, maybe this is the reason?

@tobias-kuendig
Copy link
Member Author

No it is not implemented yet but probably the reason your table is large. You can use a query like this to get old carts and delete them:

$q = \OFFLINE\Mall\Models\Cart
            ->where('customer_id', null)
            ->where('updated_at', '<', now()->subDays(3))
            ->get()
            ;

@RobertCh2
Copy link

The table is getting very large very quickly, like 500-600 new carts a day. Every few minutes, irregular intervals. I don't have such a traffic on my site. What could be the reason?

@tobias-kuendig
Copy link
Member Author

Do you have server access logs? Could be bots indexing your site

@RobertCh2
Copy link

I have checked logs. Indeed the traffic is from bots, mainly Googlebot, bingbot and PetalBot. So I have to clean the Carts table very often.

tobias-kuendig added a commit that referenced this issue Aug 21, 2024
Previously, every page visit created a cart. This was very inefficient
and lead to huge tables on high-traffic websites. Carts are now only
created once they are actually saved, for example when a product is
added.

Fixes #280, #1064
tobias-kuendig added a commit that referenced this issue Aug 21, 2024
Previously, every page visit created a cart. This was very inefficient
and lead to huge tables on high-traffic websites. Carts are now only
created once they are actually saved, for example when a product is
added.

Fixes #280, #1064
@tobias-kuendig
Copy link
Member Author

Sometimes a small change goes a long way. This is fixed in v3.4.3 (107ed12)

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

No branches or pull requests

2 participants