Skip to content

Instantly share code, notes, and snippets.

@rolandinsh
Created January 16, 2013 17:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rolandinsh/4548975 to your computer and use it in GitHub Desktop.
Save rolandinsh/4548975 to your computer and use it in GitHub Desktop.
WooCommerce: Add a product to cart programmatically via JS or PHP
<!--
source: http://wordpress.stackexchange.com/a/53516/7577
-->
<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>
@parthustad
Copy link

what should i write instead of wp in below line
/wp/?post_type=product&add-to-cart='

@felipemarques
Copy link

not work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment