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_Attribute_Tierprice_Api extends Mage_Catalog_Model_Api_Resource
35: {
36: const ATTRIBUTE_CODE = 'tier_price';
37:
38: public function __construct()
39: {
40: $this->_storeIdSessionField = 'product_store_id';
41: }
42:
43: public function info($productId, $identifierType = null)
44: {
45: $product = $this->_initProduct($productId, $identifierType);
46: $tierPrices = $product->getData(self::ATTRIBUTE_CODE);
47:
48: if (!is_array($tierPrices)) {
49: return array();
50: }
51:
52: $result = array();
53:
54: foreach ($tierPrices as $tierPrice) {
55: $row = array();
56: $row['customer_group_id'] = (empty($tierPrice['all_groups']) ? $tierPrice['cust_group'] : 'all' );
57: $row['website'] = ($tierPrice['website_id'] ?
58: Mage::app()->getWebsite($tierPrice['website_id'])->getCode() :
59: 'all'
60: );
61: $row['qty'] = $tierPrice['price_qty'];
62: $row['price'] = $tierPrice['price'];
63:
64: $result[] = $row;
65: }
66:
67: return $result;
68: }
69:
70: 71: 72: 73: 74: 75: 76:
77: public function update($productId, $tierPrices, $identifierType = null)
78: {
79: $product = $this->_initProduct($productId, $identifierType);
80:
81: $updatedTierPrices = $this->prepareTierPrices($product, $tierPrices);
82: if (is_null($updatedTierPrices)) {
83: $this->_fault('data_invalid', Mage::helper('catalog')->__('Invalid Tier Prices'));
84: }
85:
86: $product->setData(self::ATTRIBUTE_CODE, $updatedTierPrices);
87: try {
88: 89: 90: 91:
92: if (is_array($errors = $product->validate())) {
93: $strErrors = array();
94: foreach($errors as $code=>$error) {
95: $strErrors[] = ($error === true)? Mage::helper('catalog')->__('Value for "%s" is invalid.', $code) : Mage::helper('catalog')->__('Value for "%s" is invalid: %s', $code, $error);
96: }
97: $this->_fault('data_invalid', implode("\n", $strErrors));
98: }
99:
100: $product->save();
101: } catch (Mage_Core_Exception $e) {
102: $this->_fault('not_updated', $e->getMessage());
103: }
104:
105: return true;
106: }
107:
108: 109: 110: 111: 112: 113: 114:
115: public function prepareTierPrices($product, $tierPrices = null)
116: {
117: if (!is_array($tierPrices)) {
118: return null;
119: }
120:
121: if (!is_array($tierPrices)) {
122: $this->_fault('data_invalid', Mage::helper('catalog')->__('Invalid Tier Prices'));
123: }
124:
125: $updateValue = array();
126:
127: foreach ($tierPrices as $tierPrice) {
128: if (!is_array($tierPrice)
129: || !isset($tierPrice['qty'])
130: || !isset($tierPrice['price'])) {
131: $this->_fault('data_invalid', Mage::helper('catalog')->__('Invalid Tier Prices'));
132: }
133:
134: if (!isset($tierPrice['website']) || $tierPrice['website'] == 'all') {
135: $tierPrice['website'] = 0;
136: } else {
137: try {
138: $tierPrice['website'] = Mage::app()->getWebsite($tierPrice['website'])->getId();
139: } catch (Mage_Core_Exception $e) {
140: $tierPrice['website'] = 0;
141: }
142: }
143:
144: if (intval($tierPrice['website']) > 0 && !in_array($tierPrice['website'], $product->getWebsiteIds())) {
145: $this->_fault('data_invalid', Mage::helper('catalog')->__('Invalid tier prices. The product is not associated to the requested website.'));
146: }
147:
148: if (!isset($tierPrice['customer_group_id'])) {
149: $tierPrice['customer_group_id'] = 'all';
150: }
151:
152: if ($tierPrice['customer_group_id'] == 'all') {
153: $tierPrice['customer_group_id'] = Mage_Customer_Model_Group::CUST_GROUP_ALL;
154: }
155:
156: $updateValue[] = array(
157: 'website_id' => $tierPrice['website'],
158: 'cust_group' => $tierPrice['customer_group_id'],
159: 'price_qty' => $tierPrice['qty'],
160: 'price' => $tierPrice['price']
161: );
162: }
163:
164: return $updateValue;
165: }
166:
167: 168: 169: 170: 171: 172: 173:
174: protected function _initProduct($productId, $identifierType = null)
175: {
176: $product = Mage::helper('catalog/product')->getProduct($productId, $this->_getStoreId(), $identifierType);
177: if (!$product->getId()) {
178: $this->_fault('product_not_exists');
179: }
180:
181: return $product;
182: }
183: }
184: