I came across this code for clearing a Woocommerce cart on certain page loads.
But, I wonder is there a way to clear the cart after it has been abandoned?
For triggering only on front page your function needs to look like this:
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;
if ( is_front_page() && isset( $_GET['empty-cart'] ) ) {
$woocommerce->cart->empty_cart();
}
}
function is_front_page() returns true only on front page of your wordpress site. Also, you might detect any other page with function is_page() where you can pass any page title, ID or slug
From this question: Set WooCommerce Cart Expiration
From what I can see, WooCommerce 2.0.20 has a scheduled maintenance job that runs twice/day that will remove any cart sessions from the WordPress options table. The default expiration time is set to 48 hours from the time the user first created the cart. I'm guessing your standard WordPress scheduling routines (and server cron/at jobs) will need to be running properly for this to execute.
AFAIK there is no way to adjust the 48 hour rule via settings. You could write a filter in your theme or in an "adjacent" plugin.
I've adjusted the code a little bit to switch to a 24 hour session. I'm not sure you want to be deleting every few minutes as that has the potential to be performance-heavy.
add_filter('wc_session_expiring', 'so_26545001_filter_session_expiring' );
function so_26545001_filter_session_expiring($seconds) {
return 60 * 60 * 23; // 23 hours
}
add_filter('wc_session_expiration', 'so_26545001_filter_session_expired' );
function so_26545001_filter_session_expired($seconds) {
return 60 * 60 * 24; // 24 hours
}
Related
A lot of the Q&A I'm finding online is about increasing the amount of variations you can make on a woocommerce variable product but I'm trying to limit the amount to 30. The reason being on some client sites I have done, they have like over 1000 variations which makes the product take an age to load. I'm creating a multivendor site so I'm trying to prevent problematic products from the get go. Some of the functions I have tried are:
add_filter( 'woocommerce_ajax_variation_threshold', 'ww_ajax_variation_threshold', 10, 2 );
function ww_ajax_variation_threshold( $default, $product ) {
return 30;
}
and also
function wpse_rest_batch_items_limit( $limit ) {
$limit = 30;
return $limit;
}
add_filter( 'woocommerce_rest_batch_items_limit', 'wpse_rest_batch_items_limit' );
These don't seem to work for me unfortunately. I also want to try and replicate this in the variation popup message. (see below)
Does anyone know if something like this is possible?
If you want to add a limit for "per add" in admin area, add define(WC_MAX_LINKED_VARIATIONS,30) constant in wp-config.php and the popup you mentioned will show this number automatically. But consider that its just a interface limit for creating at time, and will NOT limit total variations user can build on product.
There is a cut-off at 30 variations.
Only from this number on AJAX takes over loading the data.
add_filter( 'woocommerce_ajax_variation_threshold', 'ww_ajax_variation_threshold', 10, 2 );
function ww_ajax_variation_threshold( $default, $product ) {
return 31; // > 30
}
I'm looking to create a PHP code snippet that I would add to my functions.php file but I'm not sure how to proceed.
Someone asked the same question 2 years ago (Woocommerce - disable certain shipping methods based on time of day) but finally opted for a plugin that does that. I already use add-ons for detailed delivery options and I don't want to change or add another one just for this simple function if something can be done easily with php.
So I'm looking to disable a shipping method if the client order after 11AM (website time zone).
So I have this code for now :
if (date('H') > 11) {
$shippingmethod1.disable();
}
Can someone give me the proper code to make it work?
This is quiet simple using woocommerce_package_rates filter hook.
Now you need to find out what is the shipping rate Id reference that you need to target. For that you will inspect the desired shipping method radio button (<input> field) with your browser tools to get value as shown below:
Once you get it you will set that in the code below:
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_time', 10, 2 );
function hide_shipping_method_based_on_time( $rates, $package )
{
// Set your default time zone (http://php.net/manual/en/timezones.php)
date_default_timezone_set('Europe/London');
// Here set your shipping rate Id
$shipping_rate_id = 'local_pickup:13';
// When this shipping method is available and after 11 AM
if ( array_key_exists( $shipping_rate_id, $rates ) && date('H') > 11 ) {
unset($rates[$shipping_rate_id]); // remove it
}
return $rates;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Clearing shipping caches:
You will need to empty your cart, to clear cached shipping data
Or In shipping settings, you can disable / save any shipping method, then enable back / save.
I have a wordpress website that allows non logged-in customers to order with the code below
global $woocommerce;
$woocommerce->session->set_customer_session_cookie(true);
The issue that I am facing right now is that the products left unordered are left in cart because all non-logged-in custumers share the same woocommerce session.
I am thinking of setting an expiry time to the session or the cart so that it can clear products in cart.
The code that I saw in: Woocommerce Set Cart Expiration Interval returns 72 hours in seconds
add_filter('wc_session_expiring', 'filter_ExtendSessionExpiring' );
add_filter('wc_session_expiration' , 'filter_ExtendSessionExpired' );
function filter_ExtendSessionExpiring($seconds) {
return 60 * 60 * 71;
}
function filter_ExtendSessionExpired($seconds) {
return 60 * 60 * 72;
}
Do you know how can I make the filter return every 15 minutes?
Any idea is appreciated. Thanks...
Add the following to your themes functions.php file
add_filter('wc_session_expiring', 28800);
add_filter('wc_session_expiration' , 28800);
28800 is 8 hours in seconds so replace this with 900
If this code you linked is working just change the time calculation: 60 * 15.
Completely new to Woocommerce/wordpress here. On the cart page mydomain.local/cart What filter should I use to show/hide a flat rate shipping method at certain times. From admin I managed to add an extra flat rate method and named it "Next day". Now I would like to show that flat rate method only before 4PM. I tried in functions.php
add_filter( 'woocommerce_package_rates', 'custom_change_shipping', 10);
function custom_change_shipping($rates) {
var_dump($rates);
}
Nothing seems to change and also I cant seem to debug the $rates variable as nothing is outputted when var_dump($rates);. I tried it both anonymous and as an admin but nothing seems to work.
Thanks #LoicTheAztec for confirming whether I was using the right filter. My other problem was that I was not able to output var_dump on the /cart page.
I found out that I need to clear my "cart cache" by going to woocommerce->settings->shipping and in the region, disable then enable again and click on save changes.
By doing this I could see my output from var_dump.
Filter posted earlier by #LoicTheAztec (as requested by #robert)
add_filter( 'woocommerce_package_rates', 'custom_hide_shipping', 100 );
function custom_hide_shipping( $rates ) {
$current_time = date_i18n(('H'), current_time('timestamp')); //Uses Timezone Settings > General
$maximum_time = array (
'time' =>'16'); //4PM
if ($current_time >= $maximum['time']){
unset( $rates['flat_rate:X'] ); // change X to your rate id
}
return $rates;
}
Is is possible to start the WP-Cron randomly between 30 and 60 minutes?
What i have
add_action('my_hourly_event', 'do_this_hourly');
function my_activation()
{
if(!wp_next_scheduled( 'my_hourly_event' ))
{
wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'my_hourly_event');
}
}
add_action('wp', 'my_activation');
function do_this_hourly()
{
// do something
}
Unfortunately the wp_schedule_event doesn't have 30 min and accepts only these intervals: hourly, twicedaily(12H), daily(24H).
In my opinion is a bit strange to have a scheduled event that can change randomly, and probably you should look at a different implementation.
Without discussing your choice I am going to provide a possible answer.
There are plugins with hooks into the Wordpress cron system to allow different time interval.
One solution is to set only one cron every 30 minutes and have a custom function that randomly will be executed or not.
if (rand(0,1)) { ....
For example:
after 30 min the function will be executed (and you have 30 min cron)
after another 30 min the function simply skip the run
for the next 30min will be triggered again and executed(and you have your 1 hour cron).
The problem there is to force the execution at 1 hour (after 1 skip), because you can end up to skip more than +30min. This can be achieved storing the value of the last execution.
Another solution is to have 2 cron (30 min and 1 hour) nearly in the same time and having a custom function that will trigger the 30 min if the 1 hour is not running and so on.
Here is a nice Wordpress cronjob plugin
If you need to store the cron execution safely in a Wordpress table you can use the Wordpress add_option function, with get_option and update_option to get and update its value.
In the code below, I'll be using activation hook instead of wp hook, feel free to use after_switch_theme if it's a theme your code resides in...
You can use wp_schedule_single_event() and simply add a single event to happen randomly between 30-60 minutes every time the event happens ;)
/**
* Registers single event to occur randomly in 30 to 60 minutes
* #action activation_hook
* #action my_awesome_event
*/
function register_event() {
$secs30to60min = rand( 1800, 3600 ); // Getting random number of seconds between 30 minutes and an hour
wp_schedule_single_event( time() + $secs30to60min, 'my_awesome_event' );
}
// Register activation hook to add the event
register_activation_hook( __FILE__, 'register_event' );
// In our awesome event we add a event to occcur randomly in 30-60 minutes again ;)
add_action( 'my_awesome_event', 'register_event' );
/**
* Does the stuff that needs to be done
* #action my_awesome_event
*/
function do_this_in_awesome_event() {
// do something
}
// Doing stuff in awesome event
add_action( 'my_awesome_event', 'do_this_in_awesome_event' );