public add_to_cart ( integer $product_id, integer $quantity = 1, integer $variation_id, array $variation = [], array $cart_item_data = [] ) : string | boolean | ||
$product_id | integer | contains the id of the product to add to the cart |
$quantity | integer | contains the quantity of the item to add |
$variation_id | integer | |
$variation | array | attribute values |
$cart_item_data | array | extra cart item data we want to pass into the item |
return | string | boolean | $cart_item_key |
public function add_to_cart($product_id = 0, $quantity = 1, $variation_id = 0, $variation = array(), $cart_item_data = array())
{
// Wrap in try catch so plugins can throw an exception to prevent adding to cart
try {
$product_id = absint($product_id);
$variation_id = absint($variation_id);
// Ensure we don't add a variation to the cart directly by variation ID
if ('product_variation' === get_post_type($product_id)) {
$variation_id = $product_id;
$product_id = wp_get_post_parent_id($variation_id);
}
// Get the product
$product_data = wc_get_product($variation_id ? $variation_id : $product_id);
// Sanity check
if ($quantity <= 0 || !$product_data || 'trash' === $product_data->get_status()) {
return false;
}
// Load cart item data - may be added by other plugins
$cart_item_data = (array) apply_filters('woocommerce_add_cart_item_data', $cart_item_data, $product_id, $variation_id);
// Generate a ID based on product ID, variation ID, variation data, and other cart item data
$cart_id = $this->generate_cart_id($product_id, $variation_id, $variation, $cart_item_data);
// Find the cart item key in the existing cart
$cart_item_key = $this->find_product_in_cart($cart_id);
// Force quantity to 1 if sold individually and check for existing item in cart
if ($product_data->is_sold_individually()) {
$quantity = apply_filters('woocommerce_add_to_cart_sold_individually_quantity', 1, $quantity, $product_id, $variation_id, $cart_item_data);
$in_cart_quantity = $cart_item_key ? $this->cart_contents[$cart_item_key]['quantity'] : 0;
if ($in_cart_quantity > 0) {
/* translators: %s: product name */
throw new Exception(sprintf('<a href="%s" class="button wc-forward">%s</a> %s', wc_get_cart_url(), __('View cart', 'woocommerce'), sprintf(__('You cannot add another "%s" to your cart.', 'woocommerce'), $product_data->get_name())));
}
}
// Check product is_purchasable
if (!$product_data->is_purchasable()) {
throw new Exception(__('Sorry, this product cannot be purchased.', 'woocommerce'));
}
// Stock check - only check if we're managing stock and backorders are not allowed
if (!$product_data->is_in_stock()) {
throw new Exception(sprintf(__('You cannot add "%s" to the cart because the product is out of stock.', 'woocommerce'), $product_data->get_name()));
}
if (!$product_data->has_enough_stock($quantity)) {
/* translators: 1: product name 2: quantity in stock */
throw new Exception(sprintf(__('You cannot add that amount of "%1$s" to the cart because there is not enough stock (%2$s remaining).', 'woocommerce'), $product_data->get_name(), $product_data->get_stock_quantity()));
}
// Stock check - this time accounting for whats already in-cart
if ($product_data->managing_stock()) {
$products_qty_in_cart = $this->get_cart_item_quantities();
if (isset($products_qty_in_cart[$product_data->get_stock_managed_by_id()]) && !$product_data->has_enough_stock($products_qty_in_cart[$product_data->get_stock_managed_by_id()] + $quantity)) {
throw new Exception(sprintf('<a href="%s" class="button wc-forward">%s</a> %s', wc_get_cart_url(), __('View Cart', 'woocommerce'), sprintf(__('You cannot add that amount to the cart — we have %1$s in stock and you already have %2$s in your cart.', 'woocommerce'), $product_data->get_stock_quantity(), $products_qty_in_cart[$product_data->get_id()])));
}
}
// If cart_item_key is set, the item is already in the cart
if ($cart_item_key) {
$new_quantity = $quantity + $this->cart_contents[$cart_item_key]['quantity'];
$this->set_quantity($cart_item_key, $new_quantity, false);
} else {
$cart_item_key = $cart_id;
// Add item after merging with $cart_item_data - hook to allow plugins to modify cart item
$this->cart_contents[$cart_item_key] = apply_filters('woocommerce_add_cart_item', array_merge($cart_item_data, array('product_id' => $product_id, 'variation_id' => $variation_id, 'variation' => $variation, 'quantity' => $quantity, 'data' => $product_data)), $cart_item_key);
}
if (did_action('wp')) {
$this->set_cart_cookies(!$this->is_empty());
}
do_action('woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data);
return $cart_item_key;
} catch (Exception $e) {
if ($e->getMessage()) {
wc_add_notice($e->getMessage(), 'error');
}
return false;
}
}
/** * Prepare recurring amounts, taxes etc for subscription items * * @access public * @param int $order_id * @return void */ public function new_order($order_id) { global $woocommerce; // Iterate over real cart and work with subscription products (if any) foreach ($woocommerce->cart->cart_contents as $cart_item_key => $cart_item) { $id = !empty($cart_item['variation_id']) ? $cart_item['variation_id'] : $cart_item['product_id']; if (Subscriptio_Subscription_Product::is_subscription($id)) { $product = new WC_Product($id); // Store all required renewal order fields here $renewal_order = array( 'taxes' => array(), 'shipping' => array(), ); // Create fake cart to mimic renewal order $renewal_cart = new WC_Cart(); // Add product to cart $renewal_cart->add_to_cart( $cart_item['product_id'], $cart_item['quantity'], (isset($cart_item['variation_id']) ? $cart_item['variation_id'] : ''), (isset($cart_item['variation']) ? $cart_item['variation'] : '') ); // Get fake cart item key $renewal_cart_item_keys = array_keys($renewal_cart->cart_contents); $renewal_cart_item_key = array_shift($renewal_cart_item_keys); // Set renewal price $renewal_cart->cart_contents[$renewal_cart_item_key]['data']->price = Subscriptio_Subscription_Product::get_recurring_price($id); // Add shipping if ($product->needs_shipping() && $renewal_cart->needs_shipping()) { // Get instance of checkout object to retrieve shipping options $wc_checkout = WC_Checkout::instance(); // Iterate over shipping packages foreach ($woocommerce->shipping->get_packages() as $package_key => $package) { // Check if this rate was selected if (isset($package['rates'][$wc_checkout->shipping_methods[$package_key]])) { // Check if it contains current subscription if (isset($package['contents'][$cart_item_key])) { // Save shipping details for further calculation $shipping_details = array( 'shipping_method' => $wc_checkout->shipping_methods[$package_key], 'destination' => $package['destination'], ); // Save shipping address $renewal_order['shipping_address'] = array( // First three lines may need to be changed to make this compatible with shipping extensions that allow multiple shipping addresses '_shipping_first_name' => $wc_checkout->posted['ship_to_different_address'] ? $wc_checkout->posted['shipping_first_name'] : $wc_checkout->posted['billing_first_name'], '_shipping_last_name' => $wc_checkout->posted['ship_to_different_address'] ? $wc_checkout->posted['shipping_last_name'] : $wc_checkout->posted['billing_last_name'], '_shipping_company' => $wc_checkout->posted['ship_to_different_address'] ? $wc_checkout->posted['shipping_company'] : $wc_checkout->posted['billing_company'], '_shipping_address_1' => $shipping_details['destination']['address'], '_shipping_address_2' => $shipping_details['destination']['address_2'], '_shipping_city' => $shipping_details['destination']['city'], '_shipping_state' => $shipping_details['destination']['state'], '_shipping_postcode' => $shipping_details['destination']['postcode'], '_shipping_country' => $shipping_details['destination']['country'], ); break; } } } // Got the shipping method and address for the package that contains current subscription? if (!isset($shipping_details)) { continue; } // Get packages based on renewal order details $packages = apply_filters('woocommerce_cart_shipping_packages', array( 0 => array( 'contents' => $renewal_cart->get_cart(), 'contents_cost' => isset($renewal_cart->cart_contents[$renewal_cart_item_key]['line_total']) ? $renewal_cart->cart_contents[$renewal_cart_item_key]['line_total'] : 0, 'applied_coupons' => $renewal_cart->applied_coupons, 'destination' => $shipping_details['destination'], ), )); // Now we need to calculate shipping costs but this requires overwriting session variables // In order not to affect real cart, we will overwrite them but then set them back to original values $original_session = array( 'chosen_shipping_methods' => $woocommerce->session->get('chosen_shipping_methods'), 'shipping_method_counts' => $woocommerce->session->get('shipping_method_counts'), ); // Set fake renewal values $woocommerce->session->set('chosen_shipping_methods', array($shipping_details['shipping_method'])); $woocommerce->session->set('shipping_method_counts', array(1)); // Override chosen shipping method in case there's a mismatch in shipping_method_counts (more than one available) add_filter('woocommerce_shipping_chosen_method', array($this, 'set_shipping_chosen_method')); $this->temp_shipping_chosen_method = $shipping_details['shipping_method']; // Calculate shipping for fake renewal order now $woocommerce->shipping->calculate_shipping($packages); // Remove filter remove_filter('woocommerce_shipping_chosen_method', array($this, 'set_shipping_chosen_method')); $this->temp_shipping_chosen_method = null; } // Recalculate totals $renewal_cart->calculate_totals(); // Get taxes foreach ($renewal_cart->get_tax_totals() as $rate_key => $rate) { $renewal_order['taxes'][] = array( 'name' => $rate_key, 'rate_id' => $rate->tax_rate_id, 'label' => $rate->label, 'compound' => absint($rate->is_compound ? 1 : 0), 'tax_amount' => wc_format_decimal(isset($renewal_cart->taxes[$rate->tax_rate_id]) ? $renewal_cart->taxes[$rate->tax_rate_id] : 0), 'shipping_tax_amount' => wc_format_decimal(isset($renewal_cart->shipping_taxes[$rate->tax_rate_id]) ? $renewal_cart->shipping_taxes[$rate->tax_rate_id] : 0), ); } // Get renewal_order_shipping $renewal_order['renewal_order_shipping'] = wc_format_decimal($renewal_cart->shipping_total); // Get renewal_order_shipping_tax $renewal_order['renewal_order_shipping_tax'] = wc_format_decimal($renewal_cart->shipping_tax_total); // Get renewal_cart_discount $renewal_order['renewal_cart_discount'] = wc_format_decimal($renewal_cart->get_cart_discount_total()); // Get renewal_order_discount $renewal_order['renewal_order_discount'] = wc_format_decimal($renewal_cart->get_order_discount_total()); // Get renewal_order_tax $renewal_order['renewal_order_tax'] = wc_format_decimal($renewal_cart->tax_total); // Get renewal_order_total $renewal_order['renewal_order_total'] = wc_format_decimal($renewal_cart->total, get_option('woocommerce_price_num_decimals')); // Get renewal_line_subtotal $renewal_order['renewal_line_subtotal'] = wc_format_decimal($renewal_cart->cart_contents[$renewal_cart_item_key]['line_subtotal']); // Get renewal_line_subtotal_tax $renewal_order['renewal_line_subtotal_tax'] = wc_format_decimal($renewal_cart->cart_contents[$renewal_cart_item_key]['line_subtotal_tax']); // Get renewal_line_total $renewal_order['renewal_line_total'] = wc_format_decimal($renewal_cart->cart_contents[$renewal_cart_item_key]['line_total']); // Get renewal_line_tax $renewal_order['renewal_line_tax'] = wc_format_decimal($renewal_cart->cart_contents[$renewal_cart_item_key]['line_tax']); // Get shipping details if ($product->needs_shipping()) { if (isset($woocommerce->shipping->packages[0]['rates'][$shipping_details['shipping_method']])) { $method = $woocommerce->shipping->packages[0]['rates'][$shipping_details['shipping_method']]; $renewal_order['shipping'] = array( 'name' => $method->label, 'method_id' => $method->id, 'cost' => wc_format_decimal( $method->cost ), ); } // Set session variables to original values and recalculate shipping for original order which is being processed now $woocommerce->session->set('chosen_shipping_methods', $original_session['chosen_shipping_methods']); $woocommerce->session->set('shipping_method_counts', $original_session['shipping_method_counts']); $woocommerce->shipping->calculate_shipping($packages); } // Save to object property so it can be accessed from another method $this->renewal_orders['by_cart_item_key'][$cart_item_key] = $renewal_order; } } }