View Single Post
  #1  
Old 11-22-2005, 08:40 PM
 
NuAlpha NuAlpha is offline
 

X-Adept
  
Join Date: Aug 2003
Location: US
Posts: 598
 

Default Display 'delivery by Christmas Eve if ordered by' module

Just realized I hadn't posted any mods in a while. Here is my simple Christmas present to everyone this year (sorry I didn't post it early November). I don't know if someone else has already done this or if something similar is included in the Xcart Xmas module, but here you go.

This code requires that you have an extra field (or a custom database column) of some sort with the earliest availability before it ships. For instance, we have a number of products that ship several days, and sometimes weeks, after being ordered due to product customizations. This would go in place of 'YOUR_EXTRA_FIELD' in the code. This code parses plain english time periods in that 'availability' field of the form "24 hours", "2-3 days", "1 week", "5-7 business days" etc. and uses that to calculate the date that the product must be ordered if it is to reach the customer by Christmas Eve. The code is commented throughout so examine that for further information.

Create the file xmas_delivery_time.php in the directory modules and add the code below for either Xcart 3.5 or Xcart 4.0 into that. Add the include statement include($xcart_dir.'/modules/xmas_delivery_time.php'); at the point indicated.

If you have Xcart 3.5 you need to use this first code snippet and add the include statement inside the function func_select_product() right after $product = func_query_first() in the file func.php.

Code:
<?php # $Id: xmas_delivery_time.php,v 1.1.0 2005/11/22 15:55:45 bjoshua Exp $ ############################################################################## ## ## Christmas Delivery Time ## ## ############################################################################## ## Checks if there is enough time to make a delivery by Christmas Eve and ## ## calculates the earliest date needed to receive the product in time. ## ## This code is self-activating year after year. ## ############################################################################## ## Initially Created: 11/13/2005 if (!defined('XCART_START')) { header('Location: ../'); exit('Access Denied!'); } /** * @author bjoshua * @copyright NuAlpha, Inc. * @todo Take timezones into account on the client side to accurately extend order by time to include time of day. */ ### NOTE ### # The constant 'IS_ROBOT' is for Xcart 4's built-in bot detection or if you have implemented my previously # posted "Search Engine Activity Detector" mod: http://forum.x-cart.com/viewtopic.php?p=114696#114696 ### NOTE ### if ($product && !@IS_ROBOT) { $one_day = 24 * 60 * 60; // Define a day in seconds. $current_timestamp = time(); # Get the current year. $current_year = getdate(); $current_year = $current_year['year']; # November is usually when people begin Christmas shopping. $start_of_season = strtotime('1 November '.$current_year); # 6 PM is the average time deliveries stop (between UPS, USPS, FedEx, etc.) $xmas_eve_timestamp = strtotime('24 December '.$current_year.' 6pm'); # Check if Christmas Eve falls on a weekday. $christmas_eve_date = getdate($xmas_eve_timestamp); # Push back the required arrival date if Christmas Eve falls on a weekend. if ($christmas_eve_date['weekday'] == 'Saturday') $xmas_eve_timestamp -= $one_day; elseif ($christmas_eve_date['weekday'] == 'Sunday') $xmas_eve_timestamp -= $one_day * 2; # NOTE: Remove the 'Saturday' checks if your shipping carrier makes standard deliveries or pickups on that day. # Push forward the date if today is not a weekday, assuming shipments aren't made on weekends. if ($current_date['weekday'] == 'Saturday') $current_timestamp += $one_day * 2; elseif ($current_date['weekday'] == 'Sunday') $current_timestamp += $one_day; if ($current_timestamp < $xmas_eve_timestamp && $current_timestamp > $start_of_season) { # Capture the maximum time needed (with optional minimum) to complete and ship the product and the unit of time used. # Change the regular expression below if you need to, but ensure you keep the named captures assigned correctly. if (preg_match('/(?P<min_time>[0-9]*) ?-? ?(?P<max_time>[0-9]+).*?(?P<unit>hour|day|week|month)/i', $product['YOUR_EXTRA_FIELD'], $matches)) { if (empty($matches['min_time'])) $matches['min_time'] = 0; # Convert units to seconds. For instance, "3-5 Business Days" would be converted to 5 days worth of seconds. switch (strtolower($matches['unit'])) { case 'day': $max_time_till_shipped = $matches['max_time'] * $one_day; $min_time_till_shipped = $matches['min_time'] * $one_day; break; case 'week': $max_time_till_shipped = $matches['max_time'] * 7 * $one_day; $min_time_till_shipped = $matches['min_time'] * 7 * $one_day; break; case 'month': $max_time_till_shipped = $matches['max_time'] * 4 * 7 * $one_day; $min_time_till_shipped = $matches['min_time'] * 4 * 7 * $one_day; break; default: // Default to hours. $max_time_till_shipped = $matches['max_time'] * 60 * 60; $min_time_till_shipped = $matches['min_time'] * 60 * 60; # NOTE: Most online stores ship within 24 hours. If you don't have this default defined in the # extra field then change the two $matches['...'] in the switch default above to the number 24. } # It is assumed that work on the product will only be done on business days, so weekends count as lost time. # Change this value to 6 if work is done on Saturdays or simply comment out condition block if work done all week. if ($max_time_till_shipped > 5 * $one_day) { $weeks_remainder = ($max_time_till_shipped / $one_day) % 7; $num_weeks = ($max_time_till_shipped - ($weeks_remainder * $one_day)) / $one_day / 7; $max_time_till_shipped += $num_weeks * 2 * $one_day + ($weeks_remainder > 5 ? $one_day : 0); } $std_max_delivery_time = 7 * $one_day; // Most carriers make standard deliveries in 7 days or less. $exp_max_delivery_time = 3 * $one_day; // Most carriers make expedited deliveries in 3 days or less. $product['xmas_delivery']['time'] = $xmas_eve_timestamp - $max_time_till_shipped - $std_max_delivery_time; $product['xmas_delivery']['method'] = 'standard'; $order_by_date = getdate($product['xmas_delivery']['time']); # Push back the time if the order by date is not a weekday. if ($order_by_date['weekday'] == 'Saturday') $product['xmas_delivery']['time'] -= $one_day; elseif ($order_by_date['weekday'] == 'Sunday') $product['xmas_delivery']['time'] -= $one_day * 2; # Standard delivery won't make it in time. if ($product['xmas_delivery']['time'] < $current_timestamp) { $product['xmas_delivery']['time'] = $xmas_eve_timestamp - $max_time_till_shipped - $exp_max_delivery_time; $product['xmas_delivery']['method'] = 'expedited'; $order_by_date = getdate($product['xmas_delivery']['time']); if ($order_by_date['weekday'] == 'Saturday') $product['xmas_delivery']['time'] -= $one_day; elseif ($order_by_date['weekday'] == 'Sunday') $product['xmas_delivery']['time'] -= $one_day * 2; } # Expedited delivery also won't make it in time. Maybe with min time needed for turnaround. if ($product['xmas_delivery']['time'] < $current_timestamp && $min_time_till_shipped > 0) { $product['xmas_delivery']['time'] = $xmas_eve_timestamp - $min_time_till_shipped - $exp_max_delivery_time; $product['xmas_delivery']['method'] = 'maybe_expedited'; $order_by_date = getdate($product['xmas_delivery']['time']); if ($order_by_date['weekday'] == 'Saturday') $product['xmas_delivery']['time'] -= $one_day; elseif ($order_by_date['weekday'] == 'Sunday') $product['xmas_delivery']['time'] -= $one_day * 2; } # Even with the minimum time needed, expedited delivery still isn't fast enough. if ($product['xmas_delivery']['time'] < $current_timestamp) { $product['xmas_delivery']['time'] = 0; $product['xmas_delivery']['method'] = 'none'; } } } } ?>

If you have Xcart 4.0 you need to use this second code snippet and add the include statement right after the line include $xcart_dir."/modules/Extra_Fields/extra_fields.php"; in the file product.php. Please note that this module has not been extensively tested in Xcart 4.0.
Code:
<?php # $Id: xmas_delivery_time.php,v 1.1.0 2005/11/22 15:55:45 bjoshua Exp $ ############################################################################## ## ## Christmas Delivery Time ## ## ############################################################################## ## Checks if there is enough time to make a delivery by Christmas Eve and ## ## calculates the earliest date needed to receive the product in time. ## ## This code is self-activating year after year. ## ############################################################################## ## Initially Created: 11/13/2005 if (!defined('XCART_START')) { header('Location: ../'); exit('Access Denied!'); } /** * @author bjoshua * @copyright NuAlpha, Inc. * @todo Take timezones into account on the client side to accurately extend order by time to include time of day. */ if (!empty($extra_fields) && !@IS_ROBOT) { $one_day = 24 * 60 * 60; // Define a day in seconds. $current_timestamp = time(); # Get the current year. $current_year = getdate(); $current_year = $current_year['year']; # November is usually when people begin Christmas shopping. $start_of_season = strtotime('1 November '.$current_year); # 6 PM is the average time deliveries stop (between UPS, USPS, FedEx, etc.) $xmas_eve_timestamp = strtotime('24 December '.$current_year.' 6pm'); # Check if Christmas Eve falls on a weekday. $christmas_eve_date = getdate($xmas_eve_timestamp); # Push back the required arrival date if Christmas Eve falls on a weekend. if ($christmas_eve_date['weekday'] == 'Saturday') $xmas_eve_timestamp -= $one_day; elseif ($christmas_eve_date['weekday'] == 'Sunday') $xmas_eve_timestamp -= $one_day * 2; # NOTE: Remove the 'Saturday' checks if your shipping carrier makes standard deliveries or pickups on that day. # Push forward the date if today is not a weekday, assuming shipments aren't made on weekends. if ($current_date['weekday'] == 'Saturday') $current_timestamp += $one_day * 2; elseif ($current_date['weekday'] == 'Sunday') $current_timestamp += $one_day; if ($current_timestamp < $xmas_eve_timestamp && $current_timestamp > $start_of_season) { # Capture the maximum time needed (with optional minimum) to complete and ship the product and the unit of time used. # Change the regular expression below if you need to, but ensure you keep the named captures assigned correctly. if (preg_match('/(?P<min_time>[0-9]*) ?-? ?(?P<max_time>[0-9]+).*?(?P<unit>hour|day|week|month)/i', $extra_fields['YOUR_EXTRA_FIELD'], $matches)) { if (empty($matches['min_time'])) $matches['min_time'] = 0; # Convert units to seconds. For instance, "3-5 Business Days" would be converted to 5 days worth of seconds. switch (strtolower($matches['unit'])) { case 'day': $max_time_till_shipped = $matches['max_time'] * $one_day; $min_time_till_shipped = $matches['min_time'] * $one_day; break; case 'week': $max_time_till_shipped = $matches['max_time'] * 7 * $one_day; $min_time_till_shipped = $matches['min_time'] * 7 * $one_day; break; case 'month': $max_time_till_shipped = $matches['max_time'] * 4 * 7 * $one_day; $min_time_till_shipped = $matches['min_time'] * 4 * 7 * $one_day; break; default: // Default to hours. $max_time_till_shipped = $matches['max_time'] * 60 * 60; $min_time_till_shipped = $matches['min_time'] * 60 * 60; # NOTE: Most online stores ship within 24 hours. If you don't have this default defined in the # extra field then change the two $matches['...'] in the switch default above to the number 24. } # It is assumed that work on the product will only be done on business days, so weekends count as lost time. # Change this value to 6 if work is done on Saturdays or simply comment out condition block if work done all week. if ($max_time_till_shipped > 5 * $one_day) { $weeks_remainder = ($max_time_till_shipped / $one_day) % 7; $num_weeks = ($max_time_till_shipped - ($weeks_remainder * $one_day)) / $one_day / 7; $max_time_till_shipped += $num_weeks * 2 * $one_day + ($weeks_remainder > 5 ? $one_day : 0); } $std_max_delivery_time = 7 * $one_day; // Most carriers make standard deliveries in 7 days or less. $exp_max_delivery_time = 3 * $one_day; // Most carriers make expedited deliveries in 3 days or less. $product_info['xmas_delivery']['time'] = $xmas_eve_timestamp - $max_time_till_shipped - $std_max_delivery_time; $product_info['xmas_delivery']['method'] = 'standard'; $order_by_date = getdate($product_info['xmas_delivery']['time']); # Push back the time if the order by date is not a weekday. if ($order_by_date['weekday'] == 'Saturday') $product_info['xmas_delivery']['time'] -= $one_day; elseif ($order_by_date['weekday'] == 'Sunday') $product_info['xmas_delivery']['time'] -= $one_day * 2; # Standard delivery won't make it in time. if ($product_info['xmas_delivery']['time'] < $current_timestamp) { $product_info['xmas_delivery']['time'] = $xmas_eve_timestamp - $max_time_till_shipped - $exp_max_delivery_time; $product_info['xmas_delivery']['method'] = 'expedited'; $order_by_date = getdate($product_info['xmas_delivery']['time']); if ($order_by_date['weekday'] == 'Saturday') $product_info['xmas_delivery']['time'] -= $one_day; elseif ($order_by_date['weekday'] == 'Sunday') $product_info['xmas_delivery']['time'] -= $one_day * 2; } # Expedited delivery also won't make it in time. Maybe with min time needed for turnaround. if ($product_info['xmas_delivery']['time'] < $current_timestamp && $min_time_till_shipped > 0) { $product_info['xmas_delivery']['time'] = $xmas_eve_timestamp - $min_time_till_shipped - $exp_max_delivery_time; $product_info['xmas_delivery']['method'] = 'maybe_expedited'; $order_by_date = getdate($product_info['xmas_delivery']['time']); if ($order_by_date['weekday'] == 'Saturday') $product_info['xmas_delivery']['time'] -= $one_day; elseif ($order_by_date['weekday'] == 'Sunday') $product_info['xmas_delivery']['time'] -= $one_day * 2; } # Even with the minimum time needed, expedited delivery still isn't fast enough. if ($product_info['xmas_delivery']['time'] < $current_timestamp) { $product_info['xmas_delivery']['time'] = 0; $product_info['xmas_delivery']['method'] = 'none'; } } } } ?>

Insert the following code into the customer/main/product.tpl Smarty template anywhere you'd like it to show up. Customize the appearance and wording of the below as you'd like.

Code:
{if $product.xmas_delivery} <p style="border:2px dotted DarkRed;"> {if $product.xmas_delivery.method eq 'standard'} To be certain this product will arrive by Christmas Eve, order by <font color="Green">{$product.xmas_delivery.time|date_format:'%A, %B %e'}</font>. {elseif $product.xmas_delivery.method eq 'expedited'} This product should arrive by Christmas Eve if ordered by <font color="Green">{$product.xmas_delivery.time|date_format:'%A, %B %e'} and shipped using an expedited delivery method.</font>. {elseif $product.xmas_delivery.method eq 'maybe_expedited'} It is possible that this could arrive by Christmas Eve if an expedited delivery method is used if ordered by <font color="Green">{$product.xmas_delivery.time|date_format:'%A, %B %e'}</font>. {else} It is not likely that this product will arrive by Christmas Eve if ordered now. {/if} </p> {/if}

Please post any code improvements or fixes if you come across them.

Let me know how it works out!
__________________
X-Cart Pro 4.5.5 Platinum
X-Payments 1.0.6
PHP 5.3.14
MySQL 5.1.68
Apache 2.2.23
Reply With Quote