Set a Cookie with PHP – Troubleshooting
-
Hi all, first let me say, Happy New Year!
I am trying to set a cookie in my functions.php file for extending the WooCommerce shop page. Below is what I have for setting the cookie, which does indeed set the cookie. In fact everything I have works fine until you go to the next page, then it goes back to default. I was told a cookie was the best way to handle this but I cannot seem to get the kinks worked out and after two days I figured it be best to just ask for some help.
Here is my cookie stuff:
// Set the cookie function set_results_per_page_cookie() { if (!isset($_COOKIE['sitename_newvisitor'])) { setcookie('shop_pageResults', 1, time()+1209600, '/', 'tacticalminutemen.com', false); } } add_action( 'init', 'set_results_per_page_cookie'); function dl_sort_by_page() { global $dl_page_value; $dl_page_value = $_POST['woocommerce-sort-by-columns']; if (isset($_COOKIE['shop_pageResults'])) { return $dl_page_value; } else { } }
And just so you know whats up, I want to add what I have to that section in full, the point is for the user to select the number of results shown per page, problem is when you go to the next page is defaults.
So here is the whole thing in case someone see’s where I am going wrong.
// Set the cookie function set_results_per_page_cookie() { if (!isset($_COOKIE['sitename_newvisitor'])) { setcookie('shop_pageResults', 1, time()+1209600, '/', 'tacticalminutemen.com', false); } } add_action( 'init', 'set_results_per_page_cookie'); function woocommerce_catalog_page_ordering() { ?> <form action="" method="POST" name="results"> <select name="woocommerce-sort-by-columns" id="woocommerce-sort-by-columns" class="sortby"> <?php $shopCatalog_orderby = apply_filters('woocommerce_sortby_page', array( '' => __('Results per page', 'woocommerce'), '4' => __('4 per page', 'woocommerce'), '12' => __('12 per page', 'woocommerce'), '24' => __('24 per page', 'woocommerce'), )); foreach ( $shopCatalog_orderby as $sort_id => $sort_name ) echo '<option value="' . $sort_id . '" ' . selected( $_SESSION['sortby'], $sort_id, false ) . '>' . $sort_name . '</option>'; ?> </select> <input name="submitbutton" type="submit" value="submit" /> </form> <?php } function dl_sort_by_page() { global $dl_page_value; $dl_page_value = $_POST['woocommerce-sort-by-columns']; if (isset($_COOKIE['shop_pageResults'])) { return $dl_page_value; } else { } } add_filter('loop_shop_per_page','dl_sort_by_page'); add_action( 'woocommerce_pagination', 'woocommerce_catalog_page_ordering', 20 );
Thanks again for your help
- The topic ‘Set a Cookie with PHP – Troubleshooting’ is closed to new replies.