简体   繁体   English

将多种产品添加到购物车

[英]Add multiple products to cart

I have some problem with the logic and any help would be appreciated: 我的逻辑有些问题,我们将不胜感激:

$total_add = $_POST['xTotalNumProduct'];

if(ISSET($_SESSION['cart']['CartTotalNum']) && $_SESSION['cart']['CartTotalNum'] > 0) {
    $CartTotalNum = $_SESSION['cart']['CartTotalNum'];
    $cart = $_SESSION['cart'];

    for ($i=1; $i<=$total_add; $i++) {

        for ($x=1; $x<=$CartTotalNum; $x++) {
            if ($cart['ItemId'.$x] == $_POST['xPdt'.$i.'Id']) { // this will only check the first key ie $cart['ItemId1]
                $cart['ItemQty'.$x] = $_POST['xPdt'.$i.'Qty'];
            }
            else {
                $CartTotalNum = $CartTotalNum + 1;
                $cart['ItemId'.$CartTotalNum] = $_POST['xPdt'.$i.'Id'];
                $cart['ItemQty'.$CartTotalNum] = $_POST['xPdt'.$i.'Qty'];
            }
        }
    }
    $cart['CartTotalNum'] = $CartTotalNum;

} else {
    $cart = array();
    for ($i=1; $i<=$total_add; $i++) {
        $cart['ItemId'.$i] = $_POST['xPdt'.$i.'Id'];
        $cart['ItemQty'.$i] = $_POST['xPdt'.$i.'Qty'];      
    }
    $cart['CartTotalNum'] = $total_add;
}

The problem with the above script is that it only checks the $cart['ItemId1] and if not equal it will add to cart without checking $cart['ItemId2], $cart['ItemId3] etc. 上述脚本的问题在于,它仅检查$ cart ['ItemId1],如果不相等,它将添加到购物车中,而不检查$ cart ['ItemId2],$ cart ['ItemId3]等。

How can I fix that? 我该如何解决?

This is incredibly bad code: 这是非常糟糕的代码:

            $cart['ItemQty'.$x] = $_POST['xPdt'.$i.'Qty'];
                          ^^^^^

Why build dynamic keys? 为什么要建立动态键? You can use a multi-dimensional arrays quiet easily: 您可以轻松使用安静的多维数组:

   $_SESSION['cart'][$itemID]['quantity'] = $_POST[...];

Keying your cart by the itemID allows all of the item's cart data to be kept in one place, instead of scattered everywhere. 通过itemID来对购物车进行键控,可以将所有购物车数据保存在一个地方,而不是分散在各处。

And note that similar constructs can be used in your form field names, eg 并注意,可以在表单字段名称中使用类似的构造,例如

<input type="text" name="foo[bar][baz][42]" ... >

would give you 会给你

$_REQUEST['foo']['bar']['baz'][42]

to work with when the form's submitted. 在提交表单时使用。

This question was asked years ago, but for anyone running into it now, this is what worked for me. 这个问题是几年前提出的,但是对于现在遇到这个问题的任何人来说,这都是对我有用的。

I put this HTML in my page: 我将此HTML放在页面中:

<form method="post" action="/">

    <input type="hidden" name="E24MT1260" value="23">
    <input type="hidden" name="ACFIT60060015" value="14">

    <input type="hidden" name="programatic_add_to_cart" value="true">
    <input type="submit" value="Add to cart">

</form>

And added this to my functions.php 并将其添加到我的functions.php

<?php

    add_action('wp_loaded', function() {
        global $woocommerce;

        if (!empty($_POST) && !empty($_POST['programatic_add_to_cart'])){ 
            global $woocommerce;

            foreach ($_POST as $sku => $quantity) {

                $product_id = wc_get_product_id_by_sku($sku);
                $woocommerce->cart->add_to_cart($product_id, $quantity);
            }

            wp_redirect( '/cart' );
            exit;
        }

    });

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM