Expiry Module

Dubs's picture
Offline
Joined: 11/20/2007
Juice: 79
Expiry Module

Hi Glenn,

Here is some code, which I haven't had time to make Drupal friendly. It works for this particular job, and you could easily adapt it include a settings form for some of the hard-coded values. This particular project was on a very tight deadline, which did not facilitate Drupal-friendly code.

<?php

function uc_product_availability_nodeapi(&$node, $op, $arg3, $arg4){
    switch ($op){
      case 'view':
  $available=_uc_product_availability_is_product_available($node);
  $node->content['sell_price']['#access'] = $available;
        $node->content['add_to_cart']['#access'] = $available;
      break;
    }
}

function _uc_product_availability_is_product_available($node) {
$exp_date = $node->field_expiry_date[0]['value'];
$todays_date = date("Y-m-d");

$today = strtotime($todays_date);
$expiration_date = strtotime($exp_date);

$available = TRUE;

if (!$exp_date=='') {
if ($expiration_date < $today) {
$available = FALSE;
}
if ($expiration_date == $today) {
$time = date("H:i:s");
$expire_time = strtotime("12:00:00");
$now_time = strtotime($time);
/*drupal_set_message("n=".$time." ".$now_time." e=".$expire_time);*/
if ($now_time > $expire_time) {
$available = FALSE;
}
}
}
return $available;
}

function uc_product_availability_cron() {
$result = db_query("SELECT nid FROM {node} WHERE type='race_event'");
while ($row = db_fetch_array($result)) {
$node = node_load($row['nid']);
$available = _uc_product_availability_is_product_available($node);
if (!$available) {
$reference = db_query("DELETE FROM {uc_cart_products} WHERE nid = %d", $node->nid);
if (db_affected_rows()) {
watchdog("Product Availability", $node->title . " has expired. ");
}
}
}
}

Just Racing UK - Selling an Event with Drupal, Ubercart, Views, Dates and some glue By: Dubs (12 replies) Tue, 11/11/2008 - 18:33