feat: database support

This commit is contained in:
Joel Male
2025-02-25 16:03:17 +10:00
parent ec99aa0563
commit 0f07b9ae8a
12 changed files with 4882 additions and 882 deletions
+18 -13
View File
@@ -15,11 +15,6 @@ class Cart
protected $instanceName;
protected $customStorage = false;
protected $sessionKey;
protected $sessionKeyCartItems;
protected $sessionKeyCartConditions;
protected $config;
protected $currentItemId;
@@ -168,7 +163,7 @@ public function add(
*/
public function update(int|string $id, array $data): bool
{
if ($this->fireEvent('Updating', $data) === false) {
if ($this->fireEvent('Updating', 'item', $data) === false) {
return false;
}
@@ -201,7 +196,7 @@ public function update(int|string $id, array $data): bool
$this->save($cart);
$this->fireEvent('Updated', $item);
$this->fireEvent('Updated', 'item', $item);
return true;
}
@@ -260,7 +255,7 @@ public function remove($id)
{
$cart = $this->getContent();
if ($this->fireEvent('Removing', $id) === false) {
if ($this->fireEvent('Removing', 'item', $id) === false) {
return false;
}
@@ -268,7 +263,7 @@ public function remove($id)
$this->save($cart);
$this->fireEvent('Removed', $id);
$this->fireEvent('Removed', 'item', $id);
return true;
}
@@ -714,7 +709,7 @@ protected function validate($item)
*/
protected function addItem($id, $item)
{
if ($this->fireEvent('Adding', $item) === false) {
if ($this->fireEvent('Adding', 'item', $item) === false) {
return false;
}
@@ -724,7 +719,7 @@ protected function addItem($id, $item)
$this->save($cart);
$this->fireEvent('Added', $item);
$this->fireEvent('Added', 'item', $item);
return true;
}
@@ -815,9 +810,19 @@ public function setDecimals($decimals)
$this->decimals = $decimals;
}
protected function fireEvent($name, $value = [])
protected function fireEvent($name, $type = null, $data = null)
{
return $this->events->dispatch('LaravelCart.' . $name, array_values([$value, $this]), true);
$eventData = [
'session_key' => $this->session->sessionKey,
'session_driver' => $this->session->driver,
'session_model' => $this->session->getSessionModel(),
];
if ($type) {
$eventData[$type] = $data;
}
return $this->events->dispatch('LaravelCart.' . $name, array_values([$eventData, $this]), true);
}
/**