Option example

$this->View()->assign([
           'success' => true,
           'data' => $options,
           'total' => $totalCount,
        ]);
    }

    public function createGroupAction()
    {
        $params = $this->Request()->getPost();

        $option = new Option();
        $option->fromArray($params);

        $modelManager = $this->get(\Shopware\Components\Model\ModelManager::class);
        try {
            $modelManager->persist($option);
            $modelManager->flush();
        } catch (Exception $e) {
            $this->View()->assign(['success' => false, 'message' => $e->getMessage()]);

            return;
        }

        
/** * Saves the passed configurator option data. If an id passed, the function updates the existing options, otherwise * a new option will be created. */
    public function saveConfiguratorOptionAction()
    {
        $id = (int) $this->Request()->getParam('id');
        if (!empty($id)) {
            $option = $this->get('models')->find(Option::class$id);
        } else {
            $option = new Option();
        }
        $data = $this->Request()->getParams();
        if (empty($data['groupId'])) {
            return;
        }
        $data['group'] = $this->get('models')->find(Group::class$data['groupId']);

        $option->fromArray($data);
        $this->get('models')->persist($option);
        $this->get('models')->flush();
        $data['id'] = $option->getId();

        
return $group;
    }

    /** * @param array<string, mixed> $data * * @return Option */
    public function hydrateOption(array $data)
    {
        $option = new Option();
        $translation = $this->getTranslation($data, '__propertyOption', ['optionValue' => 'value']);
        $data = array_merge($data$translation);

        $option->setId((int) $data['__propertyOption_id']);
        $option->setName($data['__propertyOption_value']);
        $option->setPosition((int) $data['__propertyOption_position']);

        if ($data['__propertyOptionAttribute_id']) {
            $this->attributeHydrator->addAttribute($option$data, 'propertyOptionAttribute', 'core', 'propertyOption');
        }

        
$this->attributeHydrator->addAttribute($group$data, 'configuratorGroupAttribute', null, 'configuratorGroup');
        }

        return $group;
    }

    /** * @param array<string, mixed> $data */
    private function createOption(array $data): Option
    {
        $option = new Option();

        $translation = $this->getTranslation($data, '__configuratorOption');
        $data = array_merge($data$translation);

        $option->setId((int) $data['__configuratorOption_id']);
        $option->setName($data['__configuratorOption_name']);

        if ($data['__configuratorOptionAttribute_id']) {
            $this->attributeHydrator->addAttribute($option$data, 'configuratorOptionAttribute', null, 'configuratorOption');
        }
        if (isset($data['__media_id'])) {
            
$relation = $query->getOneOrNullResult(self::HYDRATE_OBJECT);
                        if (!$relation instanceof Relation) {
                            // checks if a new option was created                             // because the new option is not written to the database at this point                             $groupOption = $this->getCollectionElementByProperty(
                                $propertyGroup->getOptions(),
                                'name',
                                $valueData['option']['name']
                            );
                            // Creates a new option                             if ($groupOption === null) {
                                $option = new Option();
                                $propertyGroup->addOption($option);
                            } else {
                                $option = $groupOption;
                            }
                        } else {
                            $option = $relation->getOption();
                        }
                    } else {
                        throw new CustomValidationException('A property option needs to be given for each property value');
                    }
                    if (!$option instanceof Option) {
                        
// Check if the option is available in the configured product configurator set.             $option = $this->getAvailableOption($availableGroup->getOptions()[
                'id' => $optionData['optionId'] ?? null,
                'name' => $optionData['option'] ?? null,
            ]);

            if (!$option) {
                if (!$optionData['option']) {
                    throw new CustomValidationException('A new configurator option requires a name');
                }

                $option = new Option();
                $option->setPosition(0);
                if (\array_key_exists('position', $optionData)) {
                    $option->setPosition((int) $optionData['position']);
                }
                $option->setName($optionData['option']);
                $option->setGroup($availableGroup);
                $this->getManager()->persist($option);
            }
            $options->add($option);
        }

        
Home | Imprint | This part of the site doesn't use cookies.