WooCommerce get_cart() on null
Asked Answered
R

2

8

Before I updated to WC at 4.3.1 ,I had this code and it worked well

 add_action( 'rest_api_init', function () {
                    register_rest_route( 'px-module-woocommerce', '/px/cart', array(
                            'methods' => 'POST',
                            'callback' => array($this,'ajax_add_to_cart'),
                    ));
});
public function ajax_add_to_cart() {
       $items = WC()->cart->get_cart();
}

now, WC()->cart->get_cart() still return

Call to a member function get_cart() on null

I also tried the global value $woocommerce. But, the result is still the same. Have you any solution? Thanks.

Rollick answered 24/7, 2020 at 15:52 Comment(2)
I dont have much experience in this area but would this reference assist you: docs.woocommerce.com/wc-apidocs/class-WC_Cart.htmlCowslip
Thank you for your reply. Yes that documentation is very useful, but describe only the WC_Cart class and is used through a Singleton in the main class WooCommerce. WC()->cart = WC_Cart.Rollick
R
6

I solved including the functions which are loaded only on the front-end.

public function ajax_add_to_cart() {
            include_once WC_ABSPATH . 'includes/wc-cart-functions.php';
            include_once WC_ABSPATH . 'includes/class-wc-cart.php';

            if ( is_null( WC()->cart ) ) {
                wc_load_cart();
            }

           $items = WC()->cart->get_cart();
}
Rollick answered 29/7, 2020 at 7:45 Comment(3)
This helps a lot comparing to other solutions...!Sklar
This does not resolve the problem. still it says nullCongius
Helps a lot. Thanks.Vipul
S
0

For any future travelers an update on Riccardo's answer for WooCommerce 9.0.0 and up:

function handle_rest_route( WP_REST_Request $request) {
  defined( 'WC_ABSPATH' ) || exit;

  if ( is_null( WC()->cart ) ) {
    /**
     * wc_load_cart() will:
     * - initialize session
     * - initialize customer data from session
     * - initialize empty cart
     *
     * Note, wc()->customer is session based. 
     *  Changes to customer data via this property are 
     *  not persisted to the database automatically.
     */
    wc_load_cart();

    /**
     * wc_load_cart() will initialize the cart with 
     *  a new session, meaning we don't actually
     *  get any existing session data. To have it load
     *  any existing cart session, we call this.
     */
    WC()->cart->get_cart_from_session();
  }

  // If you send your params along with the request in body
  //  you can get them like this.
  $product_id = $request->get_param( 'product_id' );
  $quantity = $request->get_param( 'quantity' );

  // Finally we can now use the cart.
  try {
    WC()->cart->add_to_cart( $product_id, $quantity );
  } catch ( \Exception $e ) {
    return new WP_REST_Response( [ 'success' => false, 'message' => $e->getMessage() ], 500 );
  }

  return new WP_REST_Response( [ 'success' => true ], 200 );
}

There's no need to include cart functions because wc_load_cart() already does that for you. Finally as reminder, if you work with the REST API, to include the nonce wp_create_nonce( 'wp_rest' ) under header X-WP-Nonce.

Sharie answered 22/8 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.