backend_prod_sku_fields

Allows adding custom fields to the product variant editing form in the new interface (2.0).

Shop-Script

Input (passed by reference)

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

Output

{$backend_prod_sku_fields.$output}
Array of custom product variants’ fields data. Each array item, corresponding to one of the fields, must be a subarray with the following keys:
  • 'type' — field type ('price' for custom price fields, 'textarea', 'input', 'select', 'help' for a custom HTML snippets; e.g., text hints for users),
  • 'name' — localized field title,
  • 'default_value' — field’s default value used when a new product variant is being added,
  • 'tooltip' — localized tooltip text,
  • 'css_class' — names of CSS classes to be applied to a 'div.wa-field' selector within which a custom field is contained,
  • 'validate' — subarray 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 variants, with variant IDs as array keys.
Shop-Script

Plugin code example

PHP

public function backendProdSkuFields($params)
{
    /** @var shopProduct */
    $product = $params['product'];

    $values = [
        'price' => [],
        'textarea' => [],
        'select' => [],
        'input' => [],
        'help' => [],
    ];

    foreach (array_keys($product->getSkus()) as $variant_id) {
        $values['price'][$variant_id] = rand(0, 10000);
        $values['textarea'][$variant_id] = sprintf_wp('Variant #%d textarea value', $variant_id);
        $values['select'][$variant_id] = floor(rand(0, 3));
        $values['input'][$variant_id] = sprintf_wp('variant #%d input value', $variant_id);
        $values['help'][$variant_id] = _wp('Everything’s fine.');
    }

    return [
        [
            'type' => 'price',
            'id' => $this->id . '_price',
            'name' => _wp('My plugin’s price'),
            'css_class' => $this->id . '-price',
            'validate' => [
                'required' => true,
                'numbers' => true,
            ],
            'sku_values' => $values['price'],
            'default_value' => '0',
        ],
        [
            'type' => 'input',
            'id' => $this->id . '_input',
            'name' => _wp('My plugin’s input'),
            'css_class' => $this->id . '-input',
            'tooltip' => _wp('My plugin’s input hint'),
            'sku_values' => $values['input'],
            'default_value' => 'default input value',
        ],
        [
            'type' => 'textarea',
            'id' => $this->id . '_textarea',
            'name' => _wp('My plugin’s textarea'),
            'css_class' => $this->id . '-textarea',
            'tooltip' => _wp('My plugin’s textarea hint'),
            'sku_values' => $values['textarea'],
            'default_value' => 'default textarea value',
        ],
        [
            'type' => 'select',
            'id' => $this->id . '_select',
            'name' => _wp('My plugin’s select'),
            'css_class' => $this->id . '-select',
            'tooltip' => _wp('My plugin’s select hint'),
            'options' => [
                [
                    'name' => _wp('zero'),
                    'value' => 0,
                ],
                [
                    'name' => _wp('one'),
                    'value' => 1,
                ],
                [
                    'name' => _wp('two'),
                    'value' => 2,
                ],
                [
                    'name' => _wp('three'),
                    'value' => 3,
                ],
            ],
            'sku_values' => $values['select'],
            'default_value' => 0,
        ],
        [
            'type' => 'help',
            'id' => $this->id . '_help',
            'name' => _wp('My plugin’s notice'),
            'css_class' => $this->id . '-help',
            'placement' => 'bottom',
            'sku_values' => $values['help'],
            'default_value' => _wp('User, beware!'),
        ],
    ];
}