### Define Custom Sales Channel Setup Fields Source: https://context7.com/webasyst/shop-mysaleschannel/llms.txt Implement `getFormFieldsConfig` in the sales channel type class to define custom fields for the channel's setup interface. This example configures a 'Stock' selection field. ```php [ 'title' => _w('Stock'), 'description' => '', 'control_type' => waHtmlControl::SELECT, 'options' => array_map( function ($stock) { return [ 'value' => $stock['id'], 'title' => $stock['name'] ]; }, [ '' => [ 'id' => '', 'name' => _wp('Select stock') ] ] + shopHelper::getStocks() ), ], ]; } } ``` -------------------------------- ### Generate Custom Form HTML in PHP Source: https://context7.com/webasyst/shop-mysaleschannel/llms.txt Constructs the backend setup interface by configuring form fields and rendering a Smarty template. Uses the plugin's base fields and custom parameters to build the form structure. ```php 'data', 'title_wrapper' => '%s', 'description_wrapper' => '
%s', 'control_wrapper' => '
%s
%s %s
', ]; $form_fields = []; // Add base fields (name, description, etc.) foreach ($this->getBaseFieldsConfig() as $name => $field) { $form_fields['__' . $name] = $this->getControl( $name, $channel[$name] ?? '', $field_params + $field ); } // Add custom fields with nested namespace $field_params['namespace'] = ['data', 'params']; $form_fields += $this->getFormFields($channel, $field_params); // Render template with assigned variables $view = wa('shop')->getView(); $view->assign([ 'channel' => $channel, 'form_fields' => $form_fields, ]); return $view->fetch( 'file:plugins/mysaleschannel/templates/includes/channel_params.include.html' ); } ``` -------------------------------- ### Plugin Configuration File Source: https://context7.com/webasyst/shop-mysaleschannel/llms.txt Defines plugin metadata such as name, description, version, and event handlers. The `handlers` array maps Shop application events to specific plugin methods. ```php 'My sales channel', 'description' => 'Extra source of new orders.', 'img' => 'img/icon.png', 'version' => '1.0.0', 'vendor' => '--', 'handlers' => [ 'sales_channel_types' => 'salesChannelTypes', ] ]; ``` -------------------------------- ### Initialize Sales Channel Form with Event Handlers Source: https://github.com/webasyst/shop-mysaleschannel/blob/main/templates/includes/channel_params.include.html Initializes the sales channel form using jQuery and a custom SalesChannelForm class. It sets up event listeners for form submission, success, and failure, including disabling the submit button and showing a spinner. ```JavaScript $(function () { const $form = $('#js-sales-channel-form'); const $submit_button = $form.find(':submit').last(); const app_url = $form.data('app-url'); const delete_url = $form.data('app_url') + '?module=channels&action=delete'; const controller = new SalesChannelForm({ $place_for_errors: $form.find('.js-place-for-errors'), $form }); $form.on('save_request', function () { $submit_button.prop('disabled', true).after('
'); }); $form.on('save_success save_fail', function () { $submit_button.prop('disabled', false); $submit_button.siblings('.spinner').remove(); }); // ... rest of the code ``` -------------------------------- ### Handle New Sales Channel Creation Redirect Source: https://github.com/webasyst/shop-mysaleschannel/blob/main/templates/includes/channel_params.include.html When a new sales channel is successfully saved, this JavaScript code redirects the user to the editor page for the newly created channel. ```JavaScript if (empty($channel.id)) { $form.on('save_success', function (evt, data) { window.location = app_url + 'channels/editor/' + data.result.id; }); } ``` -------------------------------- ### Initialize JavaScript Form Controller Source: https://context7.com/webasyst/shop-mysaleschannel/llms.txt Initializes the form controller, handles form submission events, and manages loading states. Use this for frontend form interactions. ```javascript // Form initialization and event handling $(() => { const $form = $('#js-sales-channel-form'); const $submit_button = $form.find(':submit').last(); const app_url = $form.data('app-url'); const delete_url = $form.data('app_url') + '?module=channels&action=delete'; // Initialize form controller const controller = new SalesChannelForm({ $place_for_errors: $form.find('.js-place-for-errors'), $form }); // Show loading state during save $form.on('save_request', function () { $submit_button.prop('disabled', true).after('
'); }); // Remove loading state after save completes $form.on('save_success save_fail', function () { $submit_button.prop('disabled', false); $submit_button.siblings('.spinner').remove(); }); // Redirect to editor on new channel creation $form.on('save_success', function (evt, data) { window.location = app_url + 'channels/editor/' + data.result.id; }); // Delete confirmation dialog $delete_button.on('click', function (event) { event.preventDefault(); $.waDialog.confirm({ title: 'Delete this sales channel?', success_button_title: 'Delete', success_button_class: 'danger', cancel_button_title: 'Cancel', cancel_button_class: 'light-gray', onSuccess: async () => { await $.post(delete_url, { id: channel_id }); window.location = app_url + '?action=plugins#/'; } }); }); }); ``` -------------------------------- ### Render Frontend Channel Settings Form Source: https://context7.com/webasyst/shop-mysaleschannel/llms.txt Smarty template for the channel settings form, including hidden inputs for channel state and a loop to render dynamic form fields. Requires the channel-editor.js script for AJAX submission. ```html
{if !empty($channel.id)} {else} {/if} {foreach $form_fields as $name => $control_html}
{$control_html}
{/foreach}
{if !empty($channel.id)}
{/if}
``` -------------------------------- ### Register Custom Sales Channel Type Source: https://context7.com/webasyst/shop-mysaleschannel/llms.txt Implement the `salesChannelTypes` event handler to register a new custom sales channel. This method defines the channel's identifier, display name, icon, and availability. ```php [ 'class' => 'shopMysaleschannelPluginSalesChannelType', 'name' => _wp('My sales channel'), 'menu_icon' => '', 'available' => true, ] ]; } } ``` -------------------------------- ### Implement Sales Channel Deletion Confirmation Source: https://github.com/webasyst/shop-mysaleschannel/blob/main/templates/includes/channel_params.include.html For existing sales channels, this JavaScript code adds a click handler to the delete button. It displays a confirmation dialog before proceeding with the deletion via an AJAX POST request. ```JavaScript else { const channel_id = {$channel.id|json_encode}; const $delete_button = $form.find('.js-delete-button'); $delete_button.on('click', function (event) { event.preventDefault(); $.waDialog.confirm({ title: {"_wp('Delete this sales channel?')|json_encode}, success_button_title: {"_wp('Delete')|json_encode}, success_button_class: 'danger', cancel_button_title: {"_wp('Cancel')|json_encode}, cancel_button_class: 'light-gray', onSuccess: async () => { $delete_button .prop('disabled', true) .after('
'); await $.post(delete_url, { id: channel_id }); window.location = app_url + '?action=plugins#/'; } }); }); } ``` -------------------------------- ### Validate Channel Parameters in PHP Source: https://context7.com/webasyst/shop-mysaleschannel/llms.txt Validates channel settings before saving, ensuring required fields like stock_id are present and valid. Modifies the $params array by reference and returns an array of validation errors. ```php _w('This field is required'), 'field' => 'data[params][stock_id]', ]; } // Validate that selected stock exists if (isset($params['stock_id'])) { $stocks = shopHelper::getStocks(); if (!isset($stocks[$params['stock_id']])) { $errors['stock_id'] = [ 'error_description' => _w('This field is required'), 'field' => 'data[params][stock_id]', ]; } } return array_values($errors); } ``` -------------------------------- ### Iterate Over Form Fields in Webasyst Template Source: https://github.com/webasyst/shop-mysaleschannel/blob/main/templates/includes/channel_params.include.html This template code iterates through form fields to render them. It's used within the Webasyst framework for dynamic form generation. ```Smarty {if !empty($channel.id)} {else} {/if} {foreach $form_fields as $name => $control_html} {$control_html} {/foreach} {if !empty($channel.id)} {/if} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.