【问题标题】:Woocommerce | Plus Minus remove negative numbers (min_value)电子商务 |加减号删除负数(min_value)
【发布时间】:2021-05-27 23:38:21
【问题描述】:

我请你帮忙,我已经好几个小时没有出来了。

我在 Woocommerce 商店页面上设置了数量选择器, 它会自动将商品放入购物车和/或删除它。

我有一个大问题,当我在零上并按 - 数字变为负数时,我会确保它不会低于零。

非常感谢!!

函数.php

// Remove Add To cart Button
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);

// Add our Quanity Input
add_action('woocommerce_after_shop_loop_item', 'QTY');
function QTY()
{
    global $product;
    ?>
    <div class="shopAddToCart">
    <button  value="-" class="minus2"  >-</button>
    <input type="text"
    disabled="disabled"
    size="2"
    value="<?php echo (Check_if_product_in_cart($product->get_id())) ? Check_if_product_in_cart($product->get_id())['QTY'] : 0;
    ?>"
    id="count"
    data-product-id= "<?php echo $product->get_id() ?>"
    data-in-cart="<?php echo (Check_if_product_in_cart($product->get_id())) ? Check_if_product_in_cart($product->get_id())['in_cart'] : 0;
    ?>"
    data-in-cart-qty="<?php echo (Check_if_product_in_cart($product->get_id())) ? Check_if_product_in_cart($product->get_id())['QTY'] : 0;
    ?>"
    class="quantity qty qty-botton"
    max_value = "<?php echo ($product->get_max_purchase_quantity() == -1) ? 1000 : $product->get_max_purchase_quantity(); ?>"
    min_value = <?php echo $product->get_min_purchase_quantity(); ?>
    >

        <button type="button" value="+" class="plus2"  >+</button>

    </div>
                          <?php
}

//Check if Product in Cart Already
function Check_if_product_in_cart($product_ids)
 {

foreach (WC()->cart->get_cart() as $cart_item):

    $items_id = $cart_item['product_id'];
    $QTY = $cart_item['quantity'];

    // for a unique product ID (integer or string value)
    if ($product_ids == $items_id):
        return ['in_cart' => true, 'QTY' => $QTY];

    endif;

endforeach;
}

//Add Event Handler To update QTY
add_action('wc_ajax_update_qty', 'update_qty');

function update_qty()
{
    ob_start();
    $product_id = absint($_POST['product_id']);
    $product = wc_get_product($product_id);
    $quantity = $_POST['quantity'];

    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item):

        if ($cart_item['product_id'] == $product_id) {
            WC()->cart->set_quantity($cart_item_key, $quantity, true);
        }

    endforeach;

    wp_send_json('done');
}

*.js 在页脚上的 jQuery

jQuery(document).ready(function ($) {
      "use strict";

      // Add Event Listner on the Plush button
      $('.plus2').click(function () {

        if (parseInt($(this).prev().val()) < parseInt($(this).prev().attr('max_value'))) {
          $(this).prev().val(+$(this).prev().val() + 1);
          var currentqty = parseInt($(this).prev().attr('data-in-cart-qty')) + 1;

          var id = $(this).prev().attr('data-product-id');

          var data = {
            product_id: id,
            quantity: 1
          };
          $(this).prev().attr('data-in-cart-qty', currentqty);
          $(this).parent().addClass('loading');
          $.post(wc_add_to_cart_params.wc_ajax_url.toString().replace('%%endpoint%%', 'add_to_cart'), data, function (response) {

            if (!response) {
              return;
            }

            if (response) {

              var url = woocommerce_params.wc_ajax_url;
              url = url.replace("%%endpoint%%", "get_refreshed_fragments");
              $.post(url, function (data, status) {
                $(".woocommerce.widget_shopping_cart").html(data.fragments["div.widget_shopping_cart_content"]);
                if (data.fragments) {
                  jQuery.each(data.fragments, function (key, value) {

                    jQuery(key).replaceWith(value);
                  });
                }
                jQuery("body").trigger("wc_fragments_refreshed");
              });
              $('.plus2').parent().removeClass('loading');

            }

          });


        }




      });



      $('.minus2').click(function () {

        $(this).next().val(+$(this).next().val() - 1);


        var currentqty = parseInt($(this).next().val());

        var id = $(this).next().attr('data-product-id');

        var data = {
          product_id: id,
          quantity: currentqty
        };
        $(this).parent().addClass('loading');
        $.post(wc_add_to_cart_params.wc_ajax_url.toString().replace('%%endpoint%%', 'update_qty'), data, function (response) {

          if (!response) {
            return;
          }

          if (response) {
            var url = woocommerce_params.wc_ajax_url;
            url = url.replace("%%endpoint%%", "get_refreshed_fragments");
            $.post(url, function (data, status) {
              $(".woocommerce.widget_shopping_cart").html(data.fragments["div.widget_shopping_cart_content"]);
              if (data.fragments) {
                jQuery.each(data.fragments, function (key, value) {

                  jQuery(key).replaceWith(value);
                });
              }
              jQuery("body").trigger("wc_fragments_refreshed");
            });
            $('.plus2').parent().removeClass('loading');
          }

        });




      });



    });

【问题讨论】:

    标签: javascript php woocommerce


    【解决方案1】:

    感谢文森佐·迪加埃塔诺。

    我是这样解决的:

          $('.minus2').click(function () {
            
            var $minus2 = $(this);
            var oldValue = $minus2.parent().find("input").val();
            
            if (oldValue > 0) {
             $(this).next().val(+$(this).next().val() - 1);
    
            .....
    

    【讨论】:

      【解决方案2】:

      只需在运行该行之前添加一个检查:

      $(this).next().val(+$(this).next().val() - 1);
      

      这将变成:

      if ( $(this).next().val() > 0 ) {
          $(this).next().val(+$(this).next().val() - 1);
      }
      

      另请注意,您为每个产品的每个数量输入使用相同的 ID。 id 属性值在页面上应该是唯一的

      在这里你可以找到一些在添加到购物车表单的数量字段中添加加号和减号按钮的方法(你可以从中获得灵感)

      【讨论】:

      猜你喜欢
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      • 2010-12-13
      • 2012-08-29
      • 2013-01-11
      相关资源
      最近更新 更多