【问题标题】:WooCommerce add_to_cart Shortcode - Product already in CartWooCommerce add_to_cart 简码 - 产品已在购物车中
【发布时间】:2014-01-21 23:49:54
【问题描述】:

我已在自定义帖子类型页面上成功添加了 WooCommerce 短代码“add_to_cart”。

[add_to_cart id="510"]

如果购物车中没有任何内容,它会重定向到购物车,按预期添加产品。但是,如果产品已经在购物车中并且用户单击自定义帖子类型页面上的按钮,它会重定向回自身。

用户不能将多个给定商品添加到购物车中。我如何查看购物车中的内容,以便如果用户单击该按钮,如果该产品已经在其中,它将重定向到购物车?不需要像标准 WooCommerce 产品页面上那样出现任何错误消息。

提前感谢您的任何想法!

亚伦

【问题讨论】:

    标签: php wordpress woocommerce shortcode


    【解决方案1】:

    试试这个,

    当用户点击按钮时,您可以使用此代码检查当前购物车项目。

    global $woocommerce;
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
    
      if($cart_item['product_id'] == $your_product_id){
       //the item already added to the cart
     }
    } 
    

    如果您想将产品强制添加到购物车中,这也将迭代购物车中的所有商品,只需要这个,

    $woocommerce->cart->add_to_cart($product_id,$qty);
    

    希望对你有所帮助..

    【讨论】:

      【解决方案2】:

      我正在寻找一种将购物车按钮添加到我的产品页面底部的方法。最后我添加了以下html代码:

      <form class="cart" method="post" enctype='multipart/form-data'>
          <button type="submit" name="add-to-cart" value="820" class="single_add_to_cart_button button alt">Add to basket</button>
      </form>
      

      值为产品 ID。

      希望这可以帮助遇到同样问题的人

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-14
        • 1970-01-01
        • 1970-01-01
        • 2017-04-27
        • 2017-04-28
        • 2018-09-30
        • 2021-01-31
        • 1970-01-01
        相关资源
        最近更新 更多