cart cart for user by id and carts index
This commit is contained in:
@@ -8,9 +8,11 @@ use Cake\Core\Configure;
|
||||
use Cake\Datasource\EntityInterface;
|
||||
use Cake\Datasource\Exception\RecordNotFoundException;
|
||||
use Cake\Event\EventInterface;
|
||||
use Cake\ORM\Table;
|
||||
use Cake\ORM\TableRegistry;
|
||||
use CakeCarts\Model\Entity\CartItem;
|
||||
use CakeCarts\Model\Enum\CartTypeId;
|
||||
use CakeCarts\Model\Table\CartsTable;
|
||||
|
||||
/**
|
||||
* ShoppingCart component
|
||||
@@ -22,11 +24,18 @@ class ShoppingCartComponent extends Component
|
||||
*/
|
||||
protected string $userIdField;
|
||||
|
||||
/**
|
||||
* @var CartsTable|Table $Carts
|
||||
*/
|
||||
protected CartsTable|Table $Carts;
|
||||
|
||||
|
||||
public function initialize(array $config): void
|
||||
{
|
||||
parent::initialize($config); // TODO: Change the autogenerated stub
|
||||
|
||||
$this->userIdField = Configure::readOrFail('CakeCarts.Users.user_id') === 'uuid' ? 'user_id_uuid' : 'user_id';
|
||||
$this->Carts = TableRegistry::getTableLocator()->get(Configure::readOrFail('CakeCarts.Carts.table'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,29 +57,50 @@ class ShoppingCartComponent extends Component
|
||||
$this->getController()->set(compact('cart'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $cartId
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCartForUserById(string $cartId)
|
||||
{
|
||||
$identity = $this->getController()->getRequest()->getAttribute('identity');
|
||||
$sessionId = $this->getSessionId();
|
||||
|
||||
$cartsQ = $this->Carts
|
||||
->find()
|
||||
->contain(['CartItems'])
|
||||
->where(['Carts.id' => $cartId]);
|
||||
if ($identity) {
|
||||
$cartsQ->where([$this->userIdField => $identity->getIdentifier()]);
|
||||
} else {
|
||||
$cartsQ->where(['session_id' => $sessionId]);
|
||||
}
|
||||
|
||||
return $cartsQ->firstOrFail();
|
||||
}
|
||||
|
||||
public function findExistingCartOrCreate(string $sessionId, int $cartTypeId = null)
|
||||
{
|
||||
$cartsTable = TableRegistry::getTableLocator()->get(Configure::readOrFail('CakeCarts.Carts.table'));
|
||||
$userIdField = Configure::readOrFail('CakeCarts.Users.user_id') === 'integer' ? 'user_id' : 'user_id_uuid';
|
||||
$identity = $this->getController()->getRequest()->getAttribute('identity');
|
||||
|
||||
$cartTypeId = $cartTypeId ?? CartTypeId::Cart->value;
|
||||
|
||||
$cart = $cartsTable
|
||||
$cart = $this->Carts
|
||||
->findBySessionId($sessionId)
|
||||
->contain(['CartItems'])
|
||||
->where(['cart_type_id' => $cartTypeId])
|
||||
->first();
|
||||
|
||||
if (isset($cart) && isset($identity) && !isset($cart[$this->userIdField])) {
|
||||
$cart = $cartsTable->patchEntity([
|
||||
$cart = $this->Carts->patchEntity([
|
||||
$this->userIdField => $identity->getIdentifier(),
|
||||
]);
|
||||
|
||||
$cart = $cartsTable->saveOrFail($cart);
|
||||
$cart = $this->Carts->saveOrFail($cart);
|
||||
}
|
||||
if (!isset($cart)) {
|
||||
$cart = $cartsTable->newEntity([
|
||||
$cart = $this->Carts->newEntity([
|
||||
'cart_type_id' => $cartTypeId,
|
||||
'session_id' => $sessionId,
|
||||
$this->userIdField => isset($identity) ? $identity->getIdentifier() : null,
|
||||
@@ -78,7 +108,7 @@ class ShoppingCartComponent extends Component
|
||||
'cart_items' => [],
|
||||
]);
|
||||
|
||||
$cart = $cartsTable->saveOrFail($cart);
|
||||
$cart = $this->Carts->saveOrFail($cart);
|
||||
}
|
||||
|
||||
return $cart;
|
||||
|
||||
Reference in New Issue
Block a user