03.12.2023・TechStuff
03.12.2023・TechStuff

Shopware 6: Checkout and Context

Fabian Blechschmidt

While surfing your awesome online store, you always has a context on yourself. The context contains a lot of interesting stuff:

class SalesChannelContext extends Struct
{
    use StateAwareTrait;

    /**
     * Unique token for context, e.g. stored in session or provided in request headers
     *
     * @var string
     */
    protected $token;

    /**
     * @var CustomerGroupEntity
     */
    protected $currentCustomerGroup;

    /**
     * @var CurrencyEntity
     */
    protected $currency;

    /**
     * @var SalesChannelEntity
     */
    protected $salesChannel;

    /**
     * @var TaxCollection
     */
    protected $taxRules;

    /**
     * @var CustomerEntity|null
     */
    protected $customer;

    /**
     * @var PaymentMethodEntity
     */
    protected $paymentMethod;

    /**
     * @var ShippingMethodEntity
     */
    protected $shippingMethod;

    /**
     * @var ShippingLocation
     */
    protected $shippingLocation;

    /**
     * @var array<string, bool>
     */
    protected $permissions = [];

    /**
     * @var bool
     */
    protected $permisionsLocked = false;

    /**
     * @var Context
     */
    protected $context;
}

For me the important stuff and mind blowing is, that the context contains:

With all the informations you can create easily an order.

For example the default checkout has on the review step (the step BEFORE the „thanks for your order“) a simple form with a button – in german shops the only thing which is send to the server is „terms and conditions is checked“. Everything else is already known to the server.

Side note, if you want to add informations to the last step of the order creation you only need to add an <input> field with form="confirmOrderForm"

In one project, I was forced to create a cart via API, because payment provider – and I expected a horror, but thankfully that was easy as well, because the server already knows everything, so we only need to send a „please make this cart an order“ and all is fine.

Side note here as well – you need a request and add to the header: sw-access-key and sw-context-tokenand then Shopware finds your cart and places an order with it <3

Consequence: