0

I've got a product attribute "size" set to "text swatch" and already populated with some values. Those values have been added via backend and they work fine.

Now I need to add other values to this attribute via php code but I'm having some difficulties:

  • the admin value for the options I'm placing via php code is not saving
  • the options I'm creating via php code aren't being placed at the end of the list but at the beginning. This means:

a) the first option I created via backend is partially overwritten by the first option I'm creating via php.

b) as a consequence of point a), the default store view value for the php options don't match with the actual description and admin value

Here is a screenshot of the problem:

As you can see from the picture:

  • the first option is partially overwritten (default store view is overwritten)
  • the other options don't overwrite the other existing option but the default store view don't match the default value and the description
  • Admin field is empty

This is the code I've used:

protected function importCustomSizes() {
    $attribute = $this->loadAttribute('size');
    $values = ['small','medium','large'];
    $data = $this->generateOptions($values, 'text');
    $attribute->addData($data)->save();
    return $this;
}

protected function generateOptions(array $values, $swatchType = 'dropdown') {
    $i = 0;
    foreach($values as $value) {
        $order["option_{$i}"] = $i;

        $optionsStore["option_{$i}"] = array(
            0 => $value, // admin
            1 => $value, // default store view
        );

        $textSwatch["option_{$i}"] = array(
            1 => $value,
        );

        $visualSwatch["option_{$i}"] = '';

        $delete["option_{$i}"] = '';

        $this->_mainhelper->log(" - Option {$value} added for the attribute.");

        $i++;
    }
    switch($swatchType) {
        case 'text':
            return [
                'optiontext' => [
                    'order'     => $order,
                    'value'     => $optionsStore,
                    'delete'    => $delete,
                ],
                'swatchtext' => [
                    'value'     => $textSwatch,
                ],
            ];
            break;
        case 'visual':
            return [
                'optionvisual' => [
                    'order'     => $order,
                    'value'     => $optionsStore,
                    'delete'    => $delete,
                ],
                'swatchvisual' => [
                    'value'     => $visualSwatch,
                ],
            ];
            break;
        default:
            return [
                'option' => [
                    'order'     => $order,
                    'value'     => $optionsStore,
                    'delete'    => $delete,
                ],
            ];
    }
}

private function loadAttribute($attrcode) {
    return $this->_attributeRepository->get('catalog_product', $attrcode);
}

Can someone help me solve this issue?

EDIT:

I've managed to solve most of those issues by leaving the optiontext order empty and by adding the admin value to the textswatch key of the array. This way the only problem left is that the new options are saved at the beginning of the option list instead of being at the end. So does somebody know how can I save them at the end of the list?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.