Created
November 30, 2015 18:13
-
-
Save gecugamo/436d7b0b4ed5adea17e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add product to request list | |
function add_product_id($product_id) { | |
$product_ids = get_product_ids(); | |
// If product id is in array, return | |
if (in_array($product_id, $product_ids)) { | |
return; | |
} | |
// Add product id to array | |
array_push($product_ids, intval($product_id)); | |
// Set cookie with updated array | |
setcookie("productIDs", serialize($product_ids), time() + (3600 * 24 * 7), '/'); | |
} | |
// Remove product from request list | |
function remove_product_id($product_id) { | |
$product_ids = get_product_ids(); | |
// If product id is in array, return | |
if (!in_array($product_id, $product_ids)) { | |
return; | |
} | |
// Remove product id from array | |
$product_ids = array_diff($product_ids, array(intval($product_id))); | |
// Set cookie with updated array | |
setcookie("productIDs", serialize($product_ids), time() + (3600 * 24 * 7), '/'); | |
} | |
// Remove product from request list | |
function clear_product_ids() { | |
// Set product ids to an empty array | |
$product_ids = []; | |
// Set cookie with updated array | |
setcookie("productIDs", serialize($product_ids), time() + (3600 * 24 * 7), '/'); | |
} | |
// Return product request list | |
function get_product_ids() { | |
// Convert serialized string back to array | |
$product_ids = unserialize($_COOKIE["productIDs"]); | |
// If productIDs is not an array, return an empty array | |
if (!is_array($product_ids)) { | |
return []; | |
} | |
return $product_ids; | |
} | |
// Checks if current product id is in the product_ids array | |
function check_product_id($product_id) { | |
return in_array($product_id, get_product_ids()); | |
} | |
// Returns number of products in product list | |
function get_request_list_count() { | |
return count(get_product_ids()); | |
} | |
function request_redirect() { | |
if (isset($_GET['redirect'])) { | |
wp_redirect(get_permalink($_GET['redirect'])); | |
} | |
} | |
// Update request list | |
function request_list_update() { | |
// Add | |
if (isset($_GET['add'])) { | |
add_product_id($_GET['add']); | |
request_redirect(); | |
exit; | |
} | |
// Remove | |
if (isset($_GET['remove'])) { | |
remove_product_id($_GET['remove']); | |
request_redirect(); | |
exit; | |
} | |
// Clear | |
if (isset($_GET['clear']) && $_GET['clear'] == '1') { | |
clear_product_ids(); | |
request_redirect(); | |
exit; | |
} | |
} | |
add_action('init', 'request_list_update'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment