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

Update class.Cart.php #22

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Update class.Cart.php
Added updateAttributes method
  • Loading branch information
JamesShaver authored Aug 12, 2024
commit aa3ec8192df8d9e7280c28b2a5984e6ab09cc190
31 changes: 30 additions & 1 deletion class.Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,35 @@ public function update($id, $quantity = 1, $attributes = [])
return false;
}

/**
* Update the attributes of a specific item in the cart.
*
* @param string $id
* @param string $hash
* @param array $attributes
*
* @return bool
*/
public function updateAttributes($id, $hash, $attributes = [])
{
if (isset($this->items[$id])) {
foreach ($this->items[$id] as $index => $item) {
if ($item['hash'] == $hash) {
// Update only the attributes that are provided
foreach ($attributes as $key => $value) {
$this->items[$id][$index]['attributes'][$key] = $value;
}

$this->write(); // Save the changes to the session

return true;
}
}
}

return false;
}

/**
* Remove item from cart.
*
Expand Down Expand Up @@ -360,4 +389,4 @@ private function write()
$_SESSION[$this->cartId] = json_encode(array_filter($this->items));
}
}
}
}