Skip to content

Instantly share code, notes, and snippets.

@bhavik-kiri
Created July 4, 2017 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhavik-kiri/6f29c152f8a4992d764a0df2f6bcda0d to your computer and use it in GitHub Desktop.
Save bhavik-kiri/6f29c152f8a4992d764a0df2f6bcda0d to your computer and use it in GitHub Desktop.
/**
* Get the cart data from the PHP session and store it in class variables.
*/
public function get_cart_from_session() {
// Load cart session data from session
foreach ( $this->cart_session_data as $key => $default ) {
$this->$key = WC()->session->get( $key, $default );
}
$update_cart_session = false;
$this->removed_cart_contents = array_filter( WC()->session->get( 'removed_cart_contents', array() ) );
$this->applied_coupons = array_filter( WC()->session->get( 'applied_coupons', array() ) );
/**
* Load the cart object. This defaults to the persistent cart if null.
*/
$cart = WC()->session->get( 'cart', null );
if ( is_null( $cart ) && ( $saved_cart = get_user_meta( get_current_user_id(), '_woocommerce_persistent_cart', true ) ) ) {
$cart = $saved_cart['cart'];
$update_cart_session = true;
} elseif ( is_null( $cart ) ) {
$cart = array();
}
/* rest of code */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment