Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function add_to_cart($product_id, $quantity = 1, $variation_id = '', $variation = array(), $customer_design) {
- if ($quantity < 0) {
- $quantity = 0;
- }
- //customer design not present
- if (empty($customer_design)) {
- return false;
- }
- // Load cart item data - may be added by other plugins
- $cart_item_data = (array) apply_filters('jigoshop_add_cart_item_data', array(), $product_id);
- $cart_id = self::generate_cart_id($product_id, $variation_id, $variation, $cart_item_data);
- $cart_item_key = self::find_product_in_cart( $cart_id );
- if (empty($variation_id)) {
- $product = new jigoshop_product($product_id);
- } else {
- $product = new jigoshop_product_variation($variation_id);
- }
- //product with a given ID doesn't exists
- if (empty($product)) {
- return false;
- }
- // prevents adding products with no price to the cart
- if ($product->get_price() === '') {
- jigoshop::add_error(__('You cannot add this product to your cart because its price is not yet announced', 'jigoshop'));
- return false;
- }
- // prevents adding products to the cart without enough quantity on hand
- $in_cart_qty = is_numeric($cart_item_key) ? self::$cart_contents[$cart_item_key]['quantity'] : 0;
- if ($product->managing_stock() && !$product->has_enough_stock($quantity + $in_cart_qty)) :
- if ($in_cart_qty > 0) :
- $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());
- else :
- $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());
- endif;
- jigoshop::add_error($error);
- return false;
- endif;
- //if product is already in the cart change its quantity
- if ($cart_item_key) {
- $quantity = (int) $quantity + self::$cart_contents[$cart_item_key]['quantity'];
- self::set_quantity($cart_item_key, $quantity);
- } else {
- // otherwise add new item to the cart
- self::$cart_contents[$cart_id] = apply_filters( 'jigoshop_add_cart_item', array(
- 'data' => $product,
- 'product_id' => $product_id,
- 'quantity' => (int) $quantity,
- 'variation' => $variation,
- 'variation_id'=> $variation_id,
- 'customer_design' => $customer_design
- ), $cart_item_data);
- }
- self::set_session();
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement