public function ajaxSaveAttributeOption() { $errors = array(); if (!isset($_POST['attribute_id']) || !is_numeric($_POST['attribute_id'])) { $errors[] = __('Respective attribute is not set.', 'jigoshop'); } if (!isset($_POST['label']) || empty($_POST['label'])) { $errors[] = __('Option label is not set.', 'jigoshop'); } if (!empty($errors)) { echo json_encode(array('success' => false, 'error' => join('<br/>', $errors))); exit; } $attribute = $this->productService->getAttribute((int) $_POST['attribute_id']); if (isset($_POST['id'])) { $option = $attribute->removeOption($_POST['id']); } else { $option = new Attribute\Option(); } $option->setLabel(trim(htmlspecialchars(strip_tags($_POST['label'])))); if (isset($_POST['slug']) && !empty($_POST['slug'])) { $option->setValue(trim(htmlspecialchars(strip_tags($_POST['value'])))); } else { $option->setValue($this->wp->getHelpers()->sanitizeTitle($option->getLabel())); } $attribute->addOption($option); $this->productService->saveAttribute($attribute); echo json_encode(array('success' => true, 'html' => Render::get('admin/product_attributes/option', array('id' => $attribute->getId(), 'option_id' => $option->getId(), 'option' => $option)))); exit; }
/** * Adds option to the attribute. * * @param Option $option Option to add. */ public function addOption(Attribute\Option $option) { $option->setAttribute($this); // Ability to add multiple new options $id = $option->getId(); if (empty($id)) { $id = hash('md5', $option->getLabel()); } $this->options[$id] = $option; }