1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Catalog_Model_Product_Option_Value_Api extends Mage_Catalog_Model_Api_Resource
35: {
36: 37: 38: 39: 40: 41: 42:
43: public function items($optionId, $store = null)
44: {
45:
46: $option = $this->_prepareOption($optionId, $store);
47: $productOptionValues = $option->getValuesCollection();
48: $result = array();
49: foreach($productOptionValues as $value){
50: $result[] = array(
51: 'value_id' => $value->getId(),
52: 'title' => $value->getTitle(),
53: 'price' => $value->getPrice(),
54: 'price_type' => $value->getPriceType(),
55: 'sku' => $value->getSku(),
56: 'sort_order' => $value->getSortOrder()
57: );
58: }
59: return $result;
60: }
61:
62: 63: 64: 65: 66: 67: 68:
69: public function info($valueId, $store = null)
70: {
71:
72: $productOptionValue = Mage::getModel('catalog/product_option_value')->load($valueId);
73: if (!$productOptionValue->getId()) {
74: $this->_fault('value_not_exists');
75: }
76: $storeId = $this->_getStoreId($store);
77: $productOptionValues = $productOptionValue
78: ->getValuesByOption(
79: array($valueId),
80: $productOptionValue->getOptionId(),
81: $storeId
82: )
83: ->addTitleToResult($storeId)
84: ->addPriceToResult($storeId);
85:
86: $result = $productOptionValues->toArray();
87:
88: $result = reset($result['items']);
89: if (empty($result)) {
90: $this->_fault('value_not_exists');
91: }
92:
93: $result['value_id'] = $result['option_type_id'];
94: unset($result['option_type_id']);
95: return $result;
96: }
97:
98: 99: 100: 101: 102: 103: 104: 105:
106: public function add($optionId, $data, $store = null)
107: {
108:
109: $option = $this->_prepareOption($optionId, $store);
110:
111: $optionValueModel = Mage::getModel('catalog/product_option_value');
112: $optionValueModel->setOption($option);
113: foreach ($data as &$optionValue) {
114: foreach ($optionValue as &$value) {
115: $value = Mage::helper('catalog')->stripTags($value);
116: }
117: }
118: $optionValueModel->setValues($data);
119: try {
120: $optionValueModel->saveValues();
121: } catch (Exception $e) {
122: $this->_fault('add_option_value_error', $e->getMessage());
123: }
124: return true;
125: }
126:
127: 128: 129: 130: 131: 132: 133: 134:
135: public function update($valueId, $data, $store = null)
136: {
137:
138: $productOptionValue = Mage::getModel('catalog/product_option_value')->load($valueId);
139: if (!$productOptionValue->getId()) {
140: $this->_fault('value_not_exists');
141: }
142:
143:
144: $option = $this->_prepareOption($productOptionValue->getOptionId(), $store);
145: if (!$option->getId()) {
146: $this->_fault('option_not_exists');
147: }
148: $productOptionValue->setOption($option);
149:
150: foreach ($data as $key => $value) {
151: $data[$key] = Mage::helper('catalog')->stripTags($value);
152: }
153: if (!isset($data['title']) OR empty($data['title'])) {
154: $this->_fault('option_value_title_required');
155: }
156: $data['option_type_id'] = $valueId;
157: $data['store_id'] = $this->_getStoreId($store);
158: $productOptionValue->addValue($data);
159: $productOptionValue->setData($data);
160:
161: try {
162: $productOptionValue->save()->saveValues();
163: } catch (Exception $e) {
164: $this->_fault('update_option_value_error', $e->getMessage());
165: }
166:
167: return true;
168: }
169:
170: 171: 172: 173: 174: 175:
176: public function remove($valueId)
177: {
178:
179: $optionValue = Mage::getModel('catalog/product_option_value')->load($valueId);
180: if (!$optionValue->getId()) {
181: $this->_fault('value_not_exists');
182: }
183:
184:
185: if(count($this->items($optionValue->getOptionId())) <= 1){
186: $this->_fault('cant_delete_last_value');
187: }
188:
189: try {
190: $optionValue->delete();
191: } catch (Mage_Core_Exception $e) {
192: $this->_fault('not_deleted', $e->getMessage());
193: }
194:
195: return true;
196: }
197:
198: 199: 200: 201: 202: 203: 204:
205: protected function _prepareOption($optionId, $store = null)
206: {
207:
208: $option = Mage::getModel('catalog/product_option');
209: if (is_string($store) || is_integer($store)) {
210: $storeId = $this->_getStoreId($store);
211: $option->setStoreId($storeId);
212: }
213: $option->load($optionId);
214: if (isset($storeId)) {
215: $option->setData('store_id', $storeId);
216: }
217: if (!$option->getId()) {
218: $this->_fault('option_not_exists');
219: }
220: if ($option->getGroupByType() != Mage_Catalog_Model_Product_Option::OPTION_GROUP_SELECT) {
221: $this->_fault('invalid_option_type');
222: }
223: return $option;
224: }
225:
226: }
227: