将多个产品添加到购物车 - WooCommerce

时间:2017-07-20 20:12:33

标签: javascript jquery wordpress woocommerce

我已使用JavaScript设置了报价批量购买报价表单。它完成了所有的计算并显示了我需要的数据,但我不能让WooCommerce允许我创建一个网址,每个网址最多可以将3个产品添加到购物车中,每个产品都有自己的数量。

例如,客户可能会输入:

productID:数量 产品15:9 产品17:12 产品19:3

目前,每个产品都有自己的添加到购物车按钮使用WooCommerce网址结构:'。com?add-to-cart = Product15& quantity = 9'

我确实找到this当我将代码放入functions.php时,我允许添加多个产品,但数量相同。所以这允许我做'.com?add-to-cart = Product15,Product17& quantity = 9'并将9个Product15和Product17添加到购物车。但是,如果我尝试这个'.com?add-to-cart = Product15,Product17& quantity = 9,12',这两种产品都会被添加到购物车中,但每个产品的数量都是9。

如果每次点击“添加到购物车”按钮时页面没有重新加载,这不会是一个大问题。重新加载意味着表单变为空白,客户必须重新输入其信息。我确实尝试使用URL参数来设置Input的Value属性,但是对于这样一个简单的问题,我找不到简单的答案。我打算使用“获取报价”按钮为每个“添加到购物车”按钮生成链接,其中args用于在页面重新加载时重新填充报价表单,例如生成的URL看起来像这样'.com?add-to -Cart = Product15&安培;数量= 9&安培; P17qty = 12&安培; P19qty = 3'

所以我的问题: a)如何创建单个按钮,将多个产品添加到购物车中,并为每个产品定义用户定义的数量? -要么- b)如何将我的URL参数输入到输入区域?使用WordPress。

这是报价表的当前代码,对不起,如果它不漂亮的话。我还在学习,所以我确信它可以用更简洁的方式完成:)

HTML:

<H2 STYLE="COLOR:WHITE;MARGIN-TOP:0;">YOUR FULL QUOTE - £<span id="totalquote"></span></H2>
<table>
    <tbody>
        <tr>
            <td>
                <p>2 Pack Qty:<br><input type="text" id="qty2pks" value="0"><br>Quote - £<span id="2pk">0</span></p>
            </td>
            <td>
                <button>Add to Cart</button>
            </td>
        </tr>
        <tr>
            <td>
                <p id="4pksqty">4 Pack Qty:<br><input type="text" name="qty4pks" value="0" id="qty4pks"><br>Quote - £<span id="4pk">0</span></p>
            </td>
            <td>
                <a href="#" id="thislink">Add to Cart</a>
            </td>
        </tr>
        <tr>
            <td>
                <p>1 Pack Qty:<br><input type="text" id="qty1pks" value="0"><br>Quote - £<span id="1pk">0</span></p>
            </td>
            <td>
                <button>Add to Cart</button>
            </td>
        </tr>
        <tr>
            <td>
                <P><button onclick="runQuote()" value="Get Quote">Get Quote</button></P>
            </td>
        </tr>
    </tbody>
</table>

SCRIPT:

<script>

var qty1pk = document.getElementById('qty1pks').value;
var qty2pk = document.getElementById('qty2pks').value;
var qty4pk = document.getElementById('qty4pks').value;
var price1pk = 9.99;
var price2pk = 14.99;
var price4pk = 19.99;
var discdec1 = 0.1;
var discdec2 = 0.125;
var quote2pk = (price2pk - (price2pk * discdec1)) * qty2pk;
var quote4pk = (price4pk - (price4pk * discdec1)) * qty4pk;
var quote1pk = (price1pk - (price1pk * discdec1)) * qty1pk;
var fullquote = quote4pk + quote2pk + quote1pk;
var fullquotedec = parseFloat(Math.round(fullquote * 100) / 100).toFixed(2);

document.getElementById("2pk").innerHTML = quote2pk.toFixed(2);
document.getElementById("4pk").innerHTML = quote4pk.toFixed(2);
document.getElementById("1pk").innerHTML = quote1pk.toFixed(2);
document.getElementById("totalquote").innerHTML = fullquotedec;

function runQuote() {
    var qty1pk = document.getElementById('qty1pks').value;
    var qty2pk = document.getElementById('qty2pks').value;
    var qty4pk = document.getElementById('qty4pks').value;
    var price1pk = 9.99;
    var price2pk = 14.99;
    var price4pk = 19.99;
    var discdec1 = 0.1;
    var discdec2 = 0.125;
    if (qty2pk <= 4) {
        var quote2pk = price2pk * qty2pk;
    } else if (qty2pk <= 10) {
        var quote2pk = (price2pk - (price2pk * discdec1)) * qty2pk;
    } else if (qty2pk >= 11) {
        var quote2pk = (price2pk - (price2pk * discdec2)) * qty2pk;
    }
    if (qty4pk <= 4) {
        var quote4pk = price4pk * qty4pk;
    } else if (qty4pk <= 10) {
        var quote4pk = (price4pk - (price4pk * discdec1)) * qty4pk;
    } else if (qty4pk >= 11) {
        var quote4pk = (price4pk - (price4pk * discdec2)) * qty4pk;
    }
    if (qty1pk <= 4) {
        var quote1pk = price1pk * qty1pk;
    } else if (qty1pk <= 10) {
        var quote1pk = (price1pk - (price1pk * discdec1)) * qty1pk;
    } else if (qty1pk >= 11) {
        var quote1pk = (price1pk - (price1pk * discdec2)) * qty1pk;
    }
    var fullquote = quote4pk + quote2pk + quote1pk;
    var fullquotedec = parseFloat(Math.round(fullquote * 100) / 100).toFixed(2);
    var url = "http://www.handiiproducts.com/professionalmultibuy-options/?add-to-cart=35&quantity=" + encodeURIComponent(qty4pk);
    var element = document.getElementById('thislink');
    element.setAttribute("href", url);

    document.getElementById("2pk").innerHTML = quote2pk.toFixed(2);
    document.getElementById("4pk").innerHTML = quote4pk.toFixed(2);
    document.getElementById("1pk").innerHTML = quote1pk.toFixed(2);
    document.getElementById("totalquote").innerHTML = fullquotedec;

}

</script>

0 个答案:

没有答案