【问题标题】:Allow to only purchase only one product @ Shopify只允许购买一种产品@Shopify
【发布时间】:2015-01-31 10:24:27
【问题描述】:

如果产品已经在购物车中,我们正在寻找一种解决方案来隐藏“添加到购物车”按钮。这样用户每个产品的订购量不能超过 1 个。

一位来自Codementor 的导师编写了一个函数,如果选定的变体在购物车中,该函数会隐藏“添加到购物车”按钮:

var updateCartButtons = function () {
    if (typeof (window.cartItems) === "undefined") {
        window.cartItems = [];
    }

    var cartItemVariantID, selectedVariantID, selectedVariantName;

    selectedVariantName = $('.swatch :radio:checked').val();

    selectedVariantID = $('#product-select option').filter(function () {
        return $(this).text() === selectedVariantName;
    }).val();

    $('.buymarginsecond').removeClass('addedToCart');
    for (var i = 0, l = cartItems.length; i < l; i++) {
        cartItemVariantID = cartItems[i].variant_id;
        if (cartItemVariantID == selectedVariantID) {
            $('.buymarginsecond').addClass('addedToCart');
            break;
        }
    }
};

我们想改变这个功能,使 主要产品进入购物车后,按钮就会消失。 因此,您只能订购每种产品中的一种,而不能订购产品的不同变体。

【问题讨论】:

    标签: javascript html css shopify liquid


    【解决方案1】:

    您可以在您的 product.liquid 文件中使用此代码,其中显示“添加到购物车”按钮。它将遍历购物车中的标题,如果找到,则将变量设置为 yes。然后检查变量是否为否,不在购物车中并显示添加到购物车。

    {% assign in_cart = 'no' %}
    {% for item in cart.items %}
    {% if item.product.title == product.title %}
    {% assign in_cart = 'yes' %}
    {% endif %}
    {% endfor %}
    {% if in_cart == 'no' %}
    // SHow Add to Cart
    {% endif %}
    

    【讨论】:

      猜你喜欢
      • 2019-05-01
      • 2022-07-11
      • 2017-09-27
      • 1970-01-01
      • 2012-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多