mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-11 18:54:33 +00:00
rename the storedCartWithIdentifier method to storedCartInstanceWithIdentifierExists, changed the signature of the merge and storedCartInstanceWithIdentifierExists methods and add instance to all stored cart retrievals
This commit is contained in:
37
src/Cart.php
37
src/Cart.php
@@ -633,13 +633,15 @@ class Cart
|
|||||||
$identifier = $identifier->getInstanceIdentifier();
|
$identifier = $identifier->getInstanceIdentifier();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->storedCartWithIdentifierExists($identifier)) {
|
$instance = $this->currentInstance();
|
||||||
|
|
||||||
|
if ($this->storedCartInstanceWithIdentifierExists($instance, $identifier)) {
|
||||||
throw new CartAlreadyStoredException("A cart with identifier {$identifier} was already stored.");
|
throw new CartAlreadyStoredException("A cart with identifier {$identifier} was already stored.");
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->getConnection()->table($this->getTableName())->insert([
|
$this->getConnection()->table($this->getTableName())->insert([
|
||||||
'identifier' => $identifier,
|
'identifier' => $identifier,
|
||||||
'instance' => $this->currentInstance(),
|
'instance' => $instance,
|
||||||
'content' => serialize($content),
|
'content' => serialize($content),
|
||||||
'created_at' => $this->createdAt ?: Carbon::now(),
|
'created_at' => $this->createdAt ?: Carbon::now(),
|
||||||
'updated_at' => Carbon::now(),
|
'updated_at' => Carbon::now(),
|
||||||
@@ -661,17 +663,17 @@ class Cart
|
|||||||
$identifier = $identifier->getInstanceIdentifier();
|
$identifier = $identifier->getInstanceIdentifier();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->storedCartWithIdentifierExists($identifier)) {
|
$currentInstance = $this->currentInstance();
|
||||||
|
|
||||||
|
if (!$this->storedCartInstanceWithIdentifierExists($currentInstance, $identifier)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$stored = $this->getConnection()->table($this->getTableName())
|
$stored = $this->getConnection()->table($this->getTableName())
|
||||||
->where('identifier', $identifier)->first();
|
->where(['identifier'=> $identifier,'instance' => $currentInstance])->first();
|
||||||
|
|
||||||
$storedContent = unserialize(data_get($stored, 'content'));
|
$storedContent = unserialize(data_get($stored, 'content'));
|
||||||
|
|
||||||
$currentInstance = $this->currentInstance();
|
|
||||||
|
|
||||||
$this->instance(data_get($stored, 'instance'));
|
$this->instance(data_get($stored, 'instance'));
|
||||||
|
|
||||||
$content = $this->getContent();
|
$content = $this->getContent();
|
||||||
@@ -689,7 +691,7 @@ class Cart
|
|||||||
$this->createdAt = Carbon::parse(data_get($stored, 'created_at'));
|
$this->createdAt = Carbon::parse(data_get($stored, 'created_at'));
|
||||||
$this->updatedAt = Carbon::parse(data_get($stored, 'updated_at'));
|
$this->updatedAt = Carbon::parse(data_get($stored, 'updated_at'));
|
||||||
|
|
||||||
$this->getConnection()->table($this->getTableName())->where('identifier', $identifier)->delete();
|
$this->getConnection()->table($this->getTableName())->where(['identifier' => $identifier, 'instance' => $currentInstance])->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -705,11 +707,13 @@ class Cart
|
|||||||
$identifier = $identifier->getInstanceIdentifier();
|
$identifier = $identifier->getInstanceIdentifier();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->storedCartWithIdentifierExists($identifier)) {
|
$instance = $this->currentInstance();
|
||||||
|
|
||||||
|
if (!$this->storedCartInstanceWithIdentifierExists($instance, $identifier)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->getConnection()->table($this->getTableName())->where('identifier', $identifier)->delete();
|
$this->getConnection()->table($this->getTableName())->where(['identifier' => $identifier, 'instance' => $instance])->delete();
|
||||||
|
|
||||||
$this->events->dispatch('cart.erased');
|
$this->events->dispatch('cart.erased');
|
||||||
}
|
}
|
||||||
@@ -724,14 +728,14 @@ class Cart
|
|||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function merge($identifier, $keepDiscount = false, $keepTax = false, $dispatchAdd = true)
|
public function merge($identifier, $keepDiscount = false, $keepTax = false, $dispatchAdd = true, $instance = SELF::DEFAULT_INSTANCE)
|
||||||
{
|
{
|
||||||
if (!$this->storedCartWithIdentifierExists($identifier)) {
|
if (!$this->storedCartInstanceWithIdentifierExists($instance, $identifier)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$stored = $this->getConnection()->table($this->getTableName())
|
$stored = $this->getConnection()->table($this->getTableName())
|
||||||
->where('identifier', $identifier)->first();
|
->where(['identifier'=> $identifier,'instance'=>$instance])->first();
|
||||||
|
|
||||||
$storedContent = unserialize($stored->content);
|
$storedContent = unserialize($stored->content);
|
||||||
|
|
||||||
@@ -829,14 +833,9 @@ class Cart
|
|||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function storedCartWithIdentifierExists($identifier)
|
private function storedCartInstanceWithIdentifierExists($instance, $identifier)
|
||||||
{
|
{
|
||||||
$data = ['identifier' => $identifier];
|
return $this->getConnection()->table($this->getTableName())->where(['identifier' => $identifier, 'instance'=> $instance])->exists();
|
||||||
if ($this->countInstances() > 1) {
|
|
||||||
$data['instance'] = $this->currentInstance();
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->getConnection()->table($this->getTableName())->where($data)->exists();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -865,11 +865,11 @@ class CartTest extends TestCase
|
|||||||
|
|
||||||
$cart->store($identifier = 123);
|
$cart->store($identifier = 123);
|
||||||
|
|
||||||
|
Event::assertDispatched('cart.stored');
|
||||||
|
|
||||||
$serialized = serialize($cart->content());
|
$serialized = serialize($cart->content());
|
||||||
|
|
||||||
$this->assertDatabaseHas('shoppingcart', ['identifier' => $identifier, 'instance' => 'default', 'content' => $serialized]);
|
$this->assertDatabaseHas('shoppingcart', ['identifier' => $identifier, 'instance' => 'default', 'content' => $serialized]);
|
||||||
|
|
||||||
Event::assertDispatched('cart.stored');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
@@ -905,6 +905,8 @@ class CartTest extends TestCase
|
|||||||
|
|
||||||
$cart->store($identifier);
|
$cart->store($identifier);
|
||||||
|
|
||||||
|
Event::assertDispatched('cart.stored');
|
||||||
|
|
||||||
sleep(1);
|
sleep(1);
|
||||||
$afterSecondStore = Carbon::now();
|
$afterSecondStore = Carbon::now();
|
||||||
|
|
||||||
@@ -912,8 +914,6 @@ class CartTest extends TestCase
|
|||||||
|
|
||||||
$this->assertTrue($beforeStore->lessThanOrEqualTo($cart->createdAt()) && $afterStore->greaterThanOrEqualTo($cart->createdAt()));
|
$this->assertTrue($beforeStore->lessThanOrEqualTo($cart->createdAt()) && $afterStore->greaterThanOrEqualTo($cart->createdAt()));
|
||||||
$this->assertTrue($beforeSecondStore->lessThanOrEqualTo($cart->updatedAt()) && $afterSecondStore->greaterThanOrEqualTo($cart->updatedAt()));
|
$this->assertTrue($beforeSecondStore->lessThanOrEqualTo($cart->updatedAt()) && $afterSecondStore->greaterThanOrEqualTo($cart->updatedAt()));
|
||||||
|
|
||||||
Event::assertDispatched('cart.stored');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -936,9 +936,9 @@ class CartTest extends TestCase
|
|||||||
|
|
||||||
$cart->store($identifier = 123);
|
$cart->store($identifier = 123);
|
||||||
|
|
||||||
$cart->store($identifier);
|
|
||||||
|
|
||||||
Event::assertDispatched('cart.stored');
|
Event::assertDispatched('cart.stored');
|
||||||
|
|
||||||
|
$cart->store($identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
@@ -962,11 +962,11 @@ class CartTest extends TestCase
|
|||||||
|
|
||||||
$cart->restore($identifier);
|
$cart->restore($identifier);
|
||||||
|
|
||||||
|
Event::assertDispatched('cart.restored');
|
||||||
|
|
||||||
$this->assertItemsInCart(1, $cart);
|
$this->assertItemsInCart(1, $cart);
|
||||||
|
|
||||||
$this->assertDatabaseMissing('shoppingcart', ['identifier' => $identifier, 'instance' => 'default']);
|
$this->assertDatabaseMissing('shoppingcart', ['identifier' => $identifier, 'instance' => 'default']);
|
||||||
|
|
||||||
Event::assertDispatched('cart.restored');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
@@ -1469,18 +1469,21 @@ class CartTest extends TestCase
|
|||||||
|
|
||||||
$cart->store($identifier = 123);
|
$cart->store($identifier = 123);
|
||||||
|
|
||||||
|
Event::assertDispatched('cart.stored');
|
||||||
|
|
||||||
$serialized = serialize($cart->content());
|
$serialized = serialize($cart->content());
|
||||||
|
|
||||||
$newInstance = $this->getCart();
|
$newInstance = $this->getCart();
|
||||||
$newInstance->instance($instanceName = 'someinstance');
|
$newInstance->instance($instanceName = 'someinstance');
|
||||||
$newInstance->add(new BuyableProduct());
|
$newInstance->add(new BuyableProduct());
|
||||||
$newInstance->store($newIdentifier = 456);
|
$newInstance->store($identifier);
|
||||||
|
|
||||||
|
Event::assertDispatched('cart.stored');
|
||||||
|
|
||||||
$newInstanceSerialized = serialize($newInstance->content());
|
$newInstanceSerialized = serialize($newInstance->content());
|
||||||
|
|
||||||
$this->assertDatabaseHas('shoppingcart', ['identifier' => $identifier, 'instance' => 'default', 'content' => $serialized]);
|
$this->assertDatabaseHas('shoppingcart', ['identifier' => $identifier, 'instance' => 'default', 'content' => $serialized]);
|
||||||
|
|
||||||
$this->assertDatabaseHas('shoppingcart', ['identifier' => $newIdentifier, 'instance' => $instanceName, 'content' => $newInstanceSerialized]);
|
$this->assertDatabaseHas('shoppingcart', ['identifier' => $identifier, 'instance' => $instanceName, 'content' => $newInstanceSerialized]);
|
||||||
|
|
||||||
Event::assertDispatched('cart.stored');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user