cart cart for user by id and carts index
This commit is contained in:
parent
d09cb19fd3
commit
f4e03ec35f
|
|
@ -8,9 +8,11 @@ use Cake\Core\Configure;
|
||||||
use Cake\Datasource\EntityInterface;
|
use Cake\Datasource\EntityInterface;
|
||||||
use Cake\Datasource\Exception\RecordNotFoundException;
|
use Cake\Datasource\Exception\RecordNotFoundException;
|
||||||
use Cake\Event\EventInterface;
|
use Cake\Event\EventInterface;
|
||||||
|
use Cake\ORM\Table;
|
||||||
use Cake\ORM\TableRegistry;
|
use Cake\ORM\TableRegistry;
|
||||||
use CakeCarts\Model\Entity\CartItem;
|
use CakeCarts\Model\Entity\CartItem;
|
||||||
use CakeCarts\Model\Enum\CartTypeId;
|
use CakeCarts\Model\Enum\CartTypeId;
|
||||||
|
use CakeCarts\Model\Table\CartsTable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ShoppingCart component
|
* ShoppingCart component
|
||||||
|
|
@ -22,11 +24,18 @@ class ShoppingCartComponent extends Component
|
||||||
*/
|
*/
|
||||||
protected string $userIdField;
|
protected string $userIdField;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var CartsTable|Table $Carts
|
||||||
|
*/
|
||||||
|
protected CartsTable|Table $Carts;
|
||||||
|
|
||||||
|
|
||||||
public function initialize(array $config): void
|
public function initialize(array $config): void
|
||||||
{
|
{
|
||||||
parent::initialize($config); // TODO: Change the autogenerated stub
|
parent::initialize($config); // TODO: Change the autogenerated stub
|
||||||
|
|
||||||
$this->userIdField = Configure::readOrFail('CakeCarts.Users.user_id') === 'uuid' ? 'user_id_uuid' : 'user_id';
|
$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'));
|
$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)
|
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');
|
$identity = $this->getController()->getRequest()->getAttribute('identity');
|
||||||
|
|
||||||
$cartTypeId = $cartTypeId ?? CartTypeId::Cart->value;
|
$cartTypeId = $cartTypeId ?? CartTypeId::Cart->value;
|
||||||
|
|
||||||
$cart = $cartsTable
|
$cart = $this->Carts
|
||||||
->findBySessionId($sessionId)
|
->findBySessionId($sessionId)
|
||||||
->contain(['CartItems'])
|
->contain(['CartItems'])
|
||||||
->where(['cart_type_id' => $cartTypeId])
|
->where(['cart_type_id' => $cartTypeId])
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
if (isset($cart) && isset($identity) && !isset($cart[$this->userIdField])) {
|
if (isset($cart) && isset($identity) && !isset($cart[$this->userIdField])) {
|
||||||
$cart = $cartsTable->patchEntity([
|
$cart = $this->Carts->patchEntity([
|
||||||
$this->userIdField => $identity->getIdentifier(),
|
$this->userIdField => $identity->getIdentifier(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$cart = $cartsTable->saveOrFail($cart);
|
$cart = $this->Carts->saveOrFail($cart);
|
||||||
}
|
}
|
||||||
if (!isset($cart)) {
|
if (!isset($cart)) {
|
||||||
$cart = $cartsTable->newEntity([
|
$cart = $this->Carts->newEntity([
|
||||||
'cart_type_id' => $cartTypeId,
|
'cart_type_id' => $cartTypeId,
|
||||||
'session_id' => $sessionId,
|
'session_id' => $sessionId,
|
||||||
$this->userIdField => isset($identity) ? $identity->getIdentifier() : null,
|
$this->userIdField => isset($identity) ? $identity->getIdentifier() : null,
|
||||||
|
|
@ -78,7 +108,7 @@ class ShoppingCartComponent extends Component
|
||||||
'cart_items' => [],
|
'cart_items' => [],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$cart = $cartsTable->saveOrFail($cart);
|
$cart = $this->Carts->saveOrFail($cart);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $cart;
|
return $cart;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,83 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* @var \App\View\AppView $this
|
||||||
|
* @var \CakeCarts\Model\Entity\Cart $cart
|
||||||
|
*/
|
||||||
|
?>
|
||||||
|
<div class="carts index content">
|
||||||
|
<h3><?= __('My Cart') ?></h3>
|
||||||
|
<div class="table-responsive">
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th><?= '#' ?></th>
|
||||||
|
<th><?= 'Foreign Key' ?></th>
|
||||||
|
<th><?= 'Model' ?></th>
|
||||||
|
<th><?= 'Qty' ?></th>
|
||||||
|
<th><?= 'Price' ?></th>
|
||||||
|
<th><?= 'Subtotal' ?></th>
|
||||||
|
<th class="actions"><?= __('Actions') ?></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<?php
|
||||||
|
$cnt = 1;
|
||||||
|
foreach ($cart->cart_items as $cartItem): ?>
|
||||||
|
<?php /* @var \CakeCarts\Model\Entity\CartItem $cartItem */ ?>
|
||||||
|
<tr>
|
||||||
|
<td><?= $cartItem->position === null ? $cnt : $this->Number->format($cartItem->position) ?></td>
|
||||||
|
<td><?= $cartItem->foreign_key_uuid ?? $cartItem->foreign_key ?></td>
|
||||||
|
<td><?= $cartItem->model; ?></td>
|
||||||
|
<td>
|
||||||
|
<?= $this->Form->control('qty', [
|
||||||
|
'label' => false,
|
||||||
|
'min' => 1,
|
||||||
|
'step' => 1,
|
||||||
|
'default' => $cartItem->qty,
|
||||||
|
'value' => $cartItem->qty,
|
||||||
|
'style' => 'width: 120px;',
|
||||||
|
'type' => 'number',
|
||||||
|
'data-cart-item-id' => $cartItem->id,
|
||||||
|
'hx-put' => $this->Url->build([
|
||||||
|
'controller' => 'CartItems',
|
||||||
|
'action' => 'edit',
|
||||||
|
$cartItem->id,
|
||||||
|
]),
|
||||||
|
'hx-swap' => 'none',
|
||||||
|
'hx-include' => '[name="_csrfToken"], [data-cart-item-id="' . $cartItem->id . '"]',
|
||||||
|
]) ?>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<?= $this->Form->control('price', [
|
||||||
|
'label' => false,
|
||||||
|
'min' => 0,
|
||||||
|
'default' => $cartItem->price,
|
||||||
|
'value' => $cartItem->price,
|
||||||
|
'style' => 'width: 120px;',
|
||||||
|
'type' => 'text',
|
||||||
|
'data-cart-item-id' => $cartItem->id,
|
||||||
|
'hx-put' => $this->Url->build([
|
||||||
|
'controller' => 'CartItems',
|
||||||
|
'action' => 'edit',
|
||||||
|
$cartItem->id,
|
||||||
|
]),
|
||||||
|
'hx-swap' => 'none',
|
||||||
|
'hx-include' => '[name="_csrfToken"], [data-cart-item-id="' . $cartItem->id . '"]',
|
||||||
|
]) ?>
|
||||||
|
</td>
|
||||||
|
<td><?= $cartItem->subtotal === null ? '' : $this->Number->currency($cartItem->subtotal, 'USD') ?></td>
|
||||||
|
<td class="actions">
|
||||||
|
<?= $this->Form->postLink(__('Delete'), [
|
||||||
|
'controller' => 'CartItems',
|
||||||
|
'action' => 'delete',
|
||||||
|
$cartItem->id
|
||||||
|
], ['confirm' => __('Are you sure you want to delete # {0}?', $cartItem->id)]) ?>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<?php
|
||||||
|
$cnt++;
|
||||||
|
endforeach; ?>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
Loading…
Reference in New Issue