Skip to content

Instantly share code, notes, and snippets.

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 manchumahara/bcdb7bd4f4e2f1c6f9744da16cbb8d24 to your computer and use it in GitHub Desktop.
Save manchumahara/bcdb7bd4f4e2f1c6f9744da16cbb8d24 to your computer and use it in GitHub Desktop.
WC()->cart null or such error fix for wc 3.6.0 or higher
if ( version_compare( WC_VERSION, '3.6.0', '>=' )) {
require_once( WC_ABSPATH . 'includes/wc-cart-functions.php' );
require_once( WC_ABSPATH . 'includes/wc-notice-functions.php' );
if ( null === WC()->session ) {
$session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' );
// Prefix session class with global namespace if not already namespaced
if ( false === strpos( $session_class, '\\' ) ) {
$session_class = '\\' . $session_class;
}
WC()->session = new $session_class();
WC()->session->init();
}
/**
* For logged in customers, pull data from their account rather than the
* session which may contain incomplete data.
*/
if ( null === WC()->customer ) {
if ( is_user_logged_in() ) {
WC()->customer = new WC_Customer( get_current_user_id() );
} else {
WC()->customer = new WC_Customer( get_current_user_id(), true );
}
}
// Load Cart.
if ( null === WC()->cart ) {
WC()->cart = new WC_Cart();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment