wpsc_add_to_cart

Add_to_cart function, used through ajax and in normal page loading.

Description

wpsc_add_to_cart(); 

No parameters, returns nothing


Usage

  1. if ( !function_exists( 'wpsc_add_to_cart' ) ) { 
  2. require_once '/wpsc-components/theme-engine-v1/helpers/ajax.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wpsc_add_to_cart(); 
  7.  

Defined (1)

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

/wpsc-components/theme-engine-v1/helpers/ajax.php  
  1. function wpsc_add_to_cart() { 
  2. global $wpsc_cart; 
  3.  
  4. $default_parameters = $cart_messages = $provided_parameters = array(); 
  5.  
  6. /// default values 
  7. $default_parameters['variation_values'] = null; 
  8. $default_parameters['quantity'] = 1; 
  9. $default_parameters['provided_price'] = null; 
  10. $default_parameters['comment'] = null; 
  11. $default_parameters['time_requested'] = null; 
  12. $default_parameters['custom_message'] = null; 
  13. $default_parameters['file_data'] = null; 
  14. $default_parameters['is_customisable'] = false; 
  15. $default_parameters['meta'] = null; 
  16.  
  17. $post_type_object = get_post_type_object( 'wpsc-product' ); 
  18. $permitted_post_statuses = current_user_can( $post_type_object->cap->edit_posts ) ? apply_filters( 'wpsc_product_display_status', array( 'publish' ) ) : array( 'publish' ); 
  19.  
  20. /// sanitise submitted values 
  21. $product_id = apply_filters( 'wpsc_add_to_cart_product_id' , (int) $_POST['product_id'] ); 
  22. $product = apply_filters( 'wpsc_add_to_cart_product_object', get_post( $product_id, OBJECT, 'display' ) ); 
  23.  
  24. if ( ! in_array( $product->post_status, $permitted_post_statuses ) || 'wpsc-product' != $product->post_type ) { 
  25. return false; 
  26.  
  27. // compatibility with older themes 
  28. if ( isset( $_POST['wpsc_quantity_update'] ) && is_array( $_POST['wpsc_quantity_update'] ) ) { 
  29. $_POST['wpsc_quantity_update'] = $_POST['wpsc_quantity_update'][$product_id]; 
  30.  
  31. if ( isset( $_POST['variation'] ) ) { 
  32.  
  33. foreach ( (array) $_POST['variation'] as $key => $variation ) { 
  34. $provided_parameters['variation_values'][ (int) $key ] = (int) $variation; 
  35.  
  36. if ( count( $provided_parameters['variation_values'] ) > 0 ) { 
  37. $variation_product_id = wpsc_get_child_object_in_terms( $product_id, $provided_parameters['variation_values'], 'wpsc-variation' ); 
  38. if ( $variation_product_id > 0 ) { 
  39. $product_id = $variation_product_id; 
  40.  
  41. if ( (isset( $_POST['quantity'] ) && $_POST['quantity'] > 0) && (!isset( $_POST['wpsc_quantity_update'] )) ) { 
  42. $provided_parameters['quantity'] = (int) $_POST['quantity']; 
  43. } else if ( isset( $_POST['wpsc_quantity_update'] ) ) { 
  44. $wpsc_cart->remove_item( $_POST['key'] ); 
  45. $provided_parameters['quantity'] = (int) $_POST['wpsc_quantity_update']; 
  46.  
  47. if ( isset( $_POST['is_customisable'] ) && 
  48. 'true' == $_POST['is_customisable'] ) { 
  49. $provided_parameters['is_customisable'] = true; 
  50.  
  51. if ( isset( $_POST['custom_text'] ) ) { 
  52. $provided_parameters['custom_message'] = stripslashes( $_POST['custom_text'] ); 
  53. if ( isset( $_FILES['custom_file'] ) ) { 
  54. $provided_parameters['file_data'] = $_FILES['custom_file']; 
  55.  
  56. if ( isset( $_POST['donation_price'] ) && ( (float) $_POST['donation_price'] > 0 ) ) { 
  57. $provided_parameters['provided_price'] = (float) $_POST['donation_price']; 
  58.  
  59. $parameters = array_merge( $default_parameters, (array) $provided_parameters ); 
  60.  
  61. $cart_item = $wpsc_cart->set_item( $product_id, $parameters ); 
  62.  
  63. if ( is_object( $cart_item ) ) { 
  64.  
  65. do_action( 'wpsc_add_to_cart', $product, $cart_item ); 
  66. $cart_messages[] = str_replace( "[product_name]", $cart_item->get_title(), __( 'You just added "[product_name]" to your cart.', 'wpsc' ) ); 
  67.  
  68. } else { 
  69. if ( $parameters['quantity'] <= 0 ) { 
  70.  
  71. $cart_messages[] = __( 'Sorry, but you cannot add zero items to your cart', 'wpsc' ); 
  72.  
  73. } else if ( wpsc_product_has_variations( $product_id ) && is_null( $parameters['variation_values'] ) ) { 
  74.  
  75. $cart_messages[] = 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 ); 
  76.  
  77. } else if ( $wpsc_cart->get_remaining_quantity( $product_id, $parameters['variation_values'], $parameters['quantity'] ) > 0 ) { 
  78.  
  79. $quantity = $wpsc_cart->get_remaining_quantity( $product_id, $parameters['variation_values'], $parameters['quantity'] ); 
  80. $cart_messages[] = sprintf( _n( 'Sorry, but there is only %s of this item in stock.', 'Sorry, but there are only %s of this item in stock.', $quantity, 'wpsc' ), $quantity ); 
  81.  
  82. } else { 
  83.  
  84. $cart_messages[] = apply_filters( 'wpsc_add_to_cart_out_of_stock_message', __( 'Sorry, but this item is out of stock.', 'wpsc' ), $product_id ); 
  85.  
  86.  
  87. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { 
  88. $json_response = array( 'cart_messages' => $cart_messages, 'product_id' => $product_id, 'cart_total' => wpsc_cart_total() ); 
  89.  
  90. $output = _wpsc_ajax_get_cart( false, $cart_messages ); 
  91.  
  92. $json_response = $json_response + $output; 
  93.  
  94. if ( is_numeric( $product_id ) && 1 == get_option( 'fancy_notifications' ) ) 
  95. $json_response['fancy_notification'] = str_replace( array( "\n", "\r" ), array( '\n', '\r' ), fancy_notification_content( $cart_messages ) ); 
  96.  
  97. $json_response = apply_filters( 'wpsc_add_to_cart_json_response', $json_response ); 
  98.  
  99. die( json_encode( $json_response ) );