WC()->cart is null in custom REST API
-
Hey guys!
WC()->cart
is null in 3.6.1 if running in a method callable by a custom REST API route.Check below code. Why has a _minor_ release led to so many issues?
/** * Routes helper */ class Routes { /** * Adds some actions */ public function __construct() { add_action( 'rest_api_init', array( $this, 'register' ) ); } /** * Registers the routes */ public function register() { $namespace = 'nch'; register_rest_route( $namespace, '/add_to_cart', array( array( 'methods' => 'POST', 'callback' => array( $this, 'add_to_cart' ), ), ) ); } /** * Creates the actual cart * * @param Any $request The current request. */ public function add_to_cart( $request ) { /** * [REDACTED] */ // Empty existing cart. WC()->cart->empty_cart( true ); // THIS CRASHES IN 3.6.1 WC()->session->set( 'cart', array() ); /** * [REDACTED] */ } }
Viewing 10 replies - 1 through 10 (of 10 total)
Viewing 10 replies - 1 through 10 (of 10 total)
- The topic ‘WC()->cart is null in custom REST API’ is closed to new replies.