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_V2 extends Mage_Catalog_Model_Product_Api
00035 {
00036
00037
00038
00039
00040
00041
00042
00043
00044 public function items($filters = null, $store = null)
00045 {
00046 $collection = Mage::getModel('catalog/product')->getCollection()
00047 ->setStoreId($this->_getStoreId($store))
00048 ->addAttributeToSelect('name');
00049
00050 $preparedFilters = array();
00051 if (isset($filters->filter)) {
00052 foreach ($filters->filter as $_filter) {
00053 $preparedFilters[$_filter->key] = $_filter->value;
00054 }
00055 }
00056 if (isset($filters->complex_filter)) {
00057 foreach ($filters->complex_filter as $_filter) {
00058 $_value = $_filter->value;
00059 $preparedFilters[$_filter->key] = array(
00060 $_value->key => $_value->value
00061 );
00062 }
00063 }
00064
00065 if (!empty($preparedFilters)) {
00066 try {
00067 foreach ($preparedFilters as $field => $value) {
00068 if (isset($this->_filtersMap[$field])) {
00069 $field = $this->_filtersMap[$field];
00070 }
00071
00072 $collection->addFieldToFilter($field, $value);
00073 }
00074 } catch (Mage_Core_Exception $e) {
00075 $this->_fault('filters_invalid', $e->getMessage());
00076 }
00077 }
00078
00079 $result = array();
00080
00081 foreach ($collection as $product) {
00082
00083 $result[] = array(
00084 'product_id' => $product->getId(),
00085 'sku' => $product->getSku(),
00086 'name' => $product->getName(),
00087 'set' => $product->getAttributeSetId(),
00088 'type' => $product->getTypeId(),
00089 'category_ids' => $product->getCategoryIds()
00090 );
00091 }
00092
00093 return $result;
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 public function info($productId, $store = null, $attributes = null)
00105 {
00106 $product = $this->_getProduct($productId, $store);
00107
00108 if (!$product->getId()) {
00109 $this->_fault('not_exists');
00110 }
00111
00112 $result = array(
00113 'product_id' => $product->getId(),
00114 'sku' => $product->getSku(),
00115 'set' => $product->getAttributeSetId(),
00116 'type' => $product->getTypeId(),
00117 'categories' => $product->getCategoryIds(),
00118 'websites' => $product->getWebsiteIds()
00119 );
00120
00121 $allAttributes = array();
00122 if (isset($attributes->attributes)) {
00123 $allAttributes += array_merge($allAttributes, $attributes->attributes);
00124 }
00125
00126 $_additionalAttributeCodes = array();
00127 if (isset($attributes->additional_attributes)) {
00128 foreach ($attributes->additional_attributes as $k => $_attributeCode) {
00129 $allAttributes[] = $_attributeCode;
00130 $_additionalAttributeCodes[] = $_attributeCode;
00131 }
00132 }
00133
00134 $_additionalAttribute = 0;
00135 foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
00136 if ($this->_isAllowedAttribute($attribute, $allAttributes)) {
00137 if (in_array($attribute->getAttributeCode(), $_additionalAttributeCodes)) {
00138 $result['additional_attributes'][$_additionalAttribute]['key'] = $attribute->getAttributeCode();
00139 $result['additional_attributes'][$_additionalAttribute]['value'] = $product->getData($attribute->getAttributeCode());
00140 $_additionalAttribute++;
00141 } else {
00142 $result[$attribute->getAttributeCode()] = $product->getData($attribute->getAttributeCode());
00143 }
00144 }
00145 }
00146
00147 return $result;
00148 }
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 public function create($type, $set, $sku, $productData)
00159 {
00160 if (!$type || !$set || !$sku) {
00161 $this->_fault('data_invalid');
00162 }
00163 $store = null;
00164 $product = Mage::getModel('catalog/product');
00165
00166 $product->setStoreId($this->_getStoreId($store))
00167 ->setAttributeSetId($set)
00168 ->setTypeId($type)
00169 ->setSku($sku);
00170
00171 if (property_exists($productData, 'website_ids') && is_array($productData->website_ids)) {
00172 $product->setWebsiteIds($productData->website_ids);
00173 }
00174
00175 if (property_exists($productData, 'additional_attributes')) {
00176 foreach ($productData->additional_attributes as $_attribute) {
00177 $_attrCode = $_attribute->key;
00178 $productData->$_attrCode = $_attribute->value;
00179 }
00180 unset($productData->additional_attributes);
00181 }
00182 foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
00183 $_attrCode = $attribute->getAttributeCode();
00184 if ($this->_isAllowedAttribute($attribute)
00185 && isset($productData->$_attrCode)) {
00186 $product->setData(
00187 $attribute->getAttributeCode(),
00188 $productData->$_attrCode
00189 );
00190 }
00191 }
00192
00193 $this->_prepareDataForSave($product, $productData);
00194
00195 if (is_array($errors = $product->validate())) {
00196 $this->_fault('data_invalid', implode("\n", $errors));
00197 }
00198
00199 try {
00200 $product->save();
00201 } catch (Mage_Core_Exception $e) {
00202 $this->_fault('data_invalid', $e->getMessage());
00203 }
00204
00205 return $product->getId();
00206 }
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 public function update($productId, $productData, $store = null)
00217 {
00218 $product = $this->_getProduct($productId, $store);
00219
00220 if (!$product->getId()) {
00221 $this->_fault('not_exists');
00222 }
00223
00224 if (property_exists($productData, 'website_ids') && is_array($productData->website_ids)) {
00225 $product->setWebsiteIds($productData->website_ids);
00226 }
00227
00228 if (property_exists($productData, 'additional_attributes')) {
00229 foreach ($productData->additional_attributes as $_attribute) {
00230 $_attrCode = $_attribute->key;
00231 $productData->$_attrCode = $_attribute->value;
00232 }
00233 unset($productData->additional_attributes);
00234 }
00235
00236 foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
00237 $_attrCode = $attribute->getAttributeCode();
00238 if ($this->_isAllowedAttribute($attribute)
00239 && isset($productData->$_attrCode)) {
00240 $product->setData(
00241 $attribute->getAttributeCode(),
00242 $productData->$_attrCode
00243 );
00244 }
00245 }
00246
00247 $this->_prepareDataForSave($product, $productData);
00248
00249 try {
00250 if (is_array($errors = $product->validate())) {
00251 $this->_fault('data_invalid', implode("\n", $errors));
00252 }
00253 } catch (Mage_Core_Exception $e) {
00254 $this->_fault('data_invalid', $e->getMessage());
00255 }
00256
00257 try {
00258 $product->save();
00259 } catch (Mage_Core_Exception $e) {
00260 $this->_fault('data_invalid', $e->getMessage());
00261 }
00262
00263 return true;
00264 }
00265
00266
00267
00268
00269
00270
00271
00272
00273 protected function _prepareDataForSave ($product, $productData)
00274 {
00275 if (property_exists($productData, 'categories') && is_array($productData->categories)) {
00276 $product->setCategoryIds($productData->categories);
00277 }
00278
00279 if (property_exists($productData, 'websites') && is_array($productData->websites)) {
00280 foreach ($productData->websites as &$website) {
00281 if (is_string($website)) {
00282 try {
00283 $website = Mage::app()->getWebsite($website)->getId();
00284 } catch (Exception $e) { }
00285 }
00286 }
00287 $product->setWebsiteIds($productData->websites);
00288 }
00289
00290 if (property_exists($productData, 'stock_data') && is_array($productData->stock_data)) {
00291 $product->setStockData($productData->stock_data);
00292 }
00293 }
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305 public function setSpecialPrice($productId, $specialPrice = null, $fromDate = null, $toDate = null, $store = null)
00306 {
00307 $obj = new stdClass();
00308 $obj->special_price = $specialPrice;
00309 $obj->special_from_date = $fromDate;
00310 $obj->special_to_date = $toDate;
00311 return $this->update($productId, $obj, $store);
00312 }
00313
00314
00315
00316
00317
00318
00319
00320
00321 public function getSpecialPrice($productId, $store = null)
00322 {
00323 return $this->info($productId, $store, array(
00324 'attributes' => array(
00325 'special_price',
00326 'special_from_date',
00327 'special_to_date'
00328 )
00329 )
00330 );
00331 }
00332
00333 }