简体   繁体   中英

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.

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.

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:

<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

<?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;
        }

    });

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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