1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Checkout
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * API Resource class for product
29: */
30: class Mage_Checkout_Model_Api_Resource_Product extends Mage_Checkout_Model_Api_Resource
31: {
32: /**
33: * Default ignored attribute codes
34: *
35: * @var array
36: */
37: protected $_ignoredAttributeCodes = array('entity_id', 'attribute_set_id', 'entity_type_id');
38:
39: /**
40: * Return loaded product instance
41: *
42: * @param int|string $productId (SKU or ID)
43: * @param int|string $store
44: * @param string $identifierType
45: * @return Mage_Catalog_Model_Product
46: */
47: protected function _getProduct($productId, $store = null, $identifierType = null)
48: {
49: $product = Mage::helper('catalog/product')->getProduct($productId,
50: $this->_getStoreId($store),
51: $identifierType
52: );
53: return $product;
54: }
55:
56: /**
57: * Get request for product add to cart procedure
58: *
59: * @param mixed $requestInfo
60: * @return Varien_Object
61: */
62: protected function _getProductRequest($requestInfo)
63: {
64: if ($requestInfo instanceof Varien_Object) {
65: $request = $requestInfo;
66: } elseif (is_numeric($requestInfo)) {
67: $request = new Varien_Object();
68: $request->setQty($requestInfo);
69: } else {
70: $request = new Varien_Object($requestInfo);
71: }
72:
73: if (!$request->hasQty()) {
74: $request->setQty(1);
75: }
76: return $request;
77: }
78:
79: /**
80: * Get QuoteItem by Product and request info
81: *
82: * @param Mage_Sales_Model_Quote $quote
83: * @param Mage_Catalog_Model_Product $product
84: * @param Varien_Object $requestInfo
85: * @return Mage_Sales_Model_Quote_Item
86: * @throw Mage_Core_Exception
87: */
88: protected function _getQuoteItemByProduct(Mage_Sales_Model_Quote $quote,
89: Mage_Catalog_Model_Product $product,
90: Varien_Object $requestInfo)
91: {
92: $cartCandidates = $product->getTypeInstance(true)
93: ->prepareForCartAdvanced($requestInfo,
94: $product,
95: Mage_Catalog_Model_Product_Type_Abstract::PROCESS_MODE_FULL
96: );
97:
98: /**
99: * Error message
100: */
101: if (is_string($cartCandidates)) {
102: throw Mage::throwException($cartCandidates);
103: }
104:
105: /**
106: * If prepare process return one object
107: */
108: if (!is_array($cartCandidates)) {
109: $cartCandidates = array($cartCandidates);
110: }
111:
112: /** @var $item Mage_Sales_Model_Quote_Item */
113: $item = null;
114: foreach ($cartCandidates as $candidate) {
115: if ($candidate->getParentProductId()) {
116: continue;
117: }
118:
119: $item = $quote->getItemByProduct($candidate);
120: }
121:
122: if (is_null($item)) {
123: $item = Mage::getModel("sales/quote_item");
124: }
125:
126: return $item;
127: }
128: }
129: