PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Model/Resource/Product/Type/Configurable.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 227 lines | 145 code | 16 blank | 66 comment | 7 complexity | cd48973bc8b1b7484eea3be93713cb69 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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Catalog
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Configurable product type resource model
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Resource_Product_Type_Configurable extends Mage_Core_Model_Resource_Db_Abstract
  34. {
  35. /**
  36. * Init resource
  37. *
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init('catalog/product_super_link', 'link_id');
  42. }
  43. /**
  44. * Save configurable product relations
  45. *
  46. * @param Mage_Catalog_Model_Product|int $mainProduct the parent id
  47. * @param array $productIds the children id array
  48. * @return Mage_Catalog_Model_Resource_Product_Type_Configurable
  49. */
  50. public function saveProducts($mainProduct, $productIds)
  51. {
  52. $isProductInstance = false;
  53. if ($mainProduct instanceof Mage_Catalog_Model_Product) {
  54. $mainProductId = $mainProduct->getId();
  55. $isProductInstance = true;
  56. } else {
  57. $mainProductId = $mainProduct;
  58. }
  59. $old = $mainProduct->getTypeInstance()->getUsedProductIds();
  60. $insert = array_diff($productIds, $old);
  61. $delete = array_diff($old, $productIds);
  62. if ((!empty($insert) || !empty($delete)) && $isProductInstance) {
  63. $mainProduct->setIsRelationsChanged(true);
  64. }
  65. if (!empty($delete)) {
  66. $where = array(
  67. 'parent_id = ?' => $mainProductId,
  68. 'product_id IN(?)' => $delete
  69. );
  70. $this->_getWriteAdapter()->delete($this->getMainTable(), $where);
  71. }
  72. if (!empty($insert)) {
  73. $data = array();
  74. foreach ($insert as $childId) {
  75. $data[] = array(
  76. 'product_id' => (int)$childId,
  77. 'parent_id' => (int)$mainProductId
  78. );
  79. }
  80. $this->_getWriteAdapter()->insertMultiple($this->getMainTable(), $data);
  81. }
  82. // configurable product relations should be added to relation table
  83. Mage::getResourceSingleton('catalog/product_relation')
  84. ->processRelations($mainProductId, $productIds);
  85. return $this;
  86. }
  87. /**
  88. * Retrieve Required children ids
  89. * Return grouped array, ex array(
  90. * group => array(ids)
  91. * )
  92. *
  93. * @param int $parentId
  94. * @param bool $required
  95. * @return array
  96. */
  97. public function getChildrenIds($parentId, $required = true)
  98. {
  99. $childrenIds = array();
  100. $select = $this->_getReadAdapter()->select()
  101. ->from(array('l' => $this->getMainTable()), array('product_id', 'parent_id'))
  102. ->join(
  103. array('e' => $this->getTable('catalog/product')),
  104. 'e.entity_id = l.product_id AND e.required_options = 0',
  105. array()
  106. )
  107. ->where('parent_id = ?', $parentId);
  108. $childrenIds = array(0 => array());
  109. foreach ($this->_getReadAdapter()->fetchAll($select) as $row) {
  110. $childrenIds[0][$row['product_id']] = $row['product_id'];
  111. }
  112. return $childrenIds;
  113. }
  114. /**
  115. * Retrieve parent ids array by requered child
  116. *
  117. * @param int|array $childId
  118. * @return array
  119. */
  120. public function getParentIdsByChild($childId)
  121. {
  122. $parentIds = array();
  123. $select = $this->_getReadAdapter()->select()
  124. ->from($this->getMainTable(), array('product_id', 'parent_id'))
  125. ->where('product_id IN(?)', $childId);
  126. foreach ($this->_getReadAdapter()->fetchAll($select) as $row) {
  127. $parentIds[] = $row['parent_id'];
  128. }
  129. return $parentIds;
  130. }
  131. /**
  132. * Collect product options with values according to the product instance and attributes, that were received
  133. *
  134. * @param Mage_Catalog_Model_Product $product
  135. * @param array $attributes
  136. * @return array
  137. */
  138. public function getConfigurableOptions($product, $attributes)
  139. {
  140. $attributesOptionsData = array();
  141. foreach ($attributes as $superAttribute) {
  142. $select = $this->_getReadAdapter()->select()
  143. ->from(
  144. array(
  145. 'super_attribute' => $this->getTable('catalog/product_super_attribute')
  146. ),
  147. array(
  148. 'sku' => 'entity.sku',
  149. 'product_id' => 'super_attribute.product_id',
  150. 'attribute_code' => 'attribute.attribute_code',
  151. 'option_title' => 'option_value.value',
  152. 'pricing_value' => 'attribute_pricing.pricing_value',
  153. 'pricing_is_percent' => 'attribute_pricing.is_percent'
  154. )
  155. )->joinInner(
  156. array(
  157. 'product_link' => $this->getTable('catalog/product_super_link')
  158. ),
  159. 'product_link.parent_id = super_attribute.product_id',
  160. array()
  161. )->joinInner(
  162. array(
  163. 'attribute' => $this->getTable('eav/attribute')
  164. ),
  165. 'attribute.attribute_id = super_attribute.attribute_id',
  166. array()
  167. )->joinInner(
  168. array(
  169. 'entity' => $this->getTable('catalog/product')
  170. ),
  171. 'entity.entity_id = product_link.product_id',
  172. array()
  173. )->joinInner(
  174. array(
  175. 'entity_value' => $superAttribute->getBackendTable()
  176. ),
  177. implode(
  178. ' AND ',
  179. array(
  180. $this->_getReadAdapter()
  181. ->quoteInto('entity_value.entity_type_id = ?', $product->getEntityTypeId()),
  182. 'entity_value.attribute_id = super_attribute.attribute_id',
  183. 'entity_value.store_id = 0',
  184. 'entity_value.entity_id = product_link.product_id'
  185. )
  186. ),
  187. array()
  188. )->joinLeft(
  189. array(
  190. 'option_value' => $this->getTable('eav/attribute_option_value')
  191. ),
  192. implode(' AND ', array(
  193. 'option_value.option_id = entity_value.value',
  194. 'option_value.store_id = ' . Mage_Core_Model_App::ADMIN_STORE_ID,
  195. )),
  196. array()
  197. )->joinLeft(
  198. array(
  199. 'attribute_pricing' => $this->getTable('catalog/product_super_attribute_pricing')
  200. ),
  201. implode(' AND ', array(
  202. 'super_attribute.product_super_attribute_id = attribute_pricing.product_super_attribute_id',
  203. 'entity_value.value = attribute_pricing.value_index'
  204. )),
  205. array()
  206. )->where('super_attribute.product_id = ?', $product->getId());
  207. $attributesOptionsData[$superAttribute->getAttributeId()] = $this->_getReadAdapter()->fetchAll($select);
  208. }
  209. return $attributesOptionsData;
  210. }
  211. }