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: abstract class Mage_Catalog_Model_Api2_Product_Rest extends Mage_Catalog_Model_Api2_Product
35: {
36: 37: 38: 39: 40:
41: protected $_product;
42:
43: 44: 45: 46: 47:
48: protected function _retrieve()
49: {
50: $product = $this->_getProduct();
51:
52: $this->_prepareProductForResponse($product);
53: return $product->getData();
54: }
55:
56: 57: 58: 59: 60:
61: protected function _retrieveCollection()
62: {
63:
64: $collection = Mage::getResourceModel('catalog/product_collection');
65: $store = $this->_getStore();
66: $entityOnlyAttributes = $this->getEntityOnlyAttributes($this->getUserType(),
67: Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_READ);
68: $availableAttributes = array_keys($this->getAvailableAttributes($this->getUserType(),
69: Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_READ));
70:
71: $availableAttributes[] = 'image';
72: $collection->addStoreFilter($store->getId())
73: ->addPriceData($this->_getCustomerGroupId(), $store->getWebsiteId())
74: ->addAttributeToSelect(array_diff($availableAttributes, $entityOnlyAttributes))
75: ->addAttributeToFilter('visibility', array(
76: 'neq' => Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE))
77: ->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED));
78: $this->_applyCategoryFilter($collection);
79: $this->_applyCollectionModifiers($collection);
80: $products = $collection->load();
81:
82:
83: foreach ($products as $product) {
84: $this->_setProduct($product);
85: $this->_prepareProductForResponse($product);
86: }
87: return $products->toArray();
88: }
89:
90: 91: 92: 93: 94:
95: protected function _applyCategoryFilter(Mage_Catalog_Model_Resource_Product_Collection $collection)
96: {
97: $categoryId = $this->getRequest()->getParam('category_id');
98: if ($categoryId) {
99: $category = $this->_getCategoryById($categoryId);
100: if (!$category->getId()) {
101: $this->_critical('Category not found.', Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
102: }
103: $collection->addCategoryFilter($category);
104: }
105: }
106:
107: 108: 109: 110: 111:
112: protected function _prepareProductForResponse(Mage_Catalog_Model_Product $product)
113: {
114:
115: $productHelper = Mage::helper('catalog/product');
116: $productData = $product->getData();
117: $product->setWebsiteId($this->_getStore()->getWebsiteId());
118:
119: $product->setCustomerGroupId($this->_getCustomerGroupId());
120:
121: $finalPrice = $product->getFinalPrice();
122: $productData['regular_price_with_tax'] = $this->_applyTaxToPrice($product->getPrice(), true);
123: $productData['regular_price_without_tax'] = $this->_applyTaxToPrice($product->getPrice(), false);
124: $productData['final_price_with_tax'] = $this->_applyTaxToPrice($finalPrice, true);
125: $productData['final_price_without_tax'] = $this->_applyTaxToPrice($finalPrice, false);
126:
127: $productData['is_saleable'] = $product->getIsSalable();
128: $productData['image_url'] = (string)Mage::helper('catalog/image')->init($product, 'image');
129:
130: if ($this->getActionType() == self::ACTION_TYPE_ENTITY) {
131:
132: $productData['url'] = $productHelper->getProductUrl($product->getId());
133:
134: $cartHelper = Mage::helper('checkout/cart');
135: $productData['buy_now_url'] = $cartHelper->getAddUrl($product);
136:
137:
138: $stockItem = $product->getStockItem();
139: if (!$stockItem) {
140: $stockItem = Mage::getModel('cataloginventory/stock_item');
141: $stockItem->loadByProduct($product);
142: }
143: $productData['is_in_stock'] = $stockItem->getIsInStock();
144:
145:
146: $reviewModel = Mage::getModel('review/review');
147: $productData['total_reviews_count'] = $reviewModel->getTotalReviews($product->getId(), true,
148: $this->_getStore()->getId());
149:
150: $productData['tier_price'] = $this->_getTierPrices();
151: $productData['has_custom_options'] = count($product->getOptions()) > 0;
152: } else {
153:
154: $product->unsetData('tier_price');
155: unset($productData['tier_price']);
156: }
157: $product->addData($productData);
158: }
159:
160: 161: 162: 163: 164:
165: protected function _create(array $data)
166: {
167: $this->_critical(self::RESOURCE_METHOD_NOT_ALLOWED);
168: }
169:
170: 171: 172: 173: 174:
175: protected function _update(array $data)
176: {
177: $this->_critical(self::RESOURCE_METHOD_NOT_ALLOWED);
178: }
179:
180: 181: 182:
183: protected function _delete()
184: {
185: $this->_critical(self::RESOURCE_METHOD_NOT_ALLOWED);
186: }
187:
188: 189: 190: 191: 192:
193: protected function _getProduct()
194: {
195: if (is_null($this->_product)) {
196: $productId = $this->getRequest()->getParam('id');
197:
198: $productHelper = Mage::helper('catalog/product');
199: $product = $productHelper->getProduct($productId, $this->_getStore()->getId());
200: if (!($product->getId())) {
201: $this->_critical(self::RESOURCE_NOT_FOUND);
202: }
203:
204: if ($this->_getStore()->getId()) {
205: $isValidWebsite = in_array($this->_getStore()->getWebsiteId(), $product->getWebsiteIds());
206: if (!$isValidWebsite) {
207: $this->_critical(self::RESOURCE_NOT_FOUND);
208: }
209: }
210:
211: if ($this->getApiUser()->getType() != Mage_Api2_Model_Auth_User_Admin::USER_TYPE) {
212:
213: if ((!Mage::app()->isSingleStoreMode() && !count($product->getWebsiteIds()))
214: || !$productHelper->canShow($product)
215: ) {
216: $this->_critical(self::RESOURCE_NOT_FOUND);
217: }
218: }
219: $this->_product = $product;
220: }
221: return $this->_product;
222: }
223:
224: 225: 226: 227: 228:
229: protected function _setProduct(Mage_Catalog_Model_Product $product)
230: {
231: $this->_product = $product;
232: }
233:
234: 235: 236: 237: 238: 239:
240: protected function _getCategoryById($categoryId)
241: {
242: return Mage::getModel('catalog/category')->load($categoryId);
243: }
244:
245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256:
257: protected function _getPrice($price, $includingTax = null, $shippingAddress = null,
258: $billingAddress = null, $ctc = null, $priceIncludesTax = null
259: )
260: {
261: $product = $this->_getProduct();
262: $store = $this->_getStore();
263:
264: if (is_null($priceIncludesTax)) {
265:
266: $config = Mage::getSingleton('tax/config');
267: $priceIncludesTax = $config->priceIncludesTax($store) || $config->getNeedUseShippingExcludeTax();
268: }
269:
270: $percent = $product->getTaxPercent();
271: $includingPercent = null;
272:
273: $taxClassId = $product->getTaxClassId();
274: if (is_null($percent)) {
275: if ($taxClassId) {
276: $request = Mage::getSingleton('tax/calculation')
277: ->getRateRequest($shippingAddress, $billingAddress, $ctc, $store);
278: $percent = Mage::getSingleton('tax/calculation')->getRate($request->setProductClassId($taxClassId));
279: }
280: }
281: if ($taxClassId && $priceIncludesTax) {
282: $request = Mage::getSingleton('tax/calculation')->getRateRequest(false, false, false, $store);
283: $includingPercent = Mage::getSingleton('tax/calculation')
284: ->getRate($request->setProductClassId($taxClassId));
285: }
286:
287: if ($percent === false || is_null($percent)) {
288: if ($priceIncludesTax && !$includingPercent) {
289: return $price;
290: }
291: }
292: $product->setTaxPercent($percent);
293:
294: if (!is_null($includingTax)) {
295: if ($priceIncludesTax) {
296: if ($includingTax) {
297: 298: 299:
300: if ($includingPercent != $percent) {
301: $price = $this->_calculatePrice($price, $includingPercent, false);
302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314:
315: if ($percent != 0) {
316: $price = Mage::getSingleton('tax/calculation')->round($price);
317: $price = $this->_calculatePrice($price, $percent, true);
318: }
319: }
320: } else {
321: $price = $this->_calculatePrice($price, $includingPercent, false);
322: }
323: } else {
324: if ($includingTax) {
325: $price = $this->_calculatePrice($price, $percent, true);
326: }
327: }
328: } else {
329: if ($priceIncludesTax) {
330: if ($includingTax) {
331: $price = $this->_calculatePrice($price, $includingPercent, false);
332: $price = $this->_calculatePrice($price, $percent, true);
333: } else {
334: $price = $this->_calculatePrice($price, $includingPercent, false);
335: }
336: } else {
337: if ($includingTax) {
338: $price = $this->_calculatePrice($price, $percent, true);
339: }
340: }
341: }
342:
343: return $store->roundPrice($price);
344: }
345:
346: 347: 348: 349: 350: 351: 352: 353:
354: protected function _calculatePrice($price, $percent, $includeTax)
355: {
356:
357: $calculator = Mage::getSingleton('tax/calculation');
358: $taxAmount = $calculator->calcTaxAmount($price, $percent, !$includeTax, false);
359:
360: return $includeTax ? $price + $taxAmount : $price - $taxAmount;
361: }
362:
363: 364: 365: 366: 367:
368: protected function _getTierPrices()
369: {
370: $tierPrices = array();
371: foreach ($this->_getProduct()->getTierPrice() as $tierPrice) {
372: $tierPrices[] = array(
373: 'qty' => $tierPrice['price_qty'],
374: 'price_with_tax' => $this->_applyTaxToPrice($tierPrice['price']),
375: 'price_without_tax' => $this->_applyTaxToPrice($tierPrice['price'], false)
376: );
377: }
378: return $tierPrices;
379: }
380:
381: 382: 383: 384: 385:
386: protected function _getCustomerGroupId()
387: {
388: return null;
389: }
390:
391: 392: 393: 394: 395: 396: 397:
398: protected function _applyTaxToPrice($price, $withTax = true)
399: {
400: return $price;
401: }
402: }
403: