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_Attribute_Tierprice_Api extends Mage_Catalog_Model_Api_Resource
00035 {
00036 const ATTRIBUTE_CODE = 'tier_price';
00037
00038 public function __construct()
00039 {
00040 $this->_storeIdSessionField = 'product_store_id';
00041 }
00042
00043 public function info($productId)
00044 {
00045 $product = $this->_initProduct($productId);
00046 $tierPrices = $product->getData(self::ATTRIBUTE_CODE);
00047
00048 if (!is_array($tierPrices)) {
00049 return array();
00050 }
00051
00052 $result = array();
00053
00054 foreach ($tierPrices as $tierPrice) {
00055 $row = array();
00056 $row['customer_group_id'] = (empty($tierPrice['all_groups']) ? $tierPrice['cust_group'] : 'all' );
00057 $row['website'] = ($tierPrice['website_id'] ? Mage::app()->getWebsite($tierPrice['website_id'])->getCode() : 'all');
00058 $row['qty'] = $tierPrice['price_qty'];
00059 $row['price'] = $tierPrice['price'];
00060
00061 $result[] = $row;
00062 }
00063
00064 return $result;
00065 }
00066
00067 public function update($productId, $tierPrices)
00068 {
00069 $product = $this->_initProduct($productId);
00070 if (!is_array($tierPrices)) {
00071 $this->_fault('data_invalid', Mage::helper('catalog')->__('Invalid Tier Prices'));
00072 }
00073
00074 $updateValue = array();
00075
00076 foreach ($tierPrices as $tierPrice) {
00077 if (!is_array($tierPrice)
00078 || !isset($tierPrice['qty'])
00079 || !isset($tierPrice['price'])) {
00080 $this->_fault('data_invalid', Mage::helper('catalog')->__('Invalid Tier Prices'));
00081 }
00082
00083 if (!isset($tierPrice['website']) || $tierPrice['website'] == 'all') {
00084 $tierPrice['website'] = 0;
00085 } else {
00086 try {
00087 $tierPrice['website'] = Mage::app()->getWebsite($tierPrice['website'])->getId();
00088 } catch (Mage_Core_Exception $e) {
00089 $tierPrice['website'] = 0;
00090 }
00091 }
00092
00093 if (!isset($tierPrice['customer_group_id'])) {
00094 $tierPrice['customer_group_id'] = 'all';
00095 }
00096
00097 if ($tierPrice['customer_group_id'] == 'all') {
00098 $tierPrice['customer_group_id'] = Mage_Customer_Model_Group::CUST_GROUP_ALL;
00099 }
00100
00101 $updateValue[] = array(
00102 'website_id' => $tierPrice['website'],
00103 'cust_group' => $tierPrice['customer_group_id'],
00104 'price_qty' => $tierPrice['qty'],
00105 'price' => $tierPrice['price']
00106 );
00107
00108 }
00109
00110
00111 try {
00112 if (is_array($errors = $product->validate())) {
00113 $this->_fault('data_invalid', implode("\n", $errors));
00114 }
00115 } catch (Mage_Core_Exception $e) {
00116 $this->_fault('data_invalid', $e->getMessage());
00117 }
00118
00119 try {
00120 $product->setData(self::ATTRIBUTE_CODE ,$updateValue);
00121 $product->validate();
00122 $product->save();
00123 } catch (Mage_Core_Exception $e) {
00124 $this->_fault('not_updated', $e->getMessage());
00125 }
00126
00127 return true;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137 protected function _initProduct($productId)
00138 {
00139 $product = Mage::getModel('catalog/product')
00140 ->setStoreId($this->_getStoreId());
00141
00142 $idBySku = $product->getIdBySku($productId);
00143 if ($idBySku) {
00144 $productId = $idBySku;
00145 }
00146
00147 $product->load($productId);
00148
00149
00150
00151 if (!$product->getId()) {
00152 $this->_fault('product_not_exists');
00153 }
00154
00155 return $product;
00156 }
00157 }