/wpsc-components/theme-engine-v2/mvc/controllers/cart.php

  1. <?php 
  2.  
  3. class WPSC_Controller_Cart extends WPSC_Controller { 
  4. public function __construct() { 
  5. parent::__construct(); 
  6. require_once( WPSC_TE_V2_CLASSES_PATH . '/cart-item-table.php' ); 
  7. require_once( WPSC_TE_V2_CLASSES_PATH . '/cart-item-table-form.php' ); 
  8. $this->view = 'cart'; 
  9. $this->title = wpsc_get_cart_title(); 
  10. $this->init_cart_item_table(); 
  11.  
  12. private function init_cart_item_table() { 
  13. $cart_item_table = WPSC_Cart_Item_Table_Form::get_instance(); 
  14. $cart_item_table->show_tax = false; 
  15. $cart_item_table->show_shipping = false; 
  16. $cart_item_table->show_total = false; 
  17.  
  18. public function add( $product_id ) { 
  19. global $wpsc_cart; 
  20.  
  21. if ( ! wp_verify_nonce( $_REQUEST['_wp_nonce'], "wpsc-add-to-cart-{$product_id}" ) ) { 
  22. wp_die( __( 'Request expired. Please try adding the item to your cart again.', 'wpsc' ) ); 
  23.  
  24. extract( $_REQUEST, EXTR_SKIP ); 
  25.  
  26. $defaults = array( 
  27. 'variation_values' => array(),  
  28. 'quantity' => 1,  
  29. 'provided_price' => null,  
  30. 'comment' => null,  
  31. 'time_requested' => null,  
  32. 'custom_message' => null,  
  33. 'file_data' => null,  
  34. 'is_customisable' => false,  
  35. 'meta' => null,  
  36. ); 
  37.  
  38. $provided_parameters = array(); 
  39. $product_id = apply_filters( 'wpsc_add_to_cart_product_id', (int) $product_id ); 
  40.  
  41. if ( ! empty( $wpsc_product_variations ) ) { 
  42.  
  43. foreach ( $wpsc_product_variations as $key => $variation ) { 
  44. $provided_parameters['variation_values'][ (int) $key ] = (int) $variation; 
  45.  
  46. $variation_product_id = wpsc_get_child_object_in_terms( $product_id, $provided_parameters['variation_values'], 'wpsc-variation' ); 
  47.  
  48. if ( $variation_product_id > 0 ) { 
  49. $product_id = $variation_product_id; 
  50.  
  51. if ( ! empty( $quantity ) ) { 
  52. $provided_parameters['quantity'] = (int) $quantity; 
  53.  
  54. if ( ! empty( $is_customisable ) ) { 
  55. $provided_parameters['is_customisable'] = true; 
  56.  
  57. if ( isset( $custom_text ) ) { 
  58. $provided_parameters['custom_message'] = $custom_text; 
  59.  
  60. if ( isset( $_FILES['custom_file'] ) ) { 
  61. $provided_parameters['file_data'] = $_FILES['custom_file']; 
  62.  
  63. if ( isset( $donation_price ) && (float) $donation_price > 0 ) { 
  64. $provided_parameters['provided_price'] = (float) $donation_price; 
  65.  
  66. $parameters = array_merge( $defaults, $provided_parameters ); 
  67.  
  68. if ( $parameters['quantity'] <= 0 ) { 
  69. $this->message_collection->add( __( 'Sorry, but the quantity you just entered is not valid. Please try again.', 'wpsc' ), 'error', 'main', 'flash' ); 
  70. return; 
  71.  
  72. $product = apply_filters( 'wpsc_add_to_cart_product_object', get_post( $product_id, OBJECT, 'display' ) ); 
  73.  
  74. $stock = get_post_meta( $product_id, '_wpsc_stock', true ); 
  75.  
  76. $remaining_quantity = $wpsc_cart->get_remaining_quantity( $product_id, $parameters['variation_values'] ); 
  77.  
  78. if ( $stock !== '' && $remaining_quantity !== true ) { 
  79. if ( $remaining_quantity <= 0 ) { 
  80. $message = apply_filters( 'wpsc_add_to_cart_out_of_stock_message', __( 'Sorry, the product "%s" is out of stock.', 'wpsc' ) ); 
  81. $this->message_collection->add( sprintf( $message, $product->post_title ), 'error', 'main', 'flash' ); 
  82. wp_safe_redirect( wp_get_referer() ); 
  83. exit; 
  84. } elseif ( $remaining_quantity < $parameters['quantity'] ) { 
  85. $message = __( 'Sorry, but the quantity you just specified is larger than the available stock. There are only %d of the item in stock.', 'wpsc' ); 
  86. $this->message_collection->add( sprintf( $message, $remaining_quantity ), 'error', 'main', 'flash' ); 
  87. wp_safe_redirect( wp_get_referer() ); 
  88. exit; 
  89.  
  90. if ( wpsc_product_has_variations( $product_id ) && is_null( $parameters['variation_values'] ) ) { 
  91. $message = apply_filters( 'wpsc_add_to_cart_variation_missing_message', sprintf( __( 'This product has several options to choose from.<br /><br /><a href="%s" style="display:inline; float:none; margin: 0; padding: 0;">Visit the product page</a> to select options.', 'wpsc' ), esc_url( get_permalink( $product_id ) ) ), $product_id ); 
  92. $this->message_collection->add( sprintf( $message, $product->post_title ), 'error', 'main', 'flash' ); 
  93. wp_safe_redirect( wp_get_referer() ); 
  94. exit; 
  95.  
  96. if ( $wpsc_cart->set_item( $product_id, $parameters ) ) { 
  97. $message = sprintf( __( 'You just added %s to your cart.', 'wpsc' ), $product->post_title ); 
  98. $this->message_collection->add( $message, 'success', 'main', 'flash' ); 
  99. wp_safe_redirect( wpsc_get_cart_url() ); 
  100. exit; 
  101. } else { 
  102. $this->message_collection->add( __( 'An unknown error just occured. Please contact the shop administrator.', 'wpsc' ), 'error', 'main', 'flash' ); 
  103. wp_safe_redirect( wp_get_referer() ); 
  104. exit; 
  105.  
  106.  
  107. public function _callback_update_quantity() { 
  108. global $wpsc_cart; 
  109.  
  110. if ( ! wp_verify_nonce( $_REQUEST['_wp_nonce'], 'wpsc-cart-update' ) ) { 
  111. wp_die( __( 'Request expired. Please try updating the items in your cart again.', 'wpsc' ) ); 
  112.  
  113. $changed = 0; 
  114. $has_errors = false; 
  115.  
  116. extract( $_REQUEST, EXTR_SKIP ); 
  117.  
  118. foreach ( $wpsc_cart->cart_items as $key => &$item ) { 
  119. if ( isset( $quantity[ $key ] ) && $quantity[ $key ] != $item->quantity ) { 
  120.  
  121. $product = get_post( $item->product_id ); 
  122.  
  123. if ( ! is_numeric( $quantity[ $key ] ) ) { 
  124. $message = sprintf( __( 'Invalid quantity for %s.', 'wpsc' ), $product->post_title ); 
  125. $this->message_collection->add( $message, 'error' ); 
  126. continue; 
  127.  
  128. if ( $quantity[ $key ] > $item->quantity ) { 
  129. $product = WPSC_Product::get_instance( $item->product_id ); 
  130.  
  131. if ( ! $product->has_stock ) { 
  132. $message = __( "Sorry, all the remaining stock of %s has been claimed. Now you can only checkout with the current number of that item in your cart.", 'wpsc' ); 
  133. $this->message_collection->add( sprintf( $message, $product->post_title ), 'error' ); 
  134. $has_errors = true; 
  135. continue; 
  136. } elseif ( $product->has_limited_stock && $product->stock < $item->quantity ) { 
  137. $message = __( 'Sorry, but the quantity you just specified is larger than the available stock of %s. Besides the current number of that product in your cart, you can only add %d more.', 'wpsc' ); 
  138. $this->message_collection->add( sprintf( $message, $product->post_title, $product->stock ), 'error' ); 
  139. $has_errors = true; 
  140. continue; 
  141.  
  142. $item->quantity = $quantity[ $key ]; 
  143. $item->refresh_item(); 
  144. $changed++; 
  145.  
  146. $wpsc_cart->clear_cache(); 
  147.  
  148. if ( ! isset( $_POST['update_quantity'] ) && ! $has_errors ) { 
  149. wp_redirect( wpsc_get_checkout_url() ); 
  150. exit; 
  151.  
  152. if ( $changed ) { 
  153. $message = _n( 'You just successfully updated the quantity for %d item.', 'You just successfully updated the quantity for %d items.', $changed, 'wpsc' ); 
  154. $this->message_collection->add( sprintf( $message, $changed ), 'success' ); 
  155.  
  156. public function index() { 
  157. if ( isset( $_SESSION['coupon_numbers'] ) ) { 
  158. $GLOBALS['wpsc_coupons'] = new wpsc_coupons( $_SESSION['coupon_numbers'] ); 
  159.  
  160. if ( isset( $_POST['action'] ) && $_POST['action'] == 'update_quantity' ) { 
  161. $this->_callback_update_quantity(); 
  162.  
  163. public function clear() { 
  164. global $wpsc_cart; 
  165.  
  166. if ( ! wp_verify_nonce( $_REQUEST['_wp_nonce'], 'wpsc-clear-cart' ) ) { 
  167. wp_die( __( 'Request expired. Please go back and try clearing the cart again.', 'wpsc' ) ); 
  168.  
  169. $wpsc_cart->empty_cart(); 
  170. $this->message_collection->add( __( 'Shopping cart emptied.', 'wpsc' ) ); 
  171.  
  172. public function remove( $key ) { 
  173. global $wpsc_cart; 
  174.  
  175. if ( ! wp_verify_nonce( $_REQUEST['_wp_nonce'], "wpsc-remove-cart-item-{$key}" ) ) { 
  176. wp_die( __( 'Request expired. Please go back and try removing the cart item again.', 'wpsc' ) ); 
  177.  
  178. $wpsc_cart->remove_item( $key ); 
  179. $this->message_collection->add( __( 'Item removed.', 'wpsc' ) ); 
.