sales_channel_types

Allows adding custom sales channel types.

Shop-Script

Input (passed by reference)

$params['locality'] string Locale’s geographical region for which the list of sales channel types is being requested.
… your plugin code …

Output

{$sales_channel_types.$output}
List of sales channel types supported by the plugin. Each list item must be a subarray with the following keys:
[]['id'] string Sales channel type identifier; e.g., a short name of an online messenger or a social media.
[]['name'] string Localized type name.
[]['class'] string The name of an existing sales channel type class extending shopSalesChannelType. In that class, method getFormFieldsConfig() must be overridden to return the list of fields which must be available to users setting up a new sales channel.
[]['menu_icon'] string HTML code of an icon displayed next to the type name.
[]['available'] bool Specify false if a type must be temporarily unavailable for use.
Dummy plugin example on GutHub for implementing a custom sales channel type ›

shopSalesChannelType class

Include a descendant of this class in your plugin and override in it some methods of the base class to manage the contents of the sales channel settings page — define the list of available settings fields or also modify the default HTML template.

public function getFormFieldsConfig ($values = [])

Overriding this method is obligatory. It must return an array of settings fields in the field_id => params format. Each array item must be a subarray, which will be passed as the $params parameter to the waHtmlControl::getControl() method, which will return the HTML code of a form control.

Parameters
  • $values array The values of fields returned by this method; available when the settings of an existing sales channel are being viewed.
Example
protected function getFormFieldsConfig($values = []): array
{
    return [
        'stock_id' => [
            'title'        => _wp('Stock'),
            'description'  => '',
            'control_type' => waHtmlControl::SELECT,
            'options'      => array_map(function ($stock) {
                return [
                    'value' => $stock['id'],
                    'title' => $stock['name']
                ];
            }, [
                '' => [
                    'id' => '',
                    'name' => _wp('select a stock')
                ]
            ] + shopHelper::getStocks()),
        ],
        '...' => [
            ...
        ],
        '...' => [
            ...
        ],
    ];
}
public function getFormHtml (array $channel)

Override this method only if you need to change the default contents of the settings page. The method returns custom HTML code of the sales channel settings page.

Parameters
  • $channel array Channel properties: its ID, name, and other previously saved settings.
Example
public function getFormHtml(array $channel): string
{
    $field_params = [
        'namespace' => 'data',
        'title_wrapper' => '%s',
        'description_wrapper' => '<br><span class="hint">%s</span>',
        'control_wrapper' => '<div class="name">%s</div><div class="value">%s %s</div>',
    ];

    $form_fields = [];

    foreach ($this->getBaseFieldsConfig() as $name => $config) {
        $form_fields['__' . $name] = $this->getControl($name, $channel[$name] ?? '', $field_params + $config);
    }

    $field_params['namespace'] = ['data', 'params'];
    $form_fields += $this->getFormFields($channel['params'], $field_params);

    $view = wa('shop')->getView();

    $view->assign([
        'channel' => $channel,
        'form_fields' => $form_fields,
    ]);

    // orignal template: wa-apps/shop/templates/actions/channels/generic_form.include.html
    return $view->fetch('file:plugins/myplugin/templates/includes/channel_params.include.html');
}
Shop-Script

Plugin code example

PHP

public function salesChannelTypes($params)
{
    if ($params['locality'] == 'de') {    
        return [
            [
                'id' => 'sales_de',
                'name' => _wp('German channel'),
                // existing class extending shopSalesChannelType
                'class' => 'shopMyPluginSalesChannelType',
                'menu_icon' => '<i class="fas fa-user"></i>',
            ]
        ];
    }
}