/** * Add a product to the cart * * @param string $product_id contains the id of the product to add to the cart * @param int|string $quantity contains the quantity of the item to add * @param int|string $variation_id * @param array $variation attribute values * @return bool */ public static function add_to_cart($product_id, $quantity = 1, $variation_id = '', $variation = array()) { if ($quantity < 0) { $quantity = 0; } // Load cart item data - may be added by other plugins $cart_item_data = (array) apply_filters('fflcommerce_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); // prevents adding non-valid products to the cart $this_post = get_post($product_id); if ($this_post->post_type != 'product') { fflcommerce::add_error(__('You cannot add this item to your Cart as it does not appear to be a valid Product.', 'fflcommerce')); return false; } // create a product record to work from if (empty($variation_id)) { $product = new fflcommerce_product($product_id); } else { $product = new fflcommerce_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() === '') { fflcommerce::add_error(__('You cannot add this product to your cart because its price is not yet announced', 'fflcommerce')); return false; } // products newly added to the Cart will not have a $cart_item_key, use 0 quantity $in_cart_qty = !empty($cart_item_key) ? self::$cart_contents[$cart_item_key]['quantity'] : 0; // prevents adding products to the cart without enough quantity on hand if (!$product->has_enough_stock($quantity + $in_cart_qty)) { fflcommerce::add_error(sprintf(__('We are sorry. We do not have enough "%s" to fill your request.', 'fflcommerce'), $product->get_title())); if (self::get_options()->get('fflcommerce_show_stock') == 'yes') { if ($in_cart_qty > 0) { fflcommerce::add_error(sprintf(__('You have %d of them in your Cart and we have %d available at this time.', 'fflcommerce'), $in_cart_qty, $product->get_stock())); } else { fflcommerce::add_error(sprintf(__('There are only %d left in stock.', 'fflcommerce'), $product->get_stock())); } } return false; } // 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, apply_filters('fflcommerce_cart_item_quantity', $quantity, $product, $cart_item_key)); } else { // otherwise add new item to the cart self::$cart_contents[$cart_id] = apply_filters('fflcommerce_add_cart_item', array('data' => $product, 'product_id' => $product_id, 'variation' => $variation, 'variation_id' => $variation_id, 'quantity' => (int) $quantity, 'unit_price' => 0, 'tax' => 0, 'discount' => 0, 'price_includes_tax' => self::get_options()->get('fflcommerce_prices_include_tax')), $cart_item_data); } self::set_session(); return true; }
function fflcommerce_add_order_item() { $fflcommerce_options = FFLCommerce_Base::get_options(); check_ajax_referer('add-order-item', 'security'); global $wpdb; $item_to_add = trim(stripslashes($_POST['item_to_add'])); $post = ''; // Find the item if (is_numeric($item_to_add)) { $post = get_post($item_to_add); } if (!$post || $post->post_type !== 'product' && $post->post_type !== 'product_variation') { $post_id = $wpdb->get_var($wpdb->prepare("\n\t\t\tSELECT post_id\n\t\t\tFROM {$wpdb->posts}\n\t\t\tLEFT JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)\n\t\t\tWHERE {$wpdb->postmeta}.meta_key = 'SKU'\n\t\t\tAND {$wpdb->posts}.post_status = 'publish'\n\t\t\tAND {$wpdb->posts}.post_type = 'shop_product'\n\t\t\tAND {$wpdb->postmeta}.meta_value = %s\n\t\t\tLIMIT 1\n\t\t", $item_to_add)); $post = get_post($post_id); } if (!$post || $post->post_type !== 'product' && $post->post_type !== 'product_variation') { die; } if ($post->post_type == "product") { $_product = new fflcommerce_product($post->ID); } else { $_product = new fflcommerce_product_variation($post->ID); } $loop = 0; ?> <tr class="item"> <?php do_action('fflcommerce_admin_order_item_before_prod_id', intval($_POST['item_no'])); ?> <td class="product-id">#<?php echo $_product->id; ?> </td> <td class="variation-id"><?php if (isset($_product->variation_id)) { echo $_product->variation_id; } else { echo '-'; } ?> </td> <td class="product-sku"><?php if ($_product->sku) { echo $_product->sku; } ?> </td> <td class="name"><a href="<?php echo esc_url(admin_url('post.php?post=' . $_product->id . '&action=edit')); ?> "><?php echo $_product->get_title(); ?> </a></td> <td class="variation"><?php if (isset($_product->variation_data)) { echo fflcommerce_get_formatted_variation($_product, array(), true); } else { echo '-'; } ?> </td> <?php do_action('fflcommerce_admin_order_item_values', $_product, array(), 0); ?> <td class="quantity"><input type="text" name="item_quantity[]" placeholder="<?php _e('Quantity e.g. 2', 'fflcommerce'); ?> " value="1" /></td> <td class="cost"><input type="text" name="item_cost[]" placeholder="<?php _e('Cost per unit ex. tax e.g. 2.99', 'fflcommerce'); ?> " value="<?php echo esc_attr($fflcommerce_options->get('fflcommerce_prices_include_tax') == 'yes' ? $_product->get_price_excluding_tax() : $_product->get_price()); ?> " /></td> <td class="tax"><input type="text" name="item_tax_rate[]" placeholder="<?php _e('Tax Rate e.g. 20.0000', 'fflcommerce'); ?> " value="<?php echo esc_attr(fflcommerce_tax::calculate_total_tax_rate($_product->get_tax_base_rate())); ?> " /></td> <td class="center"> <input type="hidden" name="item_id[]" value="<?php echo esc_attr($_product->id); ?> " /> <input type="hidden" name="item_name[]" value="<?php echo esc_attr($_product->get_title()); ?> " /> <input type="hidden" name="item_variation_id[]" value="<?php if ($_product instanceof fflcommerce_product_variation) { echo esc_attr($_product->variation_id); } else { echo ''; } ?> " /> <button type="button" class="remove_row button">×</button> </td> </tr> <?php // Quit out die; }