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:
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-token – and 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
Other articles from this category
Shopware 6 Hidden Gems #16: CloneBehavior — entity duplication with overrides, one call
You know the admin’s „duplicate“ button on products. Now the ticket says: „We need that, but from code — duplicate a product per sales channel, with adjusted name and its own product number.“ Or: „Campaign landing pages: clone this CMS page 20 times with different headlines.“ I’ve watched this get implemented as: load entity with […]
Shopware 6 Hidden Gems #15: ChangeSet — before/after values for every write, no second query
The requirement sounds harmless every time: „Log when a product price changes, with old and new value.“ Or: „When an order’s email changes, notify the old address.“ Or the audit-trail classic: „Who changed what, from what, to what?“ And every time, the naive implementation is the same sad shape: subscribe to product.written, load the entity […]
Shopware 6 Hidden Gems #14: WriteProtected & ApiAware — field-level security without a single subscriber
A code review scenario. A plugin adds a purchasePrice-style field to a custom entity — internal margin data. The review question: „Can the Store API read this?“ Followed by: „Can the Admin API write that computed field you recalculate in a subscriber?“ The answers, in the plugin at hand, were „yes“ and „yes, and then […]