PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Model/Product/Api/V2.php

https://bitbucket.org/jokusafet/magento2
PHP | 319 lines | 192 code | 33 blank | 94 comment | 34 complexity | b0003dfb07a737002647854a0366e6b3 MD5 | raw file
  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_Catalog
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Catalog product api V2
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Product_Api_V2 extends Mage_Catalog_Model_Product_Api
  34. {
  35. /**
  36. * Retrieve product info
  37. *
  38. * @param int|string $productId
  39. * @param string|int $store
  40. * @param stdClass $attributes
  41. * @return array
  42. */
  43. public function info($productId, $store = null, $attributes = null, $identifierType = null)
  44. {
  45. $product = $this->_getProduct($productId, $store, $identifierType);
  46. $result = array( // Basic product data
  47. 'product_id' => $product->getId(),
  48. 'sku' => $product->getSku(),
  49. 'set' => $product->getAttributeSetId(),
  50. 'type' => $product->getTypeId(),
  51. 'categories' => $product->getCategoryIds(),
  52. 'websites' => $product->getWebsiteIds()
  53. );
  54. $allAttributes = array();
  55. if (!empty($attributes->attributes)) {
  56. $allAttributes = array_merge($allAttributes, $attributes->attributes);
  57. } else {
  58. foreach ($product->getTypeInstance()->getEditableAttributes($product) as $attribute) {
  59. if ($this->_isAllowedAttribute($attribute, $attributes)) {
  60. $allAttributes[] = $attribute->getAttributeCode();
  61. }
  62. }
  63. }
  64. $_additionalAttributeCodes = array();
  65. if (!empty($attributes->additional_attributes)) {
  66. foreach ($attributes->additional_attributes as $k => $_attributeCode) {
  67. $allAttributes[] = $_attributeCode;
  68. $_additionalAttributeCodes[] = $_attributeCode;
  69. }
  70. }
  71. $_additionalAttribute = 0;
  72. foreach ($product->getTypeInstance()->getEditableAttributes($product) as $attribute) {
  73. if ($this->_isAllowedAttribute($attribute, $allAttributes)) {
  74. if (in_array($attribute->getAttributeCode(), $_additionalAttributeCodes)) {
  75. $result['additional_attributes'][$_additionalAttribute]['key'] = $attribute->getAttributeCode();
  76. $result['additional_attributes'][$_additionalAttribute]['value'] = $product
  77. ->getData($attribute->getAttributeCode());
  78. $_additionalAttribute++;
  79. } else {
  80. $result[$attribute->getAttributeCode()] = $product->getData($attribute->getAttributeCode());
  81. }
  82. }
  83. }
  84. return $result;
  85. }
  86. /**
  87. * Create new product.
  88. *
  89. * @param string $type
  90. * @param int $set
  91. * @param string $sku
  92. * @param array $productData
  93. * @param string $store
  94. * @return int
  95. */
  96. public function create($type, $set, $sku, $productData, $store = null)
  97. {
  98. if (!$type || !$set || !$sku) {
  99. $this->_fault('data_invalid');
  100. }
  101. $this->_checkProductTypeExists($type);
  102. $this->_checkProductAttributeSet($set);
  103. /** @var $product Mage_Catalog_Model_Product */
  104. $product = Mage::getModel('Mage_Catalog_Model_Product');
  105. $product->setStoreId($this->_getStoreId($store))
  106. ->setAttributeSetId($set)
  107. ->setTypeId($type)
  108. ->setSku($sku);
  109. if (!property_exists($productData, 'stock_data')) {
  110. //Set default stock_data if not exist in product data
  111. $_stockData = array('use_config_manage_stock' => 0);
  112. $product->setStockData($_stockData);
  113. }
  114. foreach ($product->getMediaAttributes() as $mediaAttribute) {
  115. $mediaAttrCode = $mediaAttribute->getAttributeCode();
  116. $product->setData($mediaAttrCode, 'no_selection');
  117. }
  118. $this->_prepareDataForSave($product, $productData);
  119. try {
  120. /**
  121. * @todo implement full validation process with errors returning which are ignoring now
  122. * @todo see Mage_Catalog_Model_Product::validate()
  123. */
  124. if (is_array($errors = $product->validate())) {
  125. $strErrors = array();
  126. foreach($errors as $code => $error) {
  127. if ($error === true) {
  128. $error = Mage::helper('Mage_Catalog_Helper_Data')->__('Attribute "%s" is invalid.', $code);
  129. }
  130. $strErrors[] = $error;
  131. }
  132. $this->_fault('data_invalid', implode("\n", $strErrors));
  133. }
  134. $product->save();
  135. } catch (Mage_Core_Exception $e) {
  136. $this->_fault('data_invalid', $e->getMessage());
  137. }
  138. return $product->getId();
  139. }
  140. /**
  141. * Update product data
  142. *
  143. * @param int|string $productId
  144. * @param array $productData
  145. * @param string|int $store
  146. * @return boolean
  147. */
  148. public function update($productId, $productData, $store = null, $identifierType = null)
  149. {
  150. $product = $this->_getProduct($productId, $store, $identifierType);
  151. $this->_prepareDataForSave($product, $productData);
  152. try {
  153. /**
  154. * @todo implement full validation process with errors returning which are ignoring now
  155. * @todo see Mage_Catalog_Model_Product::validate()
  156. */
  157. if (is_array($errors = $product->validate())) {
  158. $strErrors = array();
  159. foreach($errors as $code => $error) {
  160. if ($error === true) {
  161. $error = Mage::helper('Mage_Catalog_Helper_Data')->__('Value for "%s" is invalid.', $code);
  162. } else {
  163. $error = Mage::helper('Mage_Catalog_Helper_Data')->__('Value for "%s" is invalid: %s', $code, $error);
  164. }
  165. $strErrors[] = $error;
  166. }
  167. $this->_fault('data_invalid', implode("\n", $strErrors));
  168. }
  169. $product->save();
  170. } catch (Mage_Core_Exception $e) {
  171. $this->_fault('data_invalid', $e->getMessage());
  172. }
  173. return true;
  174. }
  175. /**
  176. * Set additional data before product saved
  177. *
  178. * @param Mage_Catalog_Model_Product $product
  179. * @param array $productData
  180. * @return object
  181. */
  182. protected function _prepareDataForSave ($product, $productData)
  183. {
  184. if (property_exists($productData, 'website_ids') && is_array($productData->website_ids)) {
  185. $product->setWebsiteIds($productData->website_ids);
  186. }
  187. if (property_exists($productData, 'additional_attributes')) {
  188. if (property_exists($productData->additional_attributes, 'single_data')) {
  189. foreach ($productData->additional_attributes->single_data as $_attribute) {
  190. $_attrCode = $_attribute->key;
  191. $productData->$_attrCode = $_attribute->value;
  192. }
  193. }
  194. if (property_exists($productData->additional_attributes, 'multi_data')) {
  195. foreach ($productData->additional_attributes->multi_data as $_attribute) {
  196. $_attrCode = $_attribute->key;
  197. $productData->$_attrCode = $_attribute->value;
  198. }
  199. }
  200. unset($productData->additional_attributes);
  201. }
  202. foreach ($product->getTypeInstance()->getEditableAttributes($product) as $attribute) {
  203. $_attrCode = $attribute->getAttributeCode();
  204. //Unset data if object attribute has no value in current store
  205. if (Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID !== (int) $product->getStoreId()
  206. && !$product->getExistsStoreValueFlag($_attrCode)
  207. && !$attribute->isScopeGlobal()
  208. ) {
  209. $product->setData($_attrCode, false);
  210. }
  211. if ($this->_isAllowedAttribute($attribute) && (isset($productData->$_attrCode))) {
  212. $product->setData(
  213. $_attrCode,
  214. $productData->$_attrCode
  215. );
  216. }
  217. }
  218. if (property_exists($productData, 'categories') && is_array($productData->categories)) {
  219. $product->setCategoryIds($productData->categories);
  220. }
  221. if (property_exists($productData, 'websites') && is_array($productData->websites)) {
  222. foreach ($productData->websites as &$website) {
  223. if (is_string($website)) {
  224. try {
  225. $website = Mage::app()->getWebsite($website)->getId();
  226. } catch (Exception $e) { }
  227. }
  228. }
  229. $product->setWebsiteIds($productData->websites);
  230. }
  231. if (Mage::app()->hasSingleStore()) {
  232. $product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
  233. }
  234. if (property_exists($productData, 'stock_data')) {
  235. $_stockData = array();
  236. foreach ($productData->stock_data as $key => $value) {
  237. $_stockData[$key] = $value;
  238. }
  239. $product->setStockData($_stockData);
  240. }
  241. if (property_exists($productData, 'tier_price')) {
  242. $tierPrices = Mage::getModel('Mage_Catalog_Model_Product_Attribute_Tierprice_Api_V2')
  243. ->prepareTierPrices($product, $productData->tier_price);
  244. $product->setData(Mage_Catalog_Model_Product_Attribute_Tierprice_Api_V2::ATTRIBUTE_CODE, $tierPrices);
  245. }
  246. }
  247. /**
  248. * Update product special price
  249. *
  250. * @param int|string $productId
  251. * @param float $specialPrice
  252. * @param string $fromDate
  253. * @param string $toDate
  254. * @param string|int $store
  255. * @param string $identifierType OPTIONAL If 'sku' - search product by SKU, if any except for NULL - search by ID,
  256. * otherwise - try to determine identifier type automatically
  257. * @return boolean
  258. */
  259. public function setSpecialPrice($productId, $specialPrice = null, $fromDate = null, $toDate = null, $store = null,
  260. $identifierType = null
  261. ) {
  262. $obj = new stdClass();
  263. $obj->special_price = $specialPrice;
  264. $obj->special_from_date = $fromDate;
  265. $obj->special_to_date = $toDate;
  266. return $this->update($productId, $obj, $store, $identifierType);
  267. }
  268. /**
  269. * Retrieve product special price
  270. *
  271. * @param int|string $productId
  272. * @param string|int $store
  273. * @return array
  274. */
  275. public function getSpecialPrice($productId, $store = null)
  276. {
  277. return $this->info($productId, $store, array(
  278. 'attributes' => array(
  279. 'special_price',
  280. 'special_from_date',
  281. 'special_to_date'
  282. )
  283. )
  284. );
  285. }
  286. }