Building shopping cart with two rows for one product

I’am trying to build shopping cart where cart row id is product_id but I need it to be like that: where I can enter two same products to shopping cart but width different color attribute. Width form post I post ‘id’ and ‘color’. Code so far:

<?php
    require('../../../wp-config.php');
    mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if(isset($_POST['submit'])) {
    $id=intval($_POST['id']);

    if (isset($_SESSION['cart'][$id]) && $_SESSION['cart'][$id]['color'] == $_POST['color']) {

        $_SESSION['cart'][$id]['quantity']++;
        $link =str_replace( '?action=added_to_cart', '', $_SERVER['HTTP_REFERER'] );
        header('location:' . $link . '?action=added_to_cart');

    } else {

        $sql_s="SELECT * FROM wp_posts WHERE ID=($id)";
        $query_s=mysql_query($sql_s);
        if(mysql_num_rows($query_s)!=0) {
            $row_s=mysql_fetch_array($query_s);
            if ($_POST['color'] == 'gray') {
                $price = get_price($id);
            } else {
                $price = get_price_colored_stones($id);
            }
            $_SESSION['cart'][$row_s['ID']]=array(
                "quantity" => 1,
                "price" => $price,
                "color" => $_POST['color']
            );
            $link =str_replace( '?action=added_to_cart', '', $_SERVER['HTTP_REFERER'] );
            header('location:' . $link . '?action=added_to_cart');
        } else {
            $message="This product id is invalid!";
        }
    }
}
?>

Related posts

Leave a Reply

1 comment

  1. So this is what I did:
    I made shopping cart row id like that [id_color]. So now there can be products in shoppingcart width same product_id two different rows in shopping cart(‘gray’ and ‘colored’). Later I remove color with str_replace, so I can get pure product id width what I can query product information.
    And code:

    <?php
    require('../../../wp-config.php');
    mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if(isset($_POST['submit'])) {
        $id=intval($_POST['id']);
    
        if (isset($_SESSION['cart'][$id  . '_' .  $_POST['color']])) {
    
        $_SESSION['cart'][$id  . '_' .  $_POST['color']]['quantity']++;
        $link =str_replace( '?action=added_to_cart', '', $_SERVER['HTTP_REFERER'] );
        header('location:' . $link . '?action=added_to_cart');
    
    } else {
    
        $sql_s="SELECT * FROM wp_posts WHERE ID=($id)";
        $query_s=mysql_query($sql_s);
        if(mysql_num_rows($query_s)!=0) {
            $row_s=mysql_fetch_array($query_s);
            if (!isset($_POST['color'])){
                $_POST['color'] = 'gray';
            }
            if ($_POST['color'] == 'gray') {
                $price = get_price($id);
            } else {
                $price = get_price_colored_stones($id);
            }
            $_SESSION['cart'][$row_s['ID'] . '_' .  $_POST['color']]=array(
                "quantity" => 1,
                "price" => $price,
                "color" => $_POST['color'],
                "title" => $row_s['post_title']
            );
            $link =str_replace( '?action=added_to_cart', '', $_SERVER['HTTP_REFERER'] );
            header('location:' . $link . '?action=added_to_cart');
        } else {
            $message="This product id is invalid!";
            }
        }
    }