Shopware 6: Checkout and Context

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:

  • Payment Method
  • Shipping Method
  • Customer, including
    • Billing Address and
    • Shipping Address

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:

  • There is no order or anything to get all the data. You can change the order of the address.
  • You can remove the shipping method selection from the checkout
  • Or remove the payment method selection
  • And on the other hand can we write any data to the context and it is used for the checkout and the order

3 thoughts on “Shopware 6: Checkout and Context

  1. Do not forget to add a name attribute to the input tag, since the value will be stored in the browser’s local storage under the variable confirmOrderForm.NAME. If you add several input tags with form=”confirmOrderForm” but without name attributes, only the first value will be stored under the variable confirmOrderForm.

  2. When I think more about the browser’s local storage using your handy side note can be a risky security issue since the local storage does not get deleted when a customer logs out. Thus on a public computer the next customer might see sensible data from the customer before.

Leave a Reply

Discover more from Winkelwagen

Subscribe now to keep reading and get access to the full archive.

Continue reading