cart cart for user by id and carts index
CI / testsuite (mysql, 8.2, ) (push) Has been cancelled Details
CI / testsuite (mysql, 8.4, ) (push) Has been cancelled Details
CI / testsuite (sqlite, 8.2, prefer-lowest) (push) Has been cancelled Details
CI / Coding Standard & Static Analysis (push) Has been cancelled Details

This commit is contained in:
Brandon Shipley 2025-10-17 00:25:40 -07:00
parent d09cb19fd3
commit f4e03ec35f
Signed by: bmfs
GPG Key ID: 14E38571D8BB0DE4
2 changed files with 120 additions and 7 deletions

View File

@ -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;

83
templates/Carts/index.php Normal file
View File

@ -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>