Filtering Checkout Fields in Saber Commerce

Developers can easily filter the checkout fields using the sacom_checkout_fields filter. It has 1 argument, the array of $fields.

Each field is an instance of the CheckoutField class, with properties that define the field including:

  • $id
  • $label
  • $placeholder

As with most filters in WordPress you need to return the $fields array after injecting your additional field(s) into it.

Here is an example which uses an anonymous function as the filter callback.

add_filter( 'sacom_checkout_fields', function( $fields ) {

        $m = new CheckoutField();
        $m->id = 'test123';
        $m->label = 'Test 123';
        $fields[] = $m;

        return $fields;

});

You can also add the filter using a named callback using this procedural example below. This approach might be used in functions.php in a theme for instance:

add_filter( 'sacom_checkout_fields', 'themeCheckoutFields' );

function themeCheckoutFields( $fields ) {

  // Add fields to array here.

}

Finally if you’re adding fields in an object-oriented environment (a PHP class) then you may want to use a PHP callable as shown below:

add_filter( 'sacom_checkout_fields', array( $this, 'addCheckoutFields' ) );

public function addCheckoutFields( $fields ) {

  // Add checkout fields here.

}

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *