Mage_Catalog_Model_Product_Api_V2 Class Reference

Inheritance diagram for Mage_Catalog_Model_Product_Api_V2:

Mage_Catalog_Model_Product_Api Mage_Catalog_Model_Api_Resource Mage_Api_Model_Resource_Abstract

List of all members.

Public Member Functions

 items ($filters=null, $store=null)
 info ($productId, $store=null, $attributes=null)
 create ($type, $set, $sku, $productData)
 update ($productId, $productData, $store=null)
 setSpecialPrice ($productId, $specialPrice=null, $fromDate=null, $toDate=null, $store=null)
 getSpecialPrice ($productId, $store=null)

Protected Member Functions

 _prepareDataForSave ($product, $productData)


Detailed Description

Definition at line 34 of file V2.php.


Member Function Documentation

_prepareDataForSave ( product,
productData 
) [protected]

Set additional data before product saved

Parameters:
Mage_Catalog_Model_Product $product
array $productData
Returns:
object

Reimplemented from Mage_Catalog_Model_Product_Api.

Definition at line 273 of file V2.php.

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     }

create ( type,
set,
sku,
productData 
)

Create new product.

Parameters:
string $type
int $set
array $productData
Returns:
int

Reimplemented from Mage_Catalog_Model_Product_Api.

Definition at line 158 of file V2.php.

00159     {
00160         if (!$type || !$set || !$sku) {
00161             $this->_fault('data_invalid');
00162         }
00163         $store = null;
00164         $product = Mage::getModel('catalog/product');
00165         /* @var $product Mage_Catalog_Model_Product */
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     }

getSpecialPrice ( productId,
store = null 
)

Retrieve product special price

Parameters:
int|string $productId
string|int $store
Returns:
array

Reimplemented from Mage_Catalog_Model_Product_Api.

Definition at line 321 of file V2.php.

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     }

info ( productId,
store = null,
attributes = null 
)

Retrieve product info

Parameters:
int|string $productId
string|int $store
stdClass $attributes
Returns:
array

Reimplemented from Mage_Catalog_Model_Product_Api.

Definition at line 104 of file V2.php.

00105     {
00106         $product = $this->_getProduct($productId, $store);
00107 
00108         if (!$product->getId()) {
00109             $this->_fault('not_exists');
00110         }
00111 
00112         $result = array( // Basic product data
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     }

items ( filters = null,
store = null 
)

Retrieve list of products with basic info (id, sku, type, set, name)

Parameters:
array $filters
string|int $store
Returns:
array

Reimplemented from Mage_Catalog_Model_Product_Api.

Definition at line 44 of file V2.php.

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 //            $result[] = $product->getData();
00083             $result[] = array( // Basic product data
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     }

setSpecialPrice ( productId,
specialPrice = null,
fromDate = null,
toDate = null,
store = null 
)

Update product special price

Parameters:
int|string $productId
float $specialPrice
string $fromDate
string $toDate
string|int $store
Returns:
boolean

Reimplemented from Mage_Catalog_Model_Product_Api.

Definition at line 305 of file V2.php.

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     }

update ( productId,
productData,
store = null 
)

Update product data

Parameters:
int|string $productId
array $productData
string|int $store
Returns:
boolean

Reimplemented from Mage_Catalog_Model_Product_Api.

Definition at line 216 of file V2.php.

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     }


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:23:40 2009 for Magento by  doxygen 1.5.8