简体   繁体   English

Woocommerce使用链接,重定向和刷新购物车页面添加产品

[英]Woocommerce Add Product with Link, redirect and Refresh Cart Page

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. 我正在尝试通过使用html标签来设置将Woocommerce产品添加到购物车的简单方法,然后单击它将其重定向到购物车页面。

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. 基本上,redirectToCart函数不会刷新页面。

            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. 编辑:为此,我使用代码段,它基本上是在前端运行的短代码脚本的一部分。

Try to replace window.location. 尝试替换window.location。 replace with window.location. 替换为window.location。 href href

window.location.href = cartPage;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM