Skip to content

Config step conditions

Step conditions can be used in hyva_checkout.xml in two ways:

  • In a <condition if="..."> attribute
  • In a <update if="..."> attribute

The value of the if attribute is either a step condition identifier or a condition class name.
If multiple <condition if="..."> nodes exist on a step, the step will only be shown if all conditions are true.

<step name="example-step">
    <!-- Only include the example_handle.xml layout XML if the "is_physical" condition is true -->
    <update handle="example_handle" if="is_physical"/>

    <!-- Only show the "example-step" step if the "is_customer" condition is true -->
    <condition name="guest_login" if="is_customer"/>
</step>

Some common conditions are available out of the box:

  • is_always_allow
  • is_customer
  • is_device
  • is_guest
  • is_physical
  • is_virtual

Step condition identifiers

The condition identifiers exist for readability and are mapped to class names via etc/frontend/di.xml.
The default conditions are declared in vendor/hyva-themes/magento2-hyva-checkout/src/etc/frontend/di.xml.

<type name="Hyva\Checkout\Model\CustomConditionFactory">
    <arguments>
        <argument name="customConditionTypes" xsi:type="array">
            <item name="is_always_allow" xsi:type="string">Hyva\Checkout\Model\CustomCondition\IsAlwaysAllow</item>
            <item name="is_customer" xsi:type="string">Hyva\Checkout\Model\CustomCondition\IsCustomer</item>
            <item name="is_guest" xsi:type="string">Hyva\Checkout\Model\CustomCondition\IsGuest</item>
            <item name="is_physical" xsi:type="string">Hyva\Checkout\Model\CustomCondition\IsPhysical</item>
            <item name="is_virtual" xsi:type="string">Hyva\Checkout\Model\CustomCondition\IsVirtual</item>
            <item name="is_device" xsi:type="string">Hyva\Checkout\Model\CustomCondition\IsDevice</item>
        </argument>
    </arguments>
</type>

Custom conditions can be added to the array using di.xml, but unless a condition will be reused a lot, it is better to use the class name directly as the value of the if attribute:

<step name="example-step">
    <update handle="example_handle" if="My\Module\Model\Checkout\StepCondition\ExampleStepCondition"/>
    <condition name="show_if_valid" if="My\Module\Model\Checkout\StepCondition\ExampleStepCondition"/>
</step>

Step condition classes

Condition classes have to implement the interface Hyva\Checkout\Model\CustomConditionInterface.
The interface declares only a single method: public function validate(): bool

In the case of a <condition ...> node an accompanying attribute method can be specified to use a non-default validation method name.
This can be convenient to group variations of a condition within a single class to avoid code duplication.

<condition name="show_login" if="is_customer" method="required"/>

If no method is specified, the default method validate will be used.