uc_shipping.api.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Shipping module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Handles additional data and activity for shipments.
  12. *
  13. * Adds additional activity after shipment objects are loaded from,
  14. * saved to, or deleted from the database. This is useful for shipment
  15. * modules that store method-specific shipment data in separate tables that
  16. * need to be kept in sync with the uc_shipments table.
  17. *
  18. * The members of the shipment object are the fields in the corresponding
  19. * record of the uc_shipments table, plus $shipment->packages, an array
  20. * of package objects as returned by uc_shipping_package_load().
  21. *
  22. * @param $op
  23. * The action being taken on the shipment. One of the following values:
  24. * - load: The shipment and its packages are loaded from the database.
  25. * - save: Changes to the shipment have been written.
  26. * - delete: The shipment has been deleted and the packages are available
  27. * for reshipment.
  28. * @param $shipment
  29. * The shipment object.
  30. *
  31. * @return
  32. * Only given when $op is "load". An associative array of extra data to
  33. * be added to the shipment object. Each key/value element of the array
  34. * becomes a separate member of the shipment object. Elements of the array
  35. * with the same key as members of the shipment object replace those members
  36. * of the shipment object.
  37. */
  38. function hook_uc_shipment($op, $shipment) {
  39. switch ($op) {
  40. case 'save':
  41. $google_order_number = uc_google_checkout_get_google_number($shipment->order_id);
  42. if ($google_order_number && $shipment->is_new) {
  43. $xml_data = '';
  44. foreach ($shipment->packages as $package) {
  45. if ($package->tracking_number) {
  46. $tracking_number = $package->tracking_number;
  47. }
  48. elseif ($shipment->tracking_number) {
  49. $tracking_number = $shipment->tracking_number;
  50. }
  51. if ($tracking_number) {
  52. foreach ($package->products as $product) {
  53. $xml_data .= '<item-shipping-information>';
  54. $xml_data .= '<item-id>';
  55. $xml_data .= '<merchant-item-id>' . check_plain($product->nid . '|' . $product->model) . '</merchant-item-id>';
  56. $xml_data .= '</item-id>';
  57. $xml_data .= '<tracking-data-list>';
  58. $xml_data .= '<tracking-data>';
  59. $xml_data .= '<carrier>' . check_plain($shipment->carrier) . '</carrier>';
  60. $xml_data .= '<tracking-number>' . check_plain($tracking_number) . '</tracking-number>';
  61. $xml_data .= '</tracking-data>';
  62. $xml_data .= '</tracking-data-list>';
  63. $xml_data .= '</item-shipping-information>';
  64. }
  65. }
  66. }
  67. if ($xml_data) {
  68. $request = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
  69. $request .= '<ship-items xmlns="http://checkout.google.com/schema/2" google-order-number="' . $google_order_number . '">';
  70. $request .= '<item-shipping-information-list>';
  71. $request .= $xml_data;
  72. $request .= '</item-shipping-information-list>';
  73. $request .= '<send-email>true</send-email>';
  74. $request .= '</ship-items>';
  75. $response = uc_google_checkout_send_request('request', $request);
  76. }
  77. }
  78. break;
  79. case 'delete':
  80. $google_order_number = uc_google_checkout_get_google_number($shipment->order_id);
  81. if ($google_order_number) {
  82. foreach ($shipment->packages as $package) {
  83. foreach ($package->products as $product) {
  84. $reset_ids[] = check_plain($product->nid . '|' . $product->model);
  85. }
  86. }
  87. $request = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
  88. $request .= '<reset-items-shipping-information xmlns="http://checkout.google.com/schema/2" google-order-number="' . $google_order_number . '">';
  89. $request .= '<item-ids>';
  90. foreach (array_unique($reset_ids) as $item_id) {
  91. $request .= '<item-id>';
  92. $request .= '<merchant-item-id>' . $item_id . '</merchant-item-id>';
  93. $request .= '</item-id>';
  94. }
  95. $request .= '</item-ids>';
  96. $request .= '<send-email>false</send-email>';
  97. $request .= '</reset-items-shipping-information>';
  98. }
  99. $response = uc_google_checkout_send_request('request', $request);
  100. break;
  101. }
  102. }
  103. /**
  104. * @} End of "addtogroup hooks".
  105. */