PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/sites/all/modules/ubercart/uc_product/uc_product.api.php

https://bitbucket.org/SkyBars/newplanet
PHP | 195 lines | 91 code | 19 blank | 85 comment | 14 complexity | 3f1452f7047ab3f7b6b4e187a6b79ab8 MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Product module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Make alterations to a specific variant of a product node.
  12. *
  13. * @param $node
  14. * The product node to be altered.
  15. */
  16. function hook_uc_product_alter(&$node) {
  17. if (isset($node->data['attributes']) && is_array($node->data['attributes'])) {
  18. $options = _uc_cart_product_get_options($node);
  19. foreach ($options as $option) {
  20. $node->cost += $option['cost'];
  21. $node->price += $option['price'];
  22. $node->weight += $option['weight'];
  23. }
  24. $combination = array();
  25. foreach ($node->data['attributes'] as $aid => $value) {
  26. if (is_numeric($value)) {
  27. $attribute = uc_attribute_load($aid, $node->nid, 'product');
  28. if ($attribute && ($attribute->display == 1 || $attribute->display == 2)) {
  29. $combination[$aid] = $value;
  30. }
  31. }
  32. }
  33. ksort($combination);
  34. $model = db_query("SELECT model FROM {uc_product_adjustments} WHERE nid = :nid AND combination LIKE :combo", array(':nid' => $node->nid, ':combo' => serialize($combination)))->fetchField();
  35. if (!empty($model)) {
  36. $node->model = $model;
  37. }
  38. }
  39. }
  40. /**
  41. * Performs actions on product classes.
  42. *
  43. * @param $type
  44. * The node type of the product class.
  45. * @param $op
  46. * The action being performed on the product class:
  47. * - insert: A new node type is created, or an existing node type is being
  48. * converted into a product type.
  49. * - update: A product class has been updated.
  50. * - delete: A product class has been deleted. Modules that have attached
  51. * additional information to the node type because it is a product type
  52. * should delete this information.
  53. */
  54. function hook_uc_product_class($type, $op) {
  55. switch ($op) {
  56. case 'delete':
  57. db_delete('uc_class_attributes')
  58. ->condition('pcid', $type)
  59. ->execute();
  60. db_delete('uc_class_attribute_options')
  61. ->condition('pcid', $type)
  62. ->execute();
  63. break;
  64. }
  65. }
  66. /**
  67. * Define default product classes.
  68. *
  69. * The results of this hook are eventually passed through hook_node_info(),
  70. * so you may include any keys that hook_node_info() uses. Defaults will
  71. * be provided where keys are not set. This hook can also be used to
  72. * override the default "product" product class name and description.
  73. */
  74. function hook_uc_product_default_classes() {
  75. return array(
  76. 'my_class' => array(
  77. 'name' => t('My product class'),
  78. 'description' => t('Content type description for my product class.'),
  79. ),
  80. );
  81. }
  82. /**
  83. * Returns a structured array representing the given product's description.
  84. *
  85. * Modules that add data to cart items when they are selected should display it
  86. * with this hook. The return values from each implementation will be
  87. * sent through to hook_uc_product_description_alter() implementations and then
  88. * all descriptions are rendered using drupal_render().
  89. *
  90. * @param $product
  91. * Product. Usually one of the values of the array returned by
  92. * uc_cart_get_contents().
  93. *
  94. * @return
  95. * A structured array that can be fed into drupal_render().
  96. */
  97. function hook_uc_product_description($product) {
  98. $description = array(
  99. 'attributes' => array(
  100. '#product' => array(
  101. '#type' => 'value',
  102. '#value' => $product,
  103. ),
  104. '#theme' => 'uc_product_attributes',
  105. '#weight' => 1,
  106. ),
  107. );
  108. $desc =& $description['attributes'];
  109. // Cart version of the product has numeric attribute => option values so we
  110. // need to retrieve the right ones
  111. $weight = 0;
  112. if (empty($product->order_id)) {
  113. foreach (_uc_cart_product_get_options($product) as $option) {
  114. if (!isset($desc[$option['aid']])) {
  115. $desc[$option['aid']]['#attribute_name'] = $option['attribute'];
  116. $desc[$option['aid']]['#options'] = array($option['name']);
  117. }
  118. else {
  119. $desc[$option['aid']]['#options'][] = $option['name'];
  120. }
  121. $desc[$option['aid']]['#weight'] = $weight++;
  122. }
  123. }
  124. else {
  125. foreach ((array)$product->data['attributes'] as $attribute => $option) {
  126. $desc[] = array(
  127. '#attribute_name' => $attribute,
  128. '#options' => $option,
  129. '#weight' => $weight++,
  130. );
  131. }
  132. }
  133. return $description;
  134. }
  135. /**
  136. * Alters the given product description.
  137. *
  138. * @param $description
  139. * Description array reference.
  140. * @param $product
  141. * The product being described.
  142. */
  143. function hook_uc_product_description_alter(&$description, $product) {
  144. $description['attributes']['#weight'] = 2;
  145. }
  146. /**
  147. * Notifies core of any SKUs your module adds to a given node.
  148. *
  149. * NOTE: DO NOT map the array keys, as the possibility for numeric SKUs exists,
  150. * and this will conflict with the behavior of module_invoke_all(), specifically
  151. * array_merge_recursive().
  152. *
  153. * Code lifted from uc_attribute.module.
  154. */
  155. function hook_uc_product_models($nid) {
  156. // Get all the SKUs for all the attributes on this node.
  157. $models = db_query("SELECT DISTINCT model FROM {uc_product_adjustments} WHERE nid = :nid", array(':nid' => $nid))->fetchCol();
  158. return $models;
  159. }
  160. /**
  161. * Lists node types which should be considered products.
  162. *
  163. * Trusts the duck philosophy of object identification: if it walks like a duck,
  164. * quacks like a duck, and has feathers like a duck, it's probably a duck.
  165. * Products are nodes with prices, SKUs, and everything else Ubercart expects
  166. * them to have.
  167. *
  168. * @return
  169. * Array of node type ids.
  170. */
  171. function hook_uc_product_types() {
  172. return array('product_kit');
  173. }
  174. /**
  175. * @} End of "addtogroup hooks".
  176. */