WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using the Woocommerce plugin to facilitate a small e-commerce part of a site and need to add products to it's cart via some call or function rather than using it's own 'add-to-cart' buttons.

By this I basically mean send Woocommerce a SKU and quantity for example and have the cart update.

sendToCart('123456', 55);


I've looked through the documentation and can't seem to find a reference to this sort of thing. Can anyone suggest how I might achieve this?

share|improve this question

closed as off-topic by Rarst Nov 6 '13 at 18:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions should be specific to WordPress within defined scope (merely happening in its context, such as generic PHP/JS/HTML/CSS, is insufficient). Might be better asked at Stack Overflow or other appropriate site of Stack Exchange network." – Rarst
If this question can be reworded to fit the rules in the help center, please edit the question.

Close-voted as too localized. Have you tried WooThemes support? – Chip Bennett May 25 '12 at 14:04
@Chip Bennett I have yes, they charge $125 dollars just to join unfortunately. This comes under the bracket of both Wordpress and Woocommerce - itself a valid tag here, so is surely valid? Unless I am misunderstanding 'too localized'? – Larry B May 25 '12 at 14:21
@ChipBennett Woocommerce is only for Wordpress so seems odd to catagorise as nothing to do with wordpress. How would one use the Woocommerce tag if not to post a question about Woocommerce? At any rate I have found the answer which I shall post now in case anyone else has the same problem. – Larry B May 25 '12 at 14:46
@GHarping Thank you for asking this question, even though you got a bunch of crap for it. :) – Ryan Jan 9 '13 at 4:42
@Ryan Haha thanks man glad it helped! – Larry B Jan 9 '13 at 15:59
up vote 20 down vote accepted

OK so here's how I solved it in the end. A quick and dirty example, uses JQuery.

<a id="buy" href="#">Buy this!</a>
       $('#buy').click(function(e) {
          return false;

       function addToCart(p_id) {
          $.get('/wp/?post_type=product&add-to-cart=' + p_id, function() {
             // call back

This just makes an AJAX GET request to the cart url

share|improve this answer
this is not working anymore (wc v2.1.12). found a solution and shared it here – honk31 Aug 28 '14 at 9:15

In PHP I managed to do it this way:

global $woocommerce;

The method is in woocommerce/classes/class-wc-cart.php:

     * Add a product to the cart.
     * @param string $product_id contains the id of the product to add to the cart
     * @param string $quantity contains the quantity of the item to add
     * @param int $variation_id
     * @param array $variation attribute values
     * @param array $cart_item_data extra cart item data we want to pass into the item
     * @return bool
    public function add_to_cart( $product_id, $quantity = 1, $variation_id = '', $variation = '', $cart_item_data = array() ) {
share|improve this answer
How could I call that method via ajax? – Stratboy Dec 11 '13 at 19:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.