ASH

This is the heart of the Advanced Shipping Helper for WPEC It is the entrypoint for interaction between ASH and WPEC.

Defined (1)

The class is defined in the following location(s).

/wpsc-includes/shipping.helper.php  
  1. class ASH{ 
  2. /** 
  3. * General constructor for ASH class 
  4. */ 
  5. function ASH() { 
  6.  
  7. /** 
  8. * Builds a shipment object representing the cart contents from WPEC 
  9. * @return ASHShipment 
  10. */ 
  11. function get_shipment() { 
  12. global $wpdb, $wpsc_cart; 
  13.  
  14. $shipment = new ASHShipment(); 
  15. if (!$wpsc_cart) { 
  16. return $shipment; 
  17.  
  18. foreach($wpsc_cart->cart_items as $cart_item) { 
  19. $package = new ASHPackage(); 
  20. //*** Set package dimensions ***\\ 
  21. $dimensions = get_product_meta($cart_item->product_id, 'product_metadata'); //The 'dimensions' meta doesn't exist. 
  22. if ( isset( $dimensions[0]['dimensions'] ) ) { 
  23. $dimensions = $dimensions[0]['dimensions']; 
  24. $dim_array = array(); 
  25. $dim_array["weight"] = $cart_item->weight; 
  26. $dim_array["height"] = ( !empty( $dimensions["height"] ) && is_numeric( $dimensions["height"] ) ) ? $dimensions["height"] : 1; 
  27. $dim_array["width"] = ( !empty( $dimensions["width"] ) && is_numeric( $dimensions["width"] ) ) ? $dimensions["width"] : 1; 
  28. $dim_array["length"] = ( !empty( $dimensions["length"] ) && is_numeric( $dimensions["length"] ) ) ? $dimensions["length"] : 1; 
  29. $package->set_dimensions($dim_array); 
  30.  
  31. /** Set other meta */ 
  32. $package->hazard = ( get_product_meta( $cart_item->product_id, "ship_hazard", TRUE ) === TRUE) ? TRUE : FALSE; //Fixed ternary evaluation. 
  33. $package->insurance = ( get_product_meta( $cart_item->product_id, "ship_insurance", TRUE ) === TRUE) ? TRUE : FALSE; //Fixed ternary evaluation. 
  34. $package->insured_amount = get_product_meta( $cart_item->product_id, "ship_insured_amount", TRUE ); //Fixed ternary evaluation. 
  35. $package->value = $cart_item->unit_price; 
  36. $package->contents = $cart_item->product_name; 
  37. $package->this_side_up = ( get_post_meta( $cart_item->product_id, "h:this_side_up", TRUE ) === TRUE ) ? TRUE : FALSE; //Prod. page hide, prod. UI display 
  38. if ($shipment->hazard === FALSE and $package->hazard === TRUE) { 
  39. $shipment->set_hazard(TRUE); 
  40. $quantity = (int)$cart_item->quantity; 
  41. $package->product_id[$cart_item->product_id] = 1; // The product in this package. 
  42. for($i=1; $i <= $quantity; $i++) { 
  43. $shipment->add_package($package); 
  44. return $shipment; 
  45.  
  46. /** 
  47. * Caches a result table for the given shipping module 
  48. * @param string $internal_name 
  49. * @param array $rate_table 
  50. * @param ASHShipment $shipment 
  51. */ 
  52. function cache_results($internal_name, $rate_table, $shipment) { 
  53. $wpec_ash = wpsc_get_customer_meta( 'shipping_ash' ); 
  54. if ( ! is_array( $wpec_ash ) ) 
  55. $wpec_ash = array(); 
  56.  
  57. if ( empty( $wpec_ash[$internal_name] ) || ! is_array( $wpec_ash[$internal_name] ) ) 
  58. $wpec_ash[$internal_name] = array(); 
  59.  
  60.  
  61.  
  62. $wpec_ash[$internal_name]["rate_table"] = $rate_table; 
  63. $shipment_vals = array("package_count"=>$shipment->package_count,  
  64. "destination" =>$shipment->destination,  
  65. "total_weight" =>$shipment->total_weight,  
  66. "rates_expire" =>$shipment->rates_expire ); //Refresh rates after today. 
  67. $wpec_ash[$internal_name]["shipment"] = $shipment_vals; 
  68. wpsc_update_customer_meta( 'shipping_ash', $wpec_ash ); 
  69. /** 
  70. * Checks cached results for given shipping module and returns 
  71. * the cached rates if nothing has changed. 
  72. * @param string $internal_name 
  73. * @param ASHShipment $shipment 
  74. */ 
  75. function check_cache($internal_name, $shipment) { 
  76. $wpec_ash = wpsc_get_customer_meta( 'shipping_ash' ); 
  77.  
  78. if ( ! $wpec_ash || ! is_array( $wpec_ash ) ) { //Avoids: Warning: 'array_key_exists' expects array. 
  79. return false; 
  80.  
  81. if ( ! array_key_exists( $internal_name, $wpec_ash ) ) { 
  82. return false; 
  83.  
  84. $cached_shipment = array(); 
  85.  
  86. if ( is_object( $wpec_ash[$internal_name]["shipment"] ) ) { 
  87. $cached_shipment = $wpec_ash[$internal_name]["shipment"]; 
  88. } else { 
  89. if ( ! empty( $wpec_ash[$internal_name]["shipment"] ) ) { 
  90. if ( is_array( $wpec_ash[$internal_name]["shipment"] ) ) { 
  91. $cached_shipment = $wpec_ash[$internal_name]["shipment"]; 
  92.  
  93. $shipment_vals = array("package_count"=>$shipment->package_count,  
  94. "destination" =>$shipment->destination,  
  95. "total_weight" =>$shipment->total_weight,  
  96. "rates_expire" =>$shipment->rates_expire ); //Refresh rates after today. 
  97. if ($cached_shipment["package_count"] != $shipment->package_count) { 
  98. return FALSE; 
  99. }elseif($cached_shipment["destination"] != $shipment_vals["destination"]) { 
  100. return FALSE; 
  101. }elseif($cached_shipment["total_weight"] != $shipment_vals["total_weight"]) { 
  102. return FALSE; 
  103. }elseif($cached_shipment["rates_expire"] != $shipment_vals["rates_expire"]) { //Refresh rates after today. 
  104. return FALSE; 
  105. }else{ 
  106. return $wpec_ash[$internal_name]; 
  107.  
  108.