Woocommerce Snippets

Add Product To Cart

//Javascript
        addToCart(232);
        function addToCart(p_id) {
                  $.get('/?post_type=product&add-to-cart=' + p_id, function() {
                     // call back
                  });
         }
//PHP
    $product_id = 232;
    $product_cart_id = WC()->cart->generate_cart_id($product_id);

    //WC()->cart->empty_cart();
    if (! WC()->cart->find_product_in_cart($product_cart_id)) {
        // Yep, the product with ID 55 is NOT in the cart, let's add it then!
        WC()->cart->add_to_cart($product_id);
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

Need Help With Code?