A
A
Ahmad Dusmatov2019-09-26 15:06:32
RESTful API
Ahmad Dusmatov, 2019-09-26 15:06:32

How to add product to cart in Woocommerce via REST?

function wts_add_to_cart()
    {
        $product_id = $_GET['product_id'];
        WC()->cart->add_to_cart( $product_id );
        $data = [ 'name' => 'Product 1', 'product_id' => (int) $product_id ];//для теста
        return $data;
    }

register_rest_route('wts/v1', 'cart/add', [
        'methods' => 'POST',
        'callback' => 'wts_add_to_cart',
    ]);

site.com/wp-json/wts/v1/cart/add?product_id=1762 gives 500 error

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question