I am using the Woocommerce plugin to facilitate a small e-commerce part of a site and need to add products to its cart via some call or function rather than using its 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?

  • Close-voted as too localized. Have you tried WooThemes support? – Chip Bennett May 25 '12 at 14:04
  • 3
    @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
  • This question has nothing to do with WordPress specifically. It involves only the WooCommerce Plugin itself - Plugin functionality and custom Plugin functions/code. I could have close-voted as off-topic, but I usually use too localized for questions about commercial products, or products that have commercial support options. – Chip Bennett May 25 '12 at 14:27
  • 12
    @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
  • 11
    @GHarping Thank you for asking this question, even though you got a bunch of crap for it. :) – Ryan Jan 9 '13 at 4:42

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

| improve this answer | |
  • 7
    this is not working anymore (wc v2.1.12). found a solution and shared it here – honk31 Aug 28 '14 at 9:15
  • I've used this tutorial which helped me achieve what I needed: wpharvest.com/… – Dragos Micu Mar 22 at 13:03

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() ) {
| improve this answer | |
  • 3
    How could I call that method via ajax? – Luca Reghellin Dec 11 '13 at 19:21
  • 1
    @Stratboy by using the recommended way of adding AJAX functions in Wordpress: codex.wordpress.org/AJAX_in_Plugins - basically you just insert the 3 lines from the answer in the first php example from the wordpress codex. – CodeBrauer Nov 14 '16 at 16:45

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