Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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);

etc

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
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'? – GHarping May 25 '12 at 14:21
1  
@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. – GHarping May 25 '12 at 14:46
1  
@GHarping Thank you for asking this question, even though you got a bunch of crap for it. :) – Ryan Jan 9 at 4:42
1  
@Ryan Haha thanks man glad it helped! – GHarping Jan 9 at 15:59
show 6 more comments

1 Answer

up vote 5 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>
    <script>    
       $('#buy').click(function(e) {
          e.preventDefault();
          addToCart(19);
          return false;
       });    

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

This just makes an AJAX GET request to the cart url

/wp/?post_type=product&add-to-cart=[PRODUCT_ID]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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