附加购物车,我如何折扣某些项目

Append cart, how do I discount certain items?

本文关键字:何折扣 项目 购物车      更新时间:2023-09-26

我有一个复选框购物车,我想知道的是,是否有可能使用append()功能折扣某些项目?例如,我有一个选项,可以将某件物品从基本升级到高级,但我不希望该物品适用折扣。

我有一个额外的问题,在附录中每个项目都有一个数量框,我如何排除某些项目没有数量框,而只是一些文本?

这里小提琴

function buildCart(checked) {
    // This one just builds the sidebar on demand. Quite useful if cart contents can be
    // updated many ways (eg in another tab).
    $("#cart_items").empty();
    $('#total').html('0 €'); //default price for your cart
    var cart_total = 0; //default (because your cart shows 10)

    //For each checked item, add a sidebar element
    for (var i = 0; i < checked.length; i++) {
        //build cart item as appropriate.
        var newItem = $("<div>");
        var checkbox = $("input[name='" + checked[i] + "']");
        $test1 = checkbox.parent('td').siblings('td').html(); // check this, you'll have the item name. (the first td)
        $test2 = checkbox.attr('data-val'); // this will have the value on the "data-val" attribute on your checkbox

        cart_total += parseInt($test2); //add the data-val to the cart_total
        newItem.append('<table cellspacing="2" cellpadding="0" border="0" width="320"><tr> <td width="170" valign="top" align="left"> <div class="product_name">' + $test1 + '</div> </div> </td> <td width="30" valign="top" align="center"> <input type="text" class="quantity" value="1" maxlength="2" /> </td> <td width="70" valign="top" align="center"> <div class="product_total_cost price" data-val="' + $test2 + '">' + $test2 + ' €</div> </td> <td width="9" valign="top" align="right"> <div class="remove_product">X</div> </td> </tr> </table>');
        /*************************/
        var discount_pct = parseInt($(".discount").data("val"), 25);
        var discount = -(cart_total * discount_pct / 100);
        $('.discounted').text('- ' + -discount + ' €');
        $('#total').html(cart_total + discount + " €"); //show this value as the total

        $("#cart_items").append(newItem); 
    }
}
HTML

<table cellspacing="2" cellpadding="0" border="0" width="307">
    <tr>
        <td width="190" valign="top" align="left">
            <div class="discount" data-val="10">Discount</div>
        </td>
        <td width="70" valign="top" align="center">
            <div class="discounted" data-val="25">- 0 €</div>
        </td>
    </tr>
</table>

这是一个数据结构问题。

首先,你需要区分有资格享受折扣的产品和没有资格享受折扣的产品。你还必须区分哪些产品可以编辑数量,哪些不能。

当前,您的所有输入看起来像:

<input type="checkbox" name="gold_package" value="Gold package" data-val="40" />

如果产品类型为"折扣"或"升级",我将首先添加一个data-product-type属性来设置。

在您的脚本中,您必须将类型discount的值压入与类型upgrade不同的数组中。您还可以设置项目类型upgrade不具有数量框。

您的购物车总数将是(discount[] *%) + (upgrade[])。