Advertisement
KidCache

jigoshop_cart.class.php

Oct 16th, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.15 KB | None | 0 0
  1.     function add_to_cart($product_id, $quantity = 1, $variation_id = '', $variation = array(), $customer_design) {
  2.  
  3.         if ($quantity < 0) {
  4.             $quantity = 0;
  5.         }
  6.        
  7.         //customer design not present
  8.         if (empty($customer_design)) {
  9.             return false;
  10.         }
  11.  
  12.         // Load cart item data - may be added by other plugins
  13.         $cart_item_data = (array) apply_filters('jigoshop_add_cart_item_data', array(), $product_id);
  14.  
  15.         $cart_id = self::generate_cart_id($product_id, $variation_id, $variation, $cart_item_data);
  16.         $cart_item_key = self::find_product_in_cart( $cart_id );
  17.  
  18.         if (empty($variation_id)) {
  19.             $product = new jigoshop_product($product_id);
  20.         } else {
  21.             $product = new jigoshop_product_variation($variation_id);
  22.         }
  23.  
  24.         //product with a given ID doesn't exists
  25.         if (empty($product)) {
  26.             return false;
  27.         }
  28.  
  29.         // prevents adding products with no price to the cart
  30.         if ($product->get_price() === '') {
  31.             jigoshop::add_error(__('You cannot add this product to your cart because its price is not yet announced', 'jigoshop'));
  32.             return false;
  33.         }
  34.  
  35.         // prevents adding products to the cart without enough quantity on hand
  36.         $in_cart_qty = is_numeric($cart_item_key) ? self::$cart_contents[$cart_item_key]['quantity'] : 0;
  37.         if ($product->managing_stock() && !$product->has_enough_stock($quantity + $in_cart_qty)) :
  38.             if ($in_cart_qty > 0) :
  39.                 $error = (self::get_options()->get_option('jigoshop_show_stock') == 'yes') ? sprintf(__('We are sorry.  We do not have enough "%s" to fill your request.  You have %d of them in your Cart and we have %d available at this time.', 'jigoshop'), $product->get_title(), $in_cart_qty, $product->get_stock()) : sprintf(__('We are sorry.  We do not have enough "%s" to fill your request.', 'jigoshop'), $product->get_title());
  40.             else :
  41.                 $error = (self::get_options()->get_option('jigoshop_show_stock') == 'yes') ? sprintf(__('We are sorry.  We do not have enough "%s" to fill your request. There are only %d left in stock.', 'jigoshop'), $product->get_title(), $product->get_stock()) : sprintf(__('We are sorry.  We do not have enough "%s" to fill your request.', 'jigoshop'), $product->get_title());
  42.             endif;
  43.             jigoshop::add_error($error);
  44.             return false;
  45.         endif;
  46.  
  47.         //if product is already in the cart change its quantity
  48.         if ($cart_item_key) {
  49.  
  50.             $quantity = (int) $quantity + self::$cart_contents[$cart_item_key]['quantity'];
  51.  
  52.             self::set_quantity($cart_item_key, $quantity);
  53.  
  54.         } else {
  55.             // otherwise add new item to the cart
  56.             self::$cart_contents[$cart_id] = apply_filters( 'jigoshop_add_cart_item', array(
  57.                 'data'        => $product,
  58.                 'product_id'  => $product_id,
  59.                 'quantity'    => (int) $quantity,
  60.                 'variation'   => $variation,
  61.                 'variation_id'=> $variation_id,
  62.                 'customer_design' => $customer_design
  63.             ), $cart_item_data);
  64.         }
  65.  
  66.         self::set_session();
  67.  
  68.         return true;
  69.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement