4. Using the services

When using the bundle, you have access to several handy services. You can use them to manipulate and manage the cart.

4.1. Managers and Repositories

Примечание

Sylius uses Doctrine\Common\Persistence interfaces.

You have access to following services which are used to manage and retrieve resources.

This set of default services is shared across almost all Sylius bundles, but this is just a convention. You’re interacting with them like you usually do with own entities in your project.

<?php

// ...
public function saveAction(Request $request)
{
    // ObjectManager which is capable of managing the Cart resource.
    // For *doctrine/orm* driver it will be EntityManager.
    $this->get('sylius.manager.cart');

    // ObjectRepository for the Cart resource, it extends the base EntityRepository.
    // You can use it like usual entity repository in project.
    $this->get('sylius.repository.cart');

    // Same pair for CartItem resource.
    $this->get('sylius.manager.cart_item');
    $this->get('sylius.repository.cart_item');

    // Those repositories have some handy default methods, for example...
    $item = $this->get('sylius.repository.cart')->createNew();
}

4.2. Provider and Resolver

There are also 3 more services for you.

You use the provider to obtain the current user cart, if there is none, a new one is created and saved. The ->setCart() method also allows you to replace the current cart. ->abandonCart() is resetting the current cart, a new one will be created on the next ->getCart() call. This is useful, for example, when after completing an order you want to start with a brand new and clean cart.

<?php

// ...
public function saveAction(Request $request)
{
    $provider = $this->get('sylius.cart_provider'); // Implements the CartProviderInterface.

    $currentCart = $provider->getCart();
    $provider->setCart($customCart);
    $provider->abandonCart();
}

The resolver is used to create a new item based on the user request.

<?php

// ...
public function addItemAction(Request $request)
{
    $resolver = $this->get('sylius.cart_resolver');
    $item = $resolver->resolve($this->createNew(), $request);
}

Примечание

A more advanced example of a resolver implementation is available in Sylius Sandbox application on GitHub.