You are here

function commerce_product_bundle_update_cart in Commerce Product Bundle 7

Same name and namespace in other branches
  1. 7.2 commerce_product_bundle.module \commerce_product_bundle_update_cart()

Updates the specified product in a customer's shopping cart.

Most of the code is copied from commerce_cart_product_add, we need to copy to reorder the rules invokations.

Parameters

int $uid: The uid of the user whose cart you are adding the product to.

int $product_id: The ID of the product to add to the cart.

int $quantity: The quantity of this product to add to the cart.

array $subproducts: An array of products that relates to this bundle.

int $line_item_id: The id of the line item.

Return value

obj|FALSE Returns FALSE if we can't load a product from the provided $product_id. Returns updated line item object.

1 call to commerce_product_bundle_update_cart()
commerce_product_bundle_add_to_cart_form_submit in ./commerce_product_bundle.module
Submit function to add product bundles to the cart.

File

./commerce_product_bundle.module, line 993
Allows the bundling of products in Drupal Commerce.

Code

function commerce_product_bundle_update_cart($uid, $product_id, $quantity, $subproducts, $line_item_id) {

  // Load and validate the specified product ID.
  $product = commerce_product_load($product_id);

  // Fail if the product does not exist or is disabled.
  if (empty($product) || !$product->status) {
    return FALSE;
  }

  // First attempt to load the customer's shopping cart order.
  $order = commerce_cart_order_load($uid);

  // If no order existed, create one now.
  if (empty($order)) {
    $order = commerce_cart_order_new($uid);
  }

  // Wrap the order for easy access to field data.
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $line_item = commerce_line_item_load($line_item_id);
  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
  if (empty($line_item_wrapper)) {
    return;
  }

  // Invoke the product prepare event with the shopping cart order.
  rules_invoke_all('commerce_cart_product_prepare', $order, $product, $quantity);
  $line_item_wrapper->quantity = $quantity;
  $line_item_wrapper->commerce_product = $product_id;

  // Remove all current line items.
  $sub_line_items = commerce_product_bundle_get_sub_line_items($line_item, TRUE);
  commerce_line_item_delete_multiple(array_keys($sub_line_items));

  // Iterates over all sub products:
  foreach ($subproducts as $item_values) {

    // Check product.
    $subproduct = commerce_product_load($item_values['product_id']);
    if (empty($subproduct) || !$subproduct->status) {

      // Skip this item, because it is not a valid one.
      continue;
    }

    // Check quantity.
    if ($item_values['quantity'] < 0) {

      // Skip, because it is not a valid quantity.
      continue;
    }
    $sub_line_item = commerce_product_bundle_line_item_new($subproduct, $line_item, $item_values['quantity'], $order->order_id);

    // Process the unit price through Rules so it reflects the user's actual
    // purchase price.
    rules_invoke_event('commerce_product_calculate_sell_price', $sub_line_item);

    // Save the line item.
    commerce_line_item_save($sub_line_item);
    $sub_line_items[] = $sub_line_item;
  }
  rules_invoke_event('commerce_product_calculate_sell_price', $line_item);
  commerce_line_item_save($line_item);

  // Save the updated order.
  commerce_order_save($order);
  entity_get_controller('commerce_line_item')
    ->resetCache(array(
    $line_item->line_item_id,
  ));

  // Invoke the product add event with the newly saved or updated line item.
  // TODO: Implement this event see commerce_attribute_cart_product_update()
  // rules_invoke_all('commerce_cart_product_update', $order, $product, $quantity, $line_item);
  // Return the line item.
  return $line_item;
}