Created
April 16, 2018 06:13
-
-
Save ridhamdholakia/f2b3682ff238680224eca86d5d3636fc to your computer and use it in GitHub Desktop.
Wordpress Cart Timeout
This file contains 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
function all_woocommerce_cart_expire_timeout(){ | |
global $prod_ids; | |
global $product_timeout; | |
function action_woocommerce_add_to_cart( $array, $int, $int ) { | |
function perform_task() { | |
$start_time = time(); | |
return $start_time; | |
} | |
$cart_item = WC()->cart->get_cart(); | |
foreach ($cart_item as $cart) { | |
$prod_ids = $cart['product_id']; | |
$product_timeout = perform_task(); | |
$booking_id = $cart['booking']['_booking_id']; | |
update_post_meta($booking_id,'_booking_cart_added_time',$product_timeout); | |
} | |
}; | |
add_action( 'woocommerce_add_to_cart', 'action_woocommerce_add_to_cart', 10, 3 ); | |
function action_woocommerce_review_order_before_cart_contents( ) { | |
$i = 0; | |
$cart_item = WC()->cart->get_cart(); | |
function perform_task() { | |
$start_time = time(); | |
return $start_time; | |
} | |
foreach ($cart_item as $cart) { | |
$prod_ids[] = $cart['product_id']; | |
$product_timeout[] = perform_task(); | |
$booking_id[] = $cart['booking']['_booking_id']; | |
} | |
foreach ($booking_id as $value) { | |
$new_time = $product_timeout[$i]; | |
$old_time = get_post_meta($value,'_booking_cart_added_time',true); | |
if($new_time - $old_time > 300){ | |
$call_remove = mp_remove_product_from_cart($prod_ids[$i]); | |
if($call_remove){ | |
//echo "<script>alert('Product Removed From Cart')</script>"; | |
} | |
} | |
$i++; | |
} | |
}; | |
add_action( 'woocommerce_review_order_before_cart_contents', 'action_woocommerce_review_order_before_cart_contents', 10, 0 ); | |
function mp_remove_product_from_cart($id) { | |
$WC = WC(); | |
$prod_to_remove = $id; | |
foreach ( $WC->cart->get_cart() as $cart_item_key => $cart_item ) { | |
$prod_id = $cart_item['product_id']; | |
if( $prod_to_remove == $prod_id ) { | |
$WC->cart->set_quantity( $cart_item_key, $cart_item['quantity'] - 1, true ); | |
break; | |
} | |
} | |
return true; | |
} | |
} | |
add_action('init' , 'all_woocommerce_cart_expire_timeout'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment