Mathlight
Mathlight

Reputation: 6653

OpenCart remove option from cart

Maybe it's very simple, but I can't get my head around it.

I want to be able to remove an option from an product, and if the product doesn't have anymore options delete the product from the cart.

I've looked at the session value, but I don't see a possibility there:

2014-03-14 15:18:30 - Session Object
(
    [data] => Array
        (
            [language] => nl
            [currency] => EUR
            [cart] => Array
                (
                    [63:YToxOntpOjI7YTozOntpOjA7czoxOiIyIjtpOjE7czoyOiI0MSI7aToyO3M6MjoiODAiO319:YTozOntpOjI7YToxOntpOjA7czoxOiIxIjt9aTo0MTthOjE6e2k6MDtzOjE6IjIiO31pOjgwO2E6MTp7aTowO3M6MToiMSI7fX0=] => 1
                )

            [captcha] => dc9c56
            [customer_id] => 1
        )

)

I also don't see data in the database that could manipulated easily.

I've looked at the library/cart.php file, but it seems that it isn't possible...

(the code there: )

public function remove($key) {
    if (isset($this->session->data['cart'][$key])) {
        unset($this->session->data['cart'][$key]);
    }

    $this->data = array();
}

So does anybody knows an way ( nice or dirty, don't care ) of how to do this? Maybe receiving every product and delete the one and insert?

Upvotes: 2

Views: 1173

Answers (1)

jx12345
jx12345

Reputation: 1670

The

[63:YToxOntpOjI7YTozOntpOjA7czoxOiIyIjtpOjE7czoyOiI0MSI7aToyO3M6MjoiODAiO319:YTozOntpOjI7YToxOntpOjA7czoxOiIxIjt9aTo0MTthOjE6e2k6MDtzOjE6IjIiO31pOjgwO2E6MTp7aTowO3M6MToiMSI7fX0=] => 1

is the product_id (63) followed by a serilaized base64_encoded string which represents the product options.

You could decode this and unserialize it into an array, then remove the relevant option, check to see if there are any left, etc...

The getProducts() method of the Cart unserializes and decodes this. Here's the relevant code:

foreach ($_SESSION['cart'] as $key => $quantity) {
        $is_on_special = 0;
        $product = explode(':', $key);
        $product_id = $product[0];
        $stock = true;

        // Options
        if (isset($product[1])) {
          $options = unserialize(base64_decode($product[1]));
        } else {
          $options = array();
        }       

Upvotes: 2

Related Questions