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_Api_V2 extends Mage_Catalog_Model_Product_Api
35: {
36: 37: 38: 39: 40: 41: 42: 43:
44: public function info($productId, $store = null, $attributes = null, $identifierType = null)
45: {
46: $product = $this->_getProduct($productId, $store, $identifierType);
47:
48: $result = array(
49: 'product_id' => $product->getId(),
50: 'sku' => $product->getSku(),
51: 'set' => $product->getAttributeSetId(),
52: 'type' => $product->getTypeId(),
53: 'categories' => $product->getCategoryIds(),
54: 'websites' => $product->getWebsiteIds()
55: );
56:
57: $allAttributes = array();
58: if (!empty($attributes->attributes)) {
59: $allAttributes = array_merge($allAttributes, $attributes->attributes);
60: } else {
61: foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
62: if ($this->_isAllowedAttribute($attribute, $attributes)) {
63: $allAttributes[] = $attribute->getAttributeCode();
64: }
65: }
66: }
67:
68: $_additionalAttributeCodes = array();
69: if (!empty($attributes->additional_attributes)) {
70: foreach ($attributes->additional_attributes as $k => $_attributeCode) {
71: $allAttributes[] = $_attributeCode;
72: $_additionalAttributeCodes[] = $_attributeCode;
73: }
74: }
75:
76: $_additionalAttribute = 0;
77: foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
78: if ($this->_isAllowedAttribute($attribute, $allAttributes)) {
79: if (in_array($attribute->getAttributeCode(), $_additionalAttributeCodes)) {
80: $result['additional_attributes'][$_additionalAttribute]['key'] = $attribute->getAttributeCode();
81: $result['additional_attributes'][$_additionalAttribute]['value'] = $product
82: ->getData($attribute->getAttributeCode());
83: $_additionalAttribute++;
84: } else {
85: $result[$attribute->getAttributeCode()] = $product->getData($attribute->getAttributeCode());
86: }
87: }
88: }
89:
90: return $result;
91: }
92:
93: 94: 95: 96: 97: 98: 99: 100: 101: 102:
103: public function create($type, $set, $sku, $productData, $store = null)
104: {
105: if (!$type || !$set || !$sku) {
106: $this->_fault('data_invalid');
107: }
108:
109: $this->_checkProductTypeExists($type);
110: $this->_checkProductAttributeSet($set);
111:
112:
113: $product = Mage::getModel('catalog/product');
114: $product->setStoreId($this->_getStoreId($store))
115: ->setAttributeSetId($set)
116: ->setTypeId($type)
117: ->setSku($sku);
118:
119: if (!property_exists($productData, 'stock_data')) {
120:
121: $_stockData = array('use_config_manage_stock' => 0);
122: $product->setStockData($_stockData);
123: }
124:
125: foreach ($product->getMediaAttributes() as $mediaAttribute) {
126: $mediaAttrCode = $mediaAttribute->getAttributeCode();
127: $product->setData($mediaAttrCode, 'no_selection');
128: }
129:
130: $this->_prepareDataForSave($product, $productData);
131:
132: try {
133: 134: 135: 136:
137: if (is_array($errors = $product->validate())) {
138: $strErrors = array();
139: foreach($errors as $code => $error) {
140: if ($error === true) {
141: $error = Mage::helper('catalog')->__('Attribute "%s" is invalid.', $code);
142: }
143: $strErrors[] = $error;
144: }
145: $this->_fault('data_invalid', implode("\n", $strErrors));
146: }
147:
148: $product->save();
149: } catch (Mage_Core_Exception $e) {
150: $this->_fault('data_invalid', $e->getMessage());
151: }
152:
153: return $product->getId();
154: }
155:
156: 157: 158: 159: 160: 161: 162: 163:
164: public function update($productId, $productData, $store = null, $identifierType = null)
165: {
166: $product = $this->_getProduct($productId, $store, $identifierType);
167:
168: $this->_prepareDataForSave($product, $productData);
169:
170: try {
171: 172: 173: 174:
175: if (is_array($errors = $product->validate())) {
176: $strErrors = array();
177: foreach($errors as $code => $error) {
178: if ($error === true) {
179: $error = Mage::helper('catalog')->__('Value for "%s" is invalid.', $code);
180: } else {
181: $error = Mage::helper('catalog')->__('Value for "%s" is invalid: %s', $code, $error);
182: }
183: $strErrors[] = $error;
184: }
185: $this->_fault('data_invalid', implode("\n", $strErrors));
186: }
187:
188: $product->save();
189: } catch (Mage_Core_Exception $e) {
190: $this->_fault('data_invalid', $e->getMessage());
191: }
192:
193: return true;
194: }
195:
196: 197: 198: 199: 200: 201: 202:
203: protected function _prepareDataForSave ($product, $productData)
204: {
205: if (property_exists($productData, 'website_ids') && is_array($productData->website_ids)) {
206: $product->setWebsiteIds($productData->website_ids);
207: }
208:
209: if (property_exists($productData, 'additional_attributes')) {
210: if (property_exists($productData->additional_attributes, 'single_data')) {
211: foreach ($productData->additional_attributes->single_data as $_attribute) {
212: $_attrCode = $_attribute->key;
213: $productData->$_attrCode = $_attribute->value;
214: }
215: }
216: if (property_exists($productData->additional_attributes, 'multi_data')) {
217: foreach ($productData->additional_attributes->multi_data as $_attribute) {
218: $_attrCode = $_attribute->key;
219: $productData->$_attrCode = $_attribute->value;
220: }
221: }
222: unset($productData->additional_attributes);
223: }
224:
225: foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
226: $_attrCode = $attribute->getAttributeCode();
227:
228:
229: if (Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID !== (int) $product->getStoreId()
230: && !$product->getExistsStoreValueFlag($_attrCode)
231: && !$attribute->isScopeGlobal()
232: ) {
233: $product->setData($_attrCode, false);
234: }
235:
236: if ($this->_isAllowedAttribute($attribute) && (isset($productData->$_attrCode))) {
237: $product->setData(
238: $_attrCode,
239: $productData->$_attrCode
240: );
241: }
242: }
243:
244: if (property_exists($productData, 'categories') && is_array($productData->categories)) {
245: $product->setCategoryIds($productData->categories);
246: }
247:
248: if (property_exists($productData, 'websites') && is_array($productData->websites)) {
249: foreach ($productData->websites as &$website) {
250: if (is_string($website)) {
251: try {
252: $website = Mage::app()->getWebsite($website)->getId();
253: } catch (Exception $e) { }
254: }
255: }
256: $product->setWebsiteIds($productData->websites);
257: }
258:
259: if (Mage::app()->isSingleStoreMode()) {
260: $product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
261: }
262:
263: if (property_exists($productData, 'stock_data')) {
264: $_stockData = array();
265: foreach ($productData->stock_data as $key => $value) {
266: $_stockData[$key] = $value;
267: }
268: $product->setStockData($_stockData);
269: }
270:
271: if (property_exists($productData, 'tier_price')) {
272: $tierPrices = Mage::getModel('catalog/product_attribute_tierprice_api_V2')
273: ->prepareTierPrices($product, $productData->tier_price);
274: $product->setData(Mage_Catalog_Model_Product_Attribute_Tierprice_Api_V2::ATTRIBUTE_CODE, $tierPrices);
275: }
276: }
277:
278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289:
290: public function setSpecialPrice($productId, $specialPrice = null, $fromDate = null, $toDate = null, $store = null,
291: $identifierType = null
292: ) {
293: $obj = new stdClass();
294: $obj->special_price = $specialPrice;
295: $obj->special_from_date = $fromDate;
296: $obj->special_to_date = $toDate;
297: return $this->update($productId, $obj, $store, $identifierType);
298: }
299:
300: 301: 302: 303: 304: 305: 306:
307: public function getSpecialPrice($productId, $store = null)
308: {
309: return $this->info($productId, $store, array(
310: 'attributes' => array(
311: 'special_price',
312: 'special_from_date',
313: 'special_to_date'
314: )
315: )
316: );
317: }
318:
319: }
320: