You are here

function uc_discounts_init in Ubercart Discounts (Alternative) 7.2

Same name and namespace in other branches
  1. 6.2 uc_discounts/uc_discounts.module \uc_discounts_init()

Implements hook_init().

File

uc_discounts/uc_discounts.module, line 44

Code

function uc_discounts_init() {
  drupal_add_css(drupal_get_path('module', 'uc_discounts') . '/uc_discounts.css');

  // @see uc_discounts_uc_add_to_cart()
  if (isset($_SESSION['uc_discounts_after_add_to_cart'])) {
    $nid = $_SESSION['uc_discounts_after_add_to_cart']['nid'];
    $added_qty = $_SESSION['uc_discounts_after_add_to_cart']['qty'];
    unset($_SESSION['uc_discounts_after_add_to_cart']);
    $items_to_add = $new_product_ids = array();

    // Discounts are checked against the product(s) specified in Discount
    // Application not by Discount Qualifications. Therefore, we have to look up
    // the discount by its required_product_type field and check for discounts
    // on the product(s) associated with it.
    $product = node_load($nid);
    $result = db_query("SELECT * FROM {uc_discounts} d\n                        WHERE d.add_to_cart = :add_to_cart\n                          AND (d.has_activation = :has_activation OR d.activates_on < :activates_on)\n                          AND (d.has_expiration = :has_expiration OR d.expiration > :expiration)\n                          AND d.is_active = :is_active\n                          AND d.discount_type = :discount_type\n                        ORDER BY weight", array(
      ':add_to_cart' => 1,
      ':has_activation' => 0,
      ':activates_on' => REQUEST_TIME,
      ':has_expiration' => 0,
      ':expiration' => REQUEST_TIME,
      ':is_active' => 1,
      ':discount_type' => UC_DISCOUNTS_DISCOUNT_TYPE_FREE_ITEMS,
    ));
    foreach ($result as $discount) {
      if (in_array($product->nid, uc_discounts_get_product_ids_for_discount($discount, UC_DISCOUNTS_GROUPING_QUALIFICATION, TRUE))) {
        $new_product_ids = uc_discounts_get_product_ids_for_discount($discount, UC_DISCOUNTS_GROUPING_APPLICATION, TRUE);
        break;
      }
    }
    if (!empty($new_product_ids)) {
      foreach ($new_product_ids as $id) {
        $potential_product = node_load($id);

        // How many of the item are in the cart? We're only interested in node
        // IDs right now.
        $items = uc_cart_get_contents();
        $qty = 0;
        $source_qty = 0;
        foreach ($items as $item) {
          if ($item->nid == $potential_product->nid) {
            $qty += $item->qty;
          }
          if ($item->nid == $product->nid) {
            $source_qty += $item->qty;
          }
        }
        $target_qty = $discount->discount_amount;
        $qualifying_amount = $discount->qualifying_amount;
        $times_applied = floor($qty / $target_qty);
        $times_to_apply = $target_qty / $qualifying_amount * $added_qty / $target_qty;

        // Make sure max_times_applied is respected.
        if ($times_applied < $discount->max_times_applied || $discount->max_times_applied == 0) {

          // Calculate how many there should be and subtract from what we have.
          $to_add = $target_qty * ($discount->max_times_applied ? min($discount->max_times_applied - $times_applied, $times_to_apply) : $times_to_apply);
        }

        // Don't add items immediately so that uc_cart_get_contents() will
        // behave predictably.
        if ($to_add > 0) {
          $items_to_add[] = array(
            'nid' => $potential_product->nid,
            'qty' => $to_add,
            'data' => array(),
          );
        }
      }
    }
    foreach ($items_to_add as $p) {
      uc_cart_add_item($p['nid'], $p['qty'], $p['data'] + module_invoke_all('add_to_cart_data', $p), NULL, FALSE, FALSE, FALSE);
    }
  }
}