Wordpress/Woocommerce Sessions

2019-09-24 11:29发布

问题:

Good afternoon, I have built a website for me client using Wordpress/Woocommerce. the site works great but one problem is that the basket/session doesn't clear after the order is finished. It looks to me that WooCommerce doesn't even have the feature as standard. Working with the Woocommece standard files, whats the best way to kill a session after the checkout process is complete?

Is there a way around this?

回答1:

Your cart should be clearing after checkout so something else may be wrong.

You could create a function with empty_cart() in it that is triggered when payment is complete "just in case". See: http://docs.woothemes.com/wc-apidocs/class-WC_Cart.html

add_filter( 'woocommerce_payment_complete_order_status', 'pg_woocommerce_payment_complete_order_status', 10, 2 );
function pg_woocommerce_payment_complete_order_status ( $order_status, $order_id ) {

    $global $woocommerce;
    $woocommerce->cart->empty_cart();

}


回答2:

You can user this hook :

function custom_empty_cart( $order_id, $data ) {
   // delete current cart item here
}

add_action( 'woocommerce_checkout_order_processed', 'custom_empty_cart', 11, 2 );

Or you can check woocommerce hook list from here.



回答3:

The cart should empty as this feature is standard, empty_cart();

Are you using any cache-plugins? In that case you need to exclude the cart page http://docs.woothemes.com/document/configuring-caching-plugins/

It could also be a problem with the payment-gateway, make sure you have updated to the latest version.



回答4:

i found a similar question but it clears the cart on the click of a button.. http://wordpress.org/support/topic/add-an-empty-cart-button-to-cart-page seems to have worked for some user.. here is the code , it should go in to the function.php file

add_action('init', 'woocommerce_clear_cart_url');
function woocommerce_clear_cart_url() {
    global $woocommerce;
    if( isset($_REQUEST['clear-cart']) ) {
        $woocommerce->cart->empty_cart();
    }
}

this is the code for the button

<input type="submit" class="button" name="clear-cart" value="<?php _e('Empty Cart', 'woocommerce'); ?>" />

im guessing u dont need a button, so in the next page when u want the cart to be empty, u can explicitly call this function _e('Empty Cart', 'woocommerce');

for example if it you would like to clear the cart in a post page whose title is Payment Success

if(is_single('Payment Success'))
_e('Empty Cart', 'woocommerce');

or if its slug is payment-success

is_single('payment-success');
_e('Empty Cart', 'woocommerce');

similarly to check if its homepage you can use

is_front_page(); 

or if its on the wordpress page u can use this

if(is_page( 'Payment Success' ))
    _e('Empty Cart', 'woocommerce');

if(is_page( 'payment-success' ))
    _e('Empty Cart', 'woocommerce');

you can include the above code in the head section of your wordpress site!!

Hope this helps