/**
 * Validate Product Cart Quantity
 * Checks that the quantity is within the permitted bounds and return a valid quantity.
 *
 * @since  3.8.10
 * @access public
 *
 * @param  int  $quantity                    Cart item product quantity.
 * @param  int  $prod_id                     Optional. Product ID.
 * @return int                               The maximum quantity that can be added to the cart.
 *
 * @uses   wpsc_product_max_cart_quantity    Gets the maximum product cart quantity.
 * @uses   wpsc_product_min_cart_quantity    Gets the minimum product cart quantity.
 * @uses wpsc_cart
 */
function wpsc_validate_product_cart_quantity($quantity, $product_id = 0)
{
    $max_quantity = wpsc_product_max_cart_quantity($product_id);
    $min_quantity = wpsc_product_min_cart_quantity($product_id);
    if ($quantity > $max_quantity) {
        return $max_quantity;
    }
    if ($quantity < $min_quantity) {
        return $min_quantity;
    }
    return $quantity;
}
Esempio n. 2
0
 public function _callback_update_quantity()
 {
     global $wpsc_cart;
     if (!wp_verify_nonce($_REQUEST['_wp_nonce'], 'wpsc-cart-update')) {
         wp_die(__('Request expired. Please try updating the items in your cart again.', 'wpsc'));
     }
     $changed = 0;
     $has_errors = false;
     extract($_REQUEST, EXTR_SKIP);
     foreach ($wpsc_cart->cart_items as $key => &$item) {
         if (isset($quantity[$key]) && $quantity[$key] != $item->quantity) {
             $product = get_post($item->product_id);
             if (!is_numeric($quantity[$key])) {
                 $message = sprintf(__('Invalid quantity for %s.', 'wpsc'), $product->post_title);
                 $this->message_collection->add($message, 'error');
                 continue;
             }
             if ($quantity[$key] < wpsc_product_min_cart_quantity($item->product_id)) {
                 $message = __('Sorry, but the quantity you just specified is lower than the minimum allowed quantity of %s. You must purchase at least %s at a time.', 'wpsc');
                 $this->message_collection->add(sprintf($message, $product->post_title, number_format_i18n(wpsc_product_min_cart_quantity($item->product_id))), 'error');
                 $has_errors = true;
                 continue;
             }
             if ($quantity[$key] > $item->quantity) {
                 $product = WPSC_Product::get_instance($item->product_id);
                 if (!$product->has_stock) {
                     $message = __("Sorry, all the remaining stock of %s has been claimed. You can only checkout with the current quantity in your cart.", 'wpsc');
                     $this->message_collection->add(sprintf($message, $product->post->post_title), 'error');
                     $has_errors = true;
                     continue;
                 }
                 if ($product->has_limited_stock && $product->stock < $item->quantity) {
                     $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');
                     $this->message_collection->add(sprintf($message, $product->post->post_title, $product->stock), 'error');
                     $has_errors = true;
                     continue;
                 }
                 if ($quantity[$key] > wpsc_product_max_cart_quantity($item->product_id)) {
                     $message = __('Sorry, but the quantity you just specified is larger than the maximum allowed quantity of %s. You may only purchase %s at a time.', 'wpsc');
                     $this->message_collection->add(sprintf($message, $product->post->post_title, number_format_i18n(wpsc_product_max_cart_quantity($item->product_id))), 'error');
                     $has_errors = true;
                     continue;
                 }
             }
             $item->quantity = $quantity[$key];
             $item->refresh_item();
             $changed++;
         }
     }
     $wpsc_cart->clear_cache();
     if (!isset($_POST['update_quantity']) && !$has_errors) {
         wp_redirect(wpsc_get_checkout_url());
         exit;
     }
     if ($changed) {
         $message = _n('You just successfully updated the quantity for %d item.', 'You just successfully updated the quantity for %d items.', $changed, 'wpsc');
         $this->message_collection->add(sprintf($message, $changed), 'success');
     }
 }