Sounds like a plan.
Okay, so here is what this code is expected to do and how to make it happen.
The expectation of a normal Zen Cart store is that if a product is to be made available for purchase in the future, that it be set as active, but to have an available date in the future. As soon as someone visits the store on that date (ie. Hour of 00:00 based on server time), then the product is able to be purchased. Before that time, it is visible but not purchasable.
With this code change, the product is set as disabled and given a future date. Again, when the site is visited on that date or after, the product becomes visible and purchasable.
To offer the capability of making the product hidden (while disabled) until the available date, the following code change and addition is expected to function.
New file:
includes/init_includes/init_special_funcs_disabled_upcoming.php
Code:
<?php
/**
* @copyright 2018
* @license http://www.zen-cart.com/License/2_0.txt GNU Public License V2.0
* @author mc12345678
**/
if (empty($_SESSION['today_is']) || $_SESSION['today_is'] != date('Y-m-d') || empty($_SESSION['updateExpirations_upcoming']) || $_SESSION['updateExpirations_upcoming'] !== true) {
/**
* require the disabled upcoming products functions, auto-enable disabled product.
*/
require DIR_WS_FUNCTIONS . 'disabled_upcoming.php';
zen_enable_disabled_upcoming();
// Need to set the session variable so that will not execute on every load if other default Zen Cart code sets the today_is session variable.
$_SESSION['updateExpirations_upcoming'] = true;
}
new file:
includes/functions/disabled_upcoming.php
Code:
<?php
/**
* disabled-upcoming products functions
*
* @copyright 2018
* @license http://www.zen-cart.com/License/2_0.txt GNU Public License V2.0
* @author mc12345678
**/
function zen_set_disabled_upcoming_status($products_id, $status) {
$sql = "UPDATE " . TABLE_PRODUCTS . "
SET products_status = " . (int)$status . ", products_date_available = '0001-01-01' WHERE products_id = " . (int)$products_id;
return $GLOBALS['db']->Execute($sql);
}
function zen_enable_disabled_upcoming() {
$date_range = time();
$zc_disabled_upcoming_date = date('Ymd', $date_range);
$disabled_upcoming_query = "SELECT products_id
FROM " . TABLE_PRODUCTS . "
WHERE products_status = 0
AND ((products_date_available <= " . $zc_disabled_upcoming_date . "
AND products_date_available != '0001-01-01'))
";
$disabled_upcoming = $GLOBALS['db']->Execute($disabled_upcoming_query);
if ($disabled_upcoming->RecordCount() > 0) {
while (!$disabled_upcoming->EOF) {
zen_set_disabled_upcoming_status($disabled_upcoming->fields['products_id'], 1);
$disabled_upcoming->MoveNext();
}
}
}
Then to initiate the above code (saved for last to tie it together), add the following new file:
includes/auto_loaders/config.enable_disabled_upcoming.php
Code:
<?php
/**
* disabled-upcoming products auto_loader to execute the operations.
*
* @copyright 2018
* @license http://www.zen-cart.com/License/2_0.txt GNU Public License V2.0
* @author mc12345678
**/
/**
*Load just before the other special functions that may include this product.
*/
$autoLoadConfig[149][] = array(
'autoType'=>'init_script',
'loadFile'=>'init_special_funcs_disabled_upcoming.php',
);
Note, there is no "do this for this product, but not that product" type switch incorporated. Ie. Once the final file is in place, any product that has a products_date_available that has passed will be so enabled and the products_date_available will be set/reset to 0001-01-01 as if it never had such a future date...
Again, the expectation is that product set to disabled and having a future available date will become active and available when the system date becomes the date entered for the available date. This code does not directly support a date/time release which would be a little more involved.
The above code incorporates the comments and changes posted in this thread, some of the comments have been modified to clarify and the layout of the code has been modified to attempt to meet PSR-2 style coding. By inserting the new files in the sequence provided above, no change to the system's operation will be evident until the last file has been applied. An alternate sequence of loading that would still work would be for the functions file to be loaded first then the first file then the third. Loading the third before the first and the first before the second will cause an error with site loading as a result of one or more missing files in the page load sequence.
Bookmarks