Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multicart for not logged users #50

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"symfony/debug-bundle": "^5.4 || ^6.0",
"symfony/dotenv": "^5.4 || ^6.0",
"symfony/intl": "^5.4 || ^6.0",
"symfony/uid": "^6.0",
"symfony/web-profiler-bundle": "^5.4 || ^6.0",
"symfony/webpack-encore-bundle": "^1.16",
"vimeo/psalm": "4.27",
Expand Down
231 changes: 200 additions & 31 deletions doc/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ Import required config in your `config/packages/_sylius.yaml` file:
imports:
...
- { resource: "@BitBagSyliusMultiCartPlugin/Resources/config/config.yml" }

parameters:
...
allow_multicart_for_anonymous: true
```

Add routing to your `config/routes.yaml` file:
Expand Down Expand Up @@ -82,23 +86,40 @@ declare(strict_types=1);

namespace App\Repository;

use BitBag\SyliusMultiCartPlugin\Entity\CustomerInterface;
use BitBag\SyliusMultiCartPlugin\Entity\OrderInterface;
use BitBag\SyliusMultiCartPlugin\Repository\OrderRepositoryInterface;
use Sylius\Bundle\CoreBundle\Doctrine\ORM\OrderRepository as BaseOrderRepository;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\CustomerInterface;

class OrderRepository extends BaseOrderRepository implements OrderRepositoryInterface
{
public function findCarts(ChannelInterface $channel, ?CustomerInterface $customer): array
{
return $this->createQueryBuilder('o')
public function findCarts(
ChannelInterface $channel,
?CustomerInterface $customer,
?string $machineId,
): array {
$queryBuilder = $this->createQueryBuilder('o')
->andWhere('o.state = :state')
->andWhere('o.channel = :channel')
->andWhere('o.customer = :customer')
->setParameter('state', OrderInterface::STATE_CART)
->setParameter('channel', $channel)
->setParameter('customer', $customer)
;

if (null !== $customer) {
$queryBuilder
->andWhere('o.customer = :customer')
->setParameter('customer', $customer)
;
}

if (null === $customer && null !== $machineId) {
$queryBuilder->andWhere('o.machineId = :machineId')
->setParameter('machineId', $machineId)
;
}

return $queryBuilder
->addOrderBy('o.cartNumber', 'DESC')
->getQuery()
->getResult()
Expand All @@ -107,18 +128,34 @@ class OrderRepository extends BaseOrderRepository implements OrderRepositoryInte

public function findCartsGraterOrEqualNumber(
ChannelInterface $channel,
CustomerInterface $customer,
?CustomerInterface $customer,
int $cartNumber,
?string $machineId,
): array {
return $this->createQueryBuilder('o')
$queryBuilder = $this->createQueryBuilder('o')
->andWhere('o.state = :state')
->andWhere('o.channel = :channel')
->andWhere('o.customer = :customer')
->andWhere('o.cartNumber >= :cartNumber')
->setParameter('state', OrderInterface::STATE_CART)
->setParameter('channel', $channel)
->setParameter('customer', $customer)
->setParameter('cartNumber', $cartNumber)
;

if (null !== $customer) {
$queryBuilder
->andWhere('o.customer = :customer')
->setParameter('customer', $customer)
;
}

if (null === $customer && null !== $machineId) {
$queryBuilder
->andWhere('o.machineId = :machineId')
->setParameter('machineId', $machineId)
;
}

return $queryBuilder
->addOrderBy('o.cartNumber', 'ASC')
->getQuery()
->getResult()
Expand All @@ -127,32 +164,66 @@ class OrderRepository extends BaseOrderRepository implements OrderRepositoryInte

public function findBiggestCartNumber(
ChannelInterface $channel,
CustomerInterface $customer,
?CustomerInterface $customer,
?string $machineId,
): int {
return (int) $this->createQueryBuilder('o')
$queryBuilder = $this->createQueryBuilder('o')
->select('MAX(o.cartNumber)')
->andWhere('o.state = :state')
->andWhere('o.channel = :channel')
->andWhere('o.customer = :customer')
->setParameter('state', OrderInterface::STATE_CART)
->setParameter('channel', $channel)
->setParameter('customer', $customer)
;

if (null !== $customer) {
$queryBuilder
->andWhere('o.customer = :customer')
->setParameter('customer', $customer)
;
}

if (null === $customer && null !== $machineId) {
$queryBuilder
->andWhere('o.machineId = :machineId')
->setParameter('machineId', $machineId)
;
}

return (int) $queryBuilder
->addOrderBy('o.createdAt', 'DESC')
->getQuery()
->getSingleScalarResult()
;
}

public function countCarts(ChannelInterface $channel, ?CustomerInterface $customer): int
{
return (int) $this->createQueryBuilder('o')
public function countCarts(
ChannelInterface $channel,
?CustomerInterface $customer,
?string $machineId,
): int {
$queryBuilder = $this->createQueryBuilder('o')
->select('COUNT(o.id)')
->andWhere('o.state = :state')
->andWhere('o.channel = :channel')
->andWhere('o.customer = :customer')
->setParameter('state', OrderInterface::STATE_CART)
->setParameter('channel', $channel)
->setParameter('customer', $customer)
;

if (null !== $customer) {
$queryBuilder
->andWhere('o.customer = :customer')
->setParameter('customer', $customer)
;
}

if (null === $customer && null !== $machineId) {
$queryBuilder
->andWhere('o.machineId = :machineId')
->setParameter('machineId', $machineId)
;
}

return (int) $queryBuilder
->addOrderBy('o.createdAt', 'DESC')
->getQuery()
->getSingleScalarResult()
Expand All @@ -161,23 +232,110 @@ class OrderRepository extends BaseOrderRepository implements OrderRepositoryInte

public function findLatestNotEmptyActiveCart(
ChannelInterface $channel,
CustomerInterface $customer,
?CustomerInterface $customer,
?string $machineId,
): ?OrderInterface {
return $this->createQueryBuilder('o')
$queryBuilder = $this->createQueryBuilder('o')
->andWhere('o.state = :state')
->andWhere('o.channel = :channel')
->andWhere('o.customer = :customer')
->andWhere('o.cartNumber = :activeCart')
->andWhere('o.isActive = :isActive')
->setParameter('state', OrderInterface::STATE_CART)
->setParameter('channel', $channel)
->setParameter('customer', $customer)
->setParameter('activeCart', $customer->getActiveCart())
->setParameter('isActive', true)
;

if (null !== $customer) {
$queryBuilder
->andWhere('o.customer = :customer')
->setParameter('customer', $customer)
;
}

if (null === $customer && null !== $machineId) {
$queryBuilder
->andWhere('o.machineId = :machineId')
->setParameter('machineId', $machineId)
;
}

return $queryBuilder
->addOrderBy('o.createdAt', 'DESC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}

public function findActiveCart(
ChannelInterface $channel,
?CustomerInterface $customer,
?string $machineId,
): ?OrderInterface {
$queryBuilder = $this->createQueryBuilder('o')
->andWhere('o.state = :state')
->andWhere('o.channel = :channel')
->andWhere('o.isActive = :isActive')
->setParameter('state', OrderInterface::STATE_CART)
->setParameter('channel', $channel)
->setParameter('isActive', true)
;

if (null !== $customer) {
$queryBuilder
->andWhere('o.customer = :customer')
->setParameter('customer', $customer)
;
}

if (null === $customer && null !== $machineId) {
$queryBuilder->andWhere('o.machineId = :machineId')
->setParameter('machineId', $machineId)
;
}

return $queryBuilder
->addOrderBy('o.cartNumber', 'DESC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}

public function findPickedCart(
ChannelInterface $channel,
?CustomerInterface $customer,
?string $machineId,
int $cartNumber,
): ?OrderInterface {
$queryBuilder = $this->createQueryBuilder('o')
->andWhere('o.state = :state')
->andWhere('o.channel = :channel')
->andWhere('o.cartNumber = :cartNumber')
->setParameter('state', OrderInterface::STATE_CART)
->setParameter('channel', $channel)
->setParameter('cartNumber', $cartNumber)
;

if (null !== $customer) {
$queryBuilder
->andWhere('o.customer = :customer')
->setParameter('customer', $customer)
;
}

if (null === $customer && null !== $machineId) {
$queryBuilder->andWhere('o.machineId = :machineId')
->setParameter('machineId', $machineId)
;
}

return $queryBuilder
->addOrderBy('o.cartNumber', 'DESC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
}
```

Expand All @@ -186,11 +344,22 @@ Override `config/packages/_sylius.yaml` configuration:
# config/packages/_sylius.yaml

sylius_order:
resources:
order:
classes:
...
repository: App\Repository\OrderRepository
resources:
order:
classes:
...
interface: BitBag\SyliusMultiCartPlugin\Entity\OrderInterface
repository: App\Repository\OrderRepository
```

Override `config/packages/twig.yaml` configuration:
```yaml
# config/packages/twig.yaml

twig:
...
globals:
allow_multicart_for_anonymous: '%allow_multicart_for_anonymous%'
```

### Update your database
Expand Down
23 changes: 0 additions & 23 deletions doc/installation/attribute-mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,6 @@ doctrine:

Extend entities with parameters and methods using attributes and traits:

- `Customer` entity:

```php
<?php
// src/Entity/Customer/Customer.php

declare(strict_types=1);

namespace App\Entity\Customer;

use BitBag\SyliusMultiCartPlugin\Entity\CustomerInterface;
use BitBag\SyliusMultiCartPlugin\Model\CustomerTrait;
use Sylius\Component\Core\Model\Customer as BaseCustomer;

class Customer extends BaseCustomer implements CustomerInterface
{
use CustomerTrait;

#[ORM\Column(name: "active_cart", type: "integer", nullable: true)]
protected ?int $activeCart = 1;
}
```

- `Order` entity:

```php
Expand Down
Loading