0
votes

I'm trying to setup a simple way of adding a Woocommerce product to the shopping cart by using an html a tag and after clicking on it redirect to the shopping cart page.

So far I can add the product to the cart but it only appears in the Cart if I force refresh the page. Basically the redirectToCart function is not refreshing the page.

            echo '<a class="btn btn-default" id="buy'.$image->ID.'" href="#">Order This!</a>
            <script>    
                $("#buy'.$image->ID.'").click(function(e) {
                  e.preventDefault();
                  addToCart(37);
                  redirectToCart("?page_id=27")
                  return false;
                 });    

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

                function redirectToCart(cartPage) {
                    window.location.replace(cartPage);
                }
            </script>';

Can anyone help me out with this?

Edit: I'm using a Code Snippet for this, it's basically part of a shortcode script that runs in the frontend.

1

1 Answers

0
votes

Try to replace window.location.replace with window.location.href

window.location.href = cartPage;