Forum

Thread tagged as: Question, Shop

Fetch current Cart ID

Hi!

I would like to be able to retrieve the current cart ID, is there any native perch function for that? From what I saw, the perch_shop_cart doesn't return the id of the current cart neither any of the other native functions . In my case, I need the id to pass it to a webservice that uses the perchDB API "get_rows" and fetches extra information for a given cart.

Hope you can help

Gustavo Bica

Gustavo Bica 0 points

  • 3 years ago

I ended up finding a solution, basically I fetch the current user ID and then use that id to find the most recent cart ID for that user in particular. For anyone looking for a way to do that here it is an example:

$memberID = perch_member_get('id');
$DBPerch = PerchDB::fetch();
$cartID = $DBPerch->get_value('SELECT cartID FROM perch3_shop_cart WHERE memberID='.$memberID.' ORDER BY cartID DESC');

I also ended up adding a new function to perch shop, the example that I provided earlier only works if the user already has an id associated to him. Because of that, I added the following code to perch/addons/apps/perch_shop/lib/PerchShop_Runtime.php

public function getCartID()
    {
        if (!$this->cart_id) {
            $this->Cart = new PerchShop_Cart($this->api);
            $this->cart_id = $this->Cart->init();
            return $this->cart_id;
        }
        return $this->cart_id;
    }

and perch/addons/apps/perch_shop/runtime/cart.php

function perch_get_cartID()
    {
        $ShopRuntime = PerchShop_Runtime::fetch();
        return $ShopRuntime->getCartID();
    }

Now I can just call

<?php perch_get_cartID()?>