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 extends Mage_Catalog_Model_Api_Resource
35: {
36: protected $_filtersMap = array(
37: 'product_id' => 'entity_id',
38: 'set' => 'attribute_set_id',
39: 'type' => 'type_id'
40: );
41:
42: protected $_defaultProductAttributeList = array(
43: 'type_id',
44: 'category_ids',
45: 'website_ids',
46: 'name',
47: 'description',
48: 'short_description',
49: 'sku',
50: 'weight',
51: 'status',
52: 'url_key',
53: 'url_path',
54: 'visibility',
55: 'has_options',
56: 'gift_message_available',
57: 'price',
58: 'special_price',
59: 'special_from_date',
60: 'special_to_date',
61: 'tax_class_id',
62: 'tier_price',
63: 'meta_title',
64: 'meta_keyword',
65: 'meta_description',
66: 'custom_design',
67: 'custom_layout_update',
68: 'options_container',
69: 'image_label',
70: 'small_image_label',
71: 'thumbnail_label',
72: 'created_at',
73: 'updated_at'
74: );
75:
76: public function __construct()
77: {
78: $this->_storeIdSessionField = 'product_store_id';
79: $this->_ignoredAttributeTypes[] = 'gallery';
80: $this->_ignoredAttributeTypes[] = 'media_image';
81: }
82:
83: 84: 85: 86: 87: 88: 89:
90: public function items($filters = null, $store = null)
91: {
92: $collection = Mage::getModel('catalog/product')->getCollection()
93: ->addStoreFilter($this->_getStoreId($store))
94: ->addAttributeToSelect('name');
95:
96:
97: $apiHelper = Mage::helper('api');
98: $filters = $apiHelper->parseFilters($filters, $this->_filtersMap);
99: try {
100: foreach ($filters as $field => $value) {
101: $collection->addFieldToFilter($field, $value);
102: }
103: } catch (Mage_Core_Exception $e) {
104: $this->_fault('filters_invalid', $e->getMessage());
105: }
106: $result = array();
107: foreach ($collection as $product) {
108: $result[] = array(
109: 'product_id' => $product->getId(),
110: 'sku' => $product->getSku(),
111: 'name' => $product->getName(),
112: 'set' => $product->getAttributeSetId(),
113: 'type' => $product->getTypeId(),
114: 'category_ids' => $product->getCategoryIds(),
115: 'website_ids' => $product->getWebsiteIds()
116: );
117: }
118: return $result;
119: }
120:
121: 122: 123: 124: 125: 126: 127: 128:
129: public function info($productId, $store = null, $attributes = null, $identifierType = null)
130: {
131: $product = $this->_getProduct($productId, $store, $identifierType);
132:
133:
134: $result = array(
135: 'product_id' => $product->getId(),
136: 'sku' => $product->getSku(),
137: 'set' => $product->getAttributeSetId(),
138: 'type' => $product->getTypeId(),
139: 'categories' => $product->getCategoryIds(),
140: 'websites' => $product->getWebsiteIds()
141: );
142:
143: foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
144: if ($this->_isAllowedAttribute($attribute, $attributes)) {
145: $result[$attribute->getAttributeCode()] = $product->getData(
146: $attribute->getAttributeCode());
147: }
148: }
149:
150: return $result;
151: }
152:
153: 154: 155: 156: 157: 158: 159: 160: 161: 162:
163: public function create($type, $set, $sku, $productData, $store = null)
164: {
165: if (!$type || !$set || !$sku) {
166: $this->_fault('data_invalid');
167: }
168:
169: $this->_checkProductTypeExists($type);
170: $this->_checkProductAttributeSet($set);
171:
172:
173: $product = Mage::getModel('catalog/product');
174: $product->setStoreId($this->_getStoreId($store))
175: ->setAttributeSetId($set)
176: ->setTypeId($type)
177: ->setSku($sku);
178:
179: if (!isset($productData['stock_data']) || !is_array($productData['stock_data'])) {
180:
181: $product->setStockData(array('use_config_manage_stock' => 0));
182: }
183:
184: foreach ($product->getMediaAttributes() as $mediaAttribute) {
185: $mediaAttrCode = $mediaAttribute->getAttributeCode();
186: $product->setData($mediaAttrCode, 'no_selection');
187: }
188:
189: $this->_prepareDataForSave($product, $productData);
190:
191: try {
192: 193: 194: 195:
196: if (is_array($errors = $product->validate())) {
197: $strErrors = array();
198: foreach($errors as $code => $error) {
199: if ($error === true) {
200: $error = Mage::helper('catalog')->__('Attribute "%s" is invalid.', $code);
201: }
202: $strErrors[] = $error;
203: }
204: $this->_fault('data_invalid', implode("\n", $strErrors));
205: }
206:
207: $product->save();
208: } catch (Mage_Core_Exception $e) {
209: $this->_fault('data_invalid', $e->getMessage());
210: }
211:
212: return $product->getId();
213: }
214:
215: 216: 217: 218: 219: 220: 221: 222:
223: public function update($productId, $productData, $store = null, $identifierType = null)
224: {
225: $product = $this->_getProduct($productId, $store, $identifierType);
226:
227: $this->_prepareDataForSave($product, $productData);
228:
229: try {
230: 231: 232: 233:
234: if (is_array($errors = $product->validate())) {
235: $strErrors = array();
236: foreach($errors as $code => $error) {
237: if ($error === true) {
238: $error = Mage::helper('catalog')->__('Value for "%s" is invalid.', $code);
239: } else {
240: $error = Mage::helper('catalog')->__('Value for "%s" is invalid: %s', $code, $error);
241: }
242: $strErrors[] = $error;
243: }
244: $this->_fault('data_invalid', implode("\n", $strErrors));
245: }
246:
247: $product->save();
248: } catch (Mage_Core_Exception $e) {
249: $this->_fault('data_invalid', $e->getMessage());
250: }
251:
252: return true;
253: }
254:
255: 256: 257: 258: 259: 260: 261:
262: protected function _prepareDataForSave($product, $productData)
263: {
264: if (isset($productData['website_ids']) && is_array($productData['website_ids'])) {
265: $product->setWebsiteIds($productData['website_ids']);
266: }
267:
268: foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
269:
270: if (Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID !== (int) $product->getStoreId()
271: && !$product->getExistsStoreValueFlag($attribute->getAttributeCode())
272: && !$attribute->isScopeGlobal()
273: ) {
274: $product->setData($attribute->getAttributeCode(), false);
275: }
276:
277: if ($this->_isAllowedAttribute($attribute)) {
278: if (isset($productData[$attribute->getAttributeCode()])) {
279: $product->setData(
280: $attribute->getAttributeCode(),
281: $productData[$attribute->getAttributeCode()]
282: );
283: } elseif (isset($productData['additional_attributes']['single_data'][$attribute->getAttributeCode()])) {
284: $product->setData(
285: $attribute->getAttributeCode(),
286: $productData['additional_attributes']['single_data'][$attribute->getAttributeCode()]
287: );
288: } elseif (isset($productData['additional_attributes']['multi_data'][$attribute->getAttributeCode()])) {
289: $product->setData(
290: $attribute->getAttributeCode(),
291: $productData['additional_attributes']['multi_data'][$attribute->getAttributeCode()]
292: );
293: }
294: }
295: }
296:
297: if (isset($productData['categories']) && is_array($productData['categories'])) {
298: $product->setCategoryIds($productData['categories']);
299: }
300:
301: if (isset($productData['websites']) && is_array($productData['websites'])) {
302: foreach ($productData['websites'] as &$website) {
303: if (is_string($website)) {
304: try {
305: $website = Mage::app()->getWebsite($website)->getId();
306: } catch (Exception $e) { }
307: }
308: }
309: $product->setWebsiteIds($productData['websites']);
310: }
311:
312: if (Mage::app()->isSingleStoreMode()) {
313: $product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
314: }
315:
316: if (isset($productData['stock_data']) && is_array($productData['stock_data'])) {
317: $product->setStockData($productData['stock_data']);
318: }
319:
320: if (isset($productData['tier_price']) && is_array($productData['tier_price'])) {
321: $tierPrices = Mage::getModel('catalog/product_attribute_tierprice_api')
322: ->prepareTierPrices($product, $productData['tier_price']);
323: $product->setData(Mage_Catalog_Model_Product_Attribute_Tierprice_Api::ATTRIBUTE_CODE, $tierPrices);
324: }
325: }
326:
327: 328: 329: 330: 331: 332: 333: 334: 335: 336:
337: public function setSpecialPrice($productId, $specialPrice = null, $fromDate = null, $toDate = null, $store = null)
338: {
339: return $this->update($productId, array(
340: 'special_price' => $specialPrice,
341: 'special_from_date' => $fromDate,
342: 'special_to_date' => $toDate
343: ), $store);
344: }
345:
346: 347: 348: 349: 350: 351: 352:
353: public function getSpecialPrice($productId, $store = null)
354: {
355: return $this->info($productId, $store, array('special_price', 'special_from_date', 'special_to_date'));
356: }
357:
358: 359: 360: 361: 362: 363:
364: public function delete($productId, $identifierType = null)
365: {
366: $product = $this->_getProduct($productId, null, $identifierType);
367:
368: try {
369: $product->delete();
370: } catch (Mage_Core_Exception $e) {
371: $this->_fault('not_deleted', $e->getMessage());
372: }
373:
374: return true;
375: }
376:
377: 378: 379: 380: 381: 382: 383:
384: public function getAdditionalAttributes($productType, $attributeSetId)
385: {
386: $this->_checkProductTypeExists($productType);
387: $this->_checkProductAttributeSet($attributeSetId);
388:
389:
390: $productAttributes = Mage::getModel('catalog/product')
391: ->setAttributeSetId($attributeSetId)
392: ->setTypeId($productType)
393: ->getTypeInstance(false)
394: ->getEditableAttributes();
395:
396: $result = array();
397: foreach ($productAttributes as $attribute) {
398:
399: if ($attribute->isInSet($attributeSetId) && $this->_isAllowedAttribute($attribute)
400: && !in_array($attribute->getAttributeCode(), $this->_defaultProductAttributeList)) {
401:
402: if ($attribute->isScopeGlobal()) {
403: $scope = 'global';
404: } elseif ($attribute->isScopeWebsite()) {
405: $scope = 'website';
406: } else {
407: $scope = 'store';
408: }
409:
410: $result[] = array(
411: 'attribute_id' => $attribute->getId(),
412: 'code' => $attribute->getAttributeCode(),
413: 'type' => $attribute->getFrontendInput(),
414: 'required' => $attribute->getIsRequired(),
415: 'scope' => $scope
416: );
417: }
418: }
419:
420: return $result;
421: }
422:
423: 424: 425: 426: 427: 428: 429:
430: protected function _checkProductTypeExists($productType)
431: {
432: if (!in_array($productType, array_keys(Mage::getModel('catalog/product_type')->getOptionArray()))) {
433: $this->_fault('product_type_not_exists');
434: }
435: }
436:
437: 438: 439: 440: 441: 442: 443:
444: protected function _checkProductAttributeSet($attributeSetId)
445: {
446: $attributeSet = Mage::getModel('eav/entity_attribute_set')->load($attributeSetId);
447: if (is_null($attributeSet->getId())) {
448: $this->_fault('product_attribute_set_not_exists');
449: }
450: if (Mage::getModel('catalog/product')->getResource()->getTypeId() != $attributeSet->getEntityTypeId()) {
451: $this->_fault('product_attribute_set_not_valid');
452: }
453: }
454: }
455: