00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Catalog_Model_Product_Api extends Mage_Catalog_Model_Api_Resource
00035 {
00036 protected $_filtersMap = array(
00037 'product_id' => 'entity_id',
00038 'set' => 'attribute_set_id',
00039 'type' => 'type_id'
00040 );
00041
00042 public function __construct()
00043 {
00044 $this->_storeIdSessionField = 'product_store_id';
00045 $this->_ignoredAttributeTypes[] = 'gallery';
00046 $this->_ignoredAttributeTypes[] = 'media_image';
00047 }
00048
00049
00050
00051
00052
00053
00054
00055
00056 public function items($filters = null, $store = null)
00057 {
00058 $collection = Mage::getModel('catalog/product')->getCollection()
00059 ->setStoreId($this->_getStoreId($store))
00060 ->addAttributeToSelect('name');
00061
00062 if (is_array($filters)) {
00063 try {
00064 foreach ($filters as $field => $value) {
00065 if (isset($this->_filtersMap[$field])) {
00066 $field = $this->_filtersMap[$field];
00067 }
00068
00069 $collection->addFieldToFilter($field, $value);
00070 }
00071 } catch (Mage_Core_Exception $e) {
00072 $this->_fault('filters_invalid', $e->getMessage());
00073 }
00074 }
00075
00076 $result = array();
00077
00078 foreach ($collection as $product) {
00079
00080 $result[] = array(
00081 'product_id' => $product->getId(),
00082 'sku' => $product->getSku(),
00083 'name' => $product->getName(),
00084 'set' => $product->getAttributeSetId(),
00085 'type' => $product->getTypeId(),
00086 'category_ids' => $product->getCategoryIds()
00087 );
00088 }
00089
00090 return $result;
00091 }
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 public function info($productId, $store = null, $attributes = null)
00102 {
00103 $product = $this->_getProduct($productId, $store);
00104
00105 if (!$product->getId()) {
00106 $this->_fault('not_exists');
00107 }
00108
00109 $result = array(
00110 'product_id' => $product->getId(),
00111 'sku' => $product->getSku(),
00112 'set' => $product->getAttributeSetId(),
00113 'type' => $product->getTypeId(),
00114 'categories' => $product->getCategoryIds(),
00115 'websites' => $product->getWebsiteIds()
00116 );
00117
00118 foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
00119 if ($this->_isAllowedAttribute($attribute, $attributes)) {
00120 $result[$attribute->getAttributeCode()] = $product->getData(
00121 $attribute->getAttributeCode());
00122 }
00123 }
00124
00125 return $result;
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 public function create($type, $set, $sku, $productData)
00137 {
00138 if (!$type || !$set || !$sku) {
00139 $this->_fault('data_invalid');
00140 }
00141
00142 $product = Mage::getModel('catalog/product');
00143
00144 $product->setStoreId($this->_getStoreId($store))
00145 ->setAttributeSetId($set)
00146 ->setTypeId($type)
00147 ->setSku($sku);
00148
00149 if (isset($productData['website_ids']) && is_array($productData['website_ids'])) {
00150 $product->setWebsiteIds($productData['website_ids']);
00151 }
00152
00153 foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
00154 if ($this->_isAllowedAttribute($attribute)
00155 && isset($productData[$attribute->getAttributeCode()])) {
00156 $product->setData(
00157 $attribute->getAttributeCode(),
00158 $productData[$attribute->getAttributeCode()]
00159 );
00160 }
00161 }
00162
00163 $this->_prepareDataForSave($product, $productData);
00164
00165 if (is_array($errors = $product->validate())) {
00166 $this->_fault('data_invalid', implode("\n", $errors));
00167 }
00168
00169 try {
00170 $product->save();
00171 } catch (Mage_Core_Exception $e) {
00172 $this->_fault('data_invalid', $e->getMessage());
00173 }
00174
00175 return $product->getId();
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 public function update($productId, $productData = array(), $store = null)
00187 {
00188 $product = $this->_getProduct($productId, $store);
00189
00190 if (!$product->getId()) {
00191 $this->_fault('not_exists');
00192 }
00193
00194 if (isset($productData['website_ids']) && is_array($productData['website_ids'])) {
00195 $product->setWebsiteIds($productData['website_ids']);
00196 }
00197
00198 foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
00199 if ($this->_isAllowedAttribute($attribute)
00200 && isset($productData[$attribute->getAttributeCode()])) {
00201 $product->setData(
00202 $attribute->getAttributeCode(),
00203 $productData[$attribute->getAttributeCode()]
00204 );
00205 }
00206 }
00207
00208 $this->_prepareDataForSave($product, $productData);
00209
00210 try {
00211 if (is_array($errors = $product->validate())) {
00212 $this->_fault('data_invalid', implode("\n", $errors));
00213 }
00214 } catch (Mage_Core_Exception $e) {
00215 $this->_fault('data_invalid', $e->getMessage());
00216 }
00217
00218 try {
00219 $product->save();
00220 } catch (Mage_Core_Exception $e) {
00221 $this->_fault('data_invalid', $e->getMessage());
00222 }
00223
00224 return true;
00225 }
00226
00227
00228
00229
00230
00231
00232
00233
00234 protected function _prepareDataForSave ($product, $productData)
00235 {
00236 if (isset($productData['categories']) && is_array($productData['categories'])) {
00237 $product->setCategoryIds($productData['categories']);
00238 }
00239
00240 if (isset($productData['websites']) && is_array($productData['websites'])) {
00241 foreach ($productData['websites'] as &$website) {
00242 if (is_string($website)) {
00243 try {
00244 $website = Mage::app()->getWebsite($website)->getId();
00245 } catch (Exception $e) { }
00246 }
00247 }
00248 $product->setWebsiteIds($productData['websites']);
00249 }
00250
00251 if (isset($productData['stock_data']) && is_array($productData['stock_data'])) {
00252 $product->setStockData($productData['stock_data']);
00253 }
00254 }
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 public function setSpecialPrice($productId, $specialPrice = null, $fromDate = null, $toDate = null, $store = null)
00267 {
00268 return $this->update($productId, array(
00269 'special_price' => $specialPrice,
00270 'special_from_date' => $fromDate,
00271 'special_to_date' => $toDate
00272 ), $store);
00273 }
00274
00275
00276
00277
00278
00279
00280
00281
00282 public function getSpecialPrice($productId, $store = null)
00283 {
00284 return $this->info($productId, $store, array('special_price', 'special_from_date', 'special_to_date'));
00285 }
00286
00287
00288
00289
00290
00291
00292
00293 public function delete($productId)
00294 {
00295 $product = $this->_getProduct($productId);
00296
00297 if (!$product->getId()) {
00298 $this->_fault('not_exists');
00299 }
00300
00301 try {
00302 $product->delete();
00303 } catch (Mage_Core_Exception $e) {
00304 $this->_fault('not_deleted', $e->getMessage());
00305 }
00306
00307 return true;
00308 }
00309 }