PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/app/code/local/Atipso/Atipapi/Model/Product/Api.php

https://bitbucket.org/atipso/atipso-magento-extension
PHP | 275 lines | 154 code | 33 blank | 88 comment | 14 complexity | 80c0d725f9480d399b9520c1dab35b14 MD5 | raw file
  1. <?php
  2. /**
  3. * Catalog product api - overrides Mage_Catalog_Model_Product_Api
  4. *
  5. * @category Atipso
  6. * @package Atipso_Atipapi
  7. * @author Peter Arvenom <peter.arvenom@gmail.com>
  8. */
  9. class Atipso_Atipapi_Model_Product_Api extends Mage_Catalog_Model_Product_Api
  10. {
  11. /**
  12. * Retrieve product info with options
  13. *
  14. * @param int|string $productId
  15. * @param string|int $store
  16. * @param array $attributes
  17. * @return array
  18. */
  19. public function infoEnhanced($productId, $store = null, $attributes = null, $identifierType = null)
  20. {
  21. $product = $this->_getProduct($productId, $store, $identifierType);
  22. if (!$product->getId()) {
  23. $this->_fault('not_exists');
  24. }
  25. // // Set tax percent (its always percent right ? right ?)
  26. // // Inspired by Magento's app\code\core\Mage\Tax\Model\Observer.php.
  27. // // Please note that this value must be manually added into the 'result'
  28. // // array as Magento considers it as a non-allowed attribute.
  29. if (null === $product->getTaxClassId()) {
  30. $product->setTaxClassId($product->getMinimalTaxClassId());
  31. }
  32. if (!isset($classToRate[$product->getTaxClassId()])) {
  33. $request = Mage::getSingleton('tax/calculation')->getRateRequest();
  34. $request->setProductClassId($product->getTaxClassId());
  35. $classToRate[$product->getTaxClassId()] = Mage::getSingleton('tax/calculation')->getRate($request);
  36. }
  37. $product->setTaxPercent($classToRate[$product->getTaxClassId()]);
  38. // // START : tests
  39. // // The following applies to configurable products such as
  40. // // Coalesce: Functioning On Impatience T-Shirt (Shirts, page 1)
  41. // // Get configurable type product instance and get configurable attributes collection.
  42. // // Sample uri to add a configurable product via query string:
  43. // // http://www.your_domain.com/checkout/cart/add?product=68&qty=1&super_attribute[528]=55&super_attribute[525]=56
  44. $configurable_attributes = array();
  45. $used_products = array();
  46. if (method_exists($product->getTypeInstance(), 'getConfigurableAttributes') == true) {
  47. $configurableAttributeCollection=$product->getTypeInstance()->getConfigurableAttributes();
  48. // // Use the collection to get the designated values of each configurable attribute
  49. $index = 0;
  50. foreach($configurableAttributeCollection as $attribute){
  51. // error_log( 'attribute['. $index . '] print_r:['.print_r($attribute->getProductAttribute(), true).']' );
  52. $productAttribute = $attribute->getProductAttribute();
  53. $att = array(
  54. 'id' => $productAttribute->getId(),
  55. 'code' => $productAttribute->getAttributeCode(),
  56. // // Magento sets this to 1 even for non required attributes. See the store detail view for
  57. // // 'Zolof The Rock And Roll Destroyer: LOL Cat T-shirt' from the Magento sample database.
  58. 'required' => 1,
  59. // 'required' => $attribute->getIsRequired(), // KO. Returns empty
  60. // 'required' => $productAttribute->getIsRequire(), // KO. Returns empty
  61. // 'required' => $productAttribute->getRequired(), // KO. Returns empty
  62. // 'required' => $productAttribute->getData('required'), // KO. Returns empty
  63. // 'required' => $productAttribute->getData('is_required'), // Does not match those at the store 100%
  64. 'get_is_required' => $productAttribute->getIsRequired(), // Does not match those at the store 100%
  65. 'label' => $productAttribute->getFrontend()->getLabel(),
  66. // // Next one from function 'getConfigurableAttributesAsArray' in app\code\core\Mage\Catalog\Model\Product\Type\Configurable.php
  67. 'options' => $attribute->getPrices()
  68. // 'options' => $productAttribute->getSource()->getAllOptions() // OK but does not match store
  69. // 'options' => $productAttribute->getSource()->getSelectableOptions() // KO
  70. // 'options' => $productAttribute->getSource()->getOptions() // KO
  71. // 'options' => $productAttribute->getSource()->getVisibleOptions() // KO
  72. );
  73. array_push($configurable_attributes, $att);
  74. $index++;
  75. }
  76. // // START : used products ?
  77. // // The next one returns the id of the used products
  78. $usedProductIds = $product->getTypeInstance(true)->getUsedProductIds($product);
  79. $usedProductCollection = $product->getCollection()
  80. // ->addAttributeToSelect('name')
  81. ->addAttributeToSelect('sku')
  82. // ->addAttributeToSelect('attribute_set_id')
  83. // ->addAttributeToSelect('type_id')
  84. // ->addAttributeToSelect('price')
  85. ->addFieldToFilter('attribute_set_id',$product->getAttributeSetId())
  86. ->addFieldToFilter('entity_id', array('in' => $usedProductIds ))
  87. // ->addFieldToFilter('type_id', $allowProductTypes)
  88. ->addFilterByRequiredOptions();
  89. // // The next line includes inventory count
  90. // Mage::getModel('cataloginventory/stock_item')->addCatalogInventoryToProductCollection($associatedProductCollection);
  91. // // The next block includes the super attribute i.e. size & color
  92. foreach ($product->getTypeInstance(true)->getUsedProductAttributes($product) as $attribute) {
  93. $usedProductCollection->addAttributeToSelect($attribute->getAttributeCode());
  94. $usedProductCollection->addAttributeToFilter($attribute->getAttributeCode(), array('nin'=>array(null)));
  95. }
  96. $used_products = array();
  97. foreach ($usedProductCollection as $rp) {
  98. $rpraw = $rp->getData();
  99. // // Remove fields we don't want which we're unable to filter when fetching the collection.
  100. unset($rpraw['stock_item']);
  101. unset($rpraw['created_at']);
  102. unset($rpraw['updated_at']);
  103. unset($rpraw['has_options']);
  104. unset($rpraw['required_options']);
  105. unset($rpraw['entity_type_id']);
  106. unset($rpraw['attribute_set_id']);
  107. // unset($rpraw['type_id']);
  108. array_push($used_products,
  109. // $rp->getData()
  110. $rpraw
  111. );
  112. }
  113. // // END : used products ?
  114. }
  115. // // The following applies to simple products with
  116. // // custom options such as (page 8 under 'Computers'):
  117. // // Apple MacBook Pro MA464LL/A 15.4" Notebook PC (i.e. product_id 25)
  118. $custom_options = array();
  119. // if (method_exists($product, 'getAllOptions') == true) {
  120. // $options = $product->getAllOptions();
  121. if (method_exists($product, 'getOptions') == true) {
  122. $options = $product->getOptions();
  123. if (count($options) > 0) {
  124. error_log( 'info : [' . $product->getName() . '] of type [' . $product->getTypeId() . '] has [' . count($options) . '] options.' );
  125. }
  126. $index = 0;
  127. foreach ($options as $o) {
  128. // error_log('o = [' . print_r($o, true) . ']');
  129. $ov = array();
  130. $values = $o->getValues();
  131. foreach ($values as $v) {
  132. // // The next line has 'option_id', 'title', 'default_price' etc
  133. array_push($ov, $v->getData());
  134. }
  135. $att = array(
  136. 'id' => $o->getId(),
  137. 'type' => $o->getType(),
  138. 'label' => $o->getTitle(),
  139. // 'required' => $o->getRequired(),
  140. // 'required' => $o->getData('required'),
  141. // 'required' => $o->getData('is_required'),
  142. // 'required' => $o->getIsRequired(),
  143. 'required' => $o->getIsRequire(),
  144. // 'options' => array()
  145. 'options' => $ov
  146. );
  147. array_push($custom_options, $att);
  148. $index++;
  149. }
  150. }
  151. // // The following applies to bundle products
  152. // // Sony VAIO 11.1" Notebook PC (psge 1 under 'Computers')
  153. // // Never realized how complicated it is to get bundle options until now.
  154. if (method_exists($product->getTypeInstance(true), 'getSelectionsCollection') == true) {
  155. // // From app\code\core\Mage\Bundle\Model\Observer.php
  156. $product->getTypeInstance(true)->setStoreFilter($product->getStoreId(), $product);
  157. $optionCollection = $product->getTypeInstance(true)->getOptionsCollection($product);
  158. $selectionCollection = $product->getTypeInstance(true)->getSelectionsCollection(
  159. $product->getTypeInstance(true)->getOptionsIds($product),
  160. $product
  161. );
  162. $optionCollection->appendSelections($selectionCollection);
  163. $bundle_options = array();
  164. // $selectionRawData = array();
  165. $i = 0;
  166. foreach ($optionCollection as $option) {
  167. // error_log('option = [' . print_r($option, true) . ']');
  168. $tmpSelectionData = array();
  169. foreach ($option->getSelections() as $selection) {
  170. // error_log('selection = [' . print_r($selection, true) . ']');
  171. // $selectionRawData[$i][] = array(
  172. $tmpSelectionData[] = array(
  173. // 'option_id' => $selection->getOptionId(),
  174. // 'option_id' => $selection->getOption()->getId(),
  175. // 'option_label' => $selection->getOption()->getTitle(),
  176. 'name' => $selection->getName(),
  177. 'selection_id' => $selection->getSelectionId(),
  178. 'id' => $selection->getId(),
  179. 'product_id' => $selection->getProductId(),
  180. // 'price' => $selection->getSelectionPrice(), // KO
  181. // 'price' => $selection->getData('price'), // OK
  182. 'price' => $selection->getPrice(),
  183. 'position' => $selection->getPosition(),
  184. 'is_default' => $selection->getIsDefault(),
  185. 'selection_price_type' => $selection->getSelectionPriceType(),
  186. 'selection_price_value' => $selection->getSelectionPriceValue(),
  187. 'selection_qty' => $selection->getSelectionQty(),
  188. 'selection_can_change_qty' => $selection->getSelectionCanChangeQty(),
  189. 'delete' => ''
  190. );
  191. }
  192. $bundle_options[$i] = array(
  193. 'id' => $option->getData('option_id'),
  194. 'required' => $option->getData('required'),
  195. 'position' => $option->getData('position'),
  196. 'type' => $option->getData('type'),
  197. 'title' => $option->getData('title')?$option->getData('title'):$option->getData('default_title'),
  198. 'delete' => '',
  199. 'options' => $tmpSelectionData
  200. );
  201. $i++;
  202. }
  203. // $newProduct->setBundleOptionsData($bundle_options);
  204. // $newProduct->setBundleSelectionsData($selectionRawData);
  205. }
  206. // // END : tests
  207. $result = array( // Basic product data
  208. 'product_id' => $product->getId(),
  209. 'sku' => $product->getSku(),
  210. 'set' => $product->getAttributeSetId(),
  211. 'type' => $product->getTypeId(),
  212. 'categories' => $product->getCategoryIds(),
  213. // 'websites' => $product->getWebsiteIds(),
  214. // // The next one is required for display but strangely is skipped by the foreach loop below.
  215. 'tax_percent' => $product->getTaxPercent(),
  216. 'price_includes_tax' => Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_PRICE_INCLUDES_TAX, $store),
  217. 'product_collection' => $filteredProducts,
  218. 'currency_symbol' => Mage::app()->getLocale()->currency(Mage::app()->getStore($store)->getCurrentCurrencyCode())->getSymbol()
  219. );
  220. foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
  221. if ($this->_isAllowedAttribute($attribute, $attributes)) {
  222. $result[$attribute->getAttributeCode()] = $product->getData($attribute->getAttributeCode());
  223. }
  224. }
  225. // // START : tests
  226. // // Add configurable attributes here.
  227. if (!empty($configurable_attributes)) {
  228. $result['configurable_attributes'] = $configurable_attributes;
  229. $result['used_products'] = $used_products;
  230. }
  231. // // Add custom options here.
  232. if (!empty($custom_options)) {
  233. $result['custom_options'] = $custom_options;
  234. }
  235. // // Add bundle options here.
  236. if (!empty($bundle_options)) {
  237. $result['bundle_options'] = $bundle_options;
  238. }
  239. // // END : tests
  240. return $result;
  241. }
  242. } // Class Atipso_Catalog_Model_Product_Api End