backend_prod_sku_fields

Allows adding of custom fields to the SKU editing form in the new product editor.

Shop-Script

Input (passed by reference)

$params['product'] shopProduct Current product’s properties object.
… your plugin code …

Output

{$backend_prod_sku_fields.}

Array of custom SKU fields’ data. For each field, there must be an array containing various field properties with the following keys:

  • 'type' — field type ('select', 'textarea', 'price' for custom price fields, or 'help' to add a custom HTML snippet; e.g., a hint for users),
  • 'name' — localized field title,
  • 'default_value' — field’s default value
  • 'tooltip' — localized hint text for a custom field
  • 'css_class' — names of CSS classes which must be applied to the element with the 'div.wa-field' selector within which a custom field is added to the product editor page DOM
  • 'validate' — sub-array with optional Boolean keys 'required' (a field is required) or 'numbers' (a field accepts only numbers)
  • 'sku_values' — array of field’s values for each of the product’s SKUs, with SKU IDs as array keys.

Shop-Script

Plugin code example

PHP

public function backendProdSkuFields($params)
{
    /** @var shopProduct $product */
    $product = $params['product'];
    $skus_ids = array_keys($product->getSkus());

    $sku_values = [
        'price' => [],
        'textarea' => [],
        'help' => [],
    ];

    foreach ($skus_ids as $sku_id) {
        $sku_values['price'][$sku_id] = rand(0, 100);
        $sku_values['textarea'][$sku_id] = sprintf(_wp('My plugin’s textarea value for SKU #%d'), $sku_id);
        $sku_values['help'][$sku_id] = _wp('User, beware!');
    }

    return [
        [
            'type' => 'price',
            'id' => 'myplugin_price',
            'name' => _wp('My plugin’s price'),
            'css_class' => 'myplugin-class-price',
            'sku_values' => $sku_values['price'],
            'validation' => [
                'required' => true,
                'numbers' => true,
            ],
        ],
        [
            'type' => 'textarea',
            'id' => 'myplugin_textarea',
            'name' => _wp('My plugin’s textarea'),
            'css_class' => 'myplugin-class-textarea',
            'tooltip' => _wp('My plugin’s textarea hint'),
            'sku_values' => $sku_values['textarea'],
        ],
        [
            'type' => 'help',
            'id' => 'myplugin_help',
            'name' => _wp('My plugin’s hint'),
            'css_class' => 'myplugin-class-help',
            'placement' => 'bottom',
            'sku_values' => $sku_values['help'],
            // or show the same hint for all SKUs
            // 'default_value' => _wp('User, beware!'),
        ],
    ];
}