为一个产品构建两行购物车

时间:2014-05-20 16:36:39

标签: php wordpress forms shopping-cart

我正在尝试构建购物车,其中购物车行ID为product_id,但我需要它是这样的:在哪里我可以输入两个相同的产品到购物车但宽度不同的颜色属性。我张贴的宽度表格帖子' id'和'颜色'。代码到目前为止:

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

1 个答案:

答案 0 :(得分:0)

所以这就是我所做的: 我做了[id_color]的购物车行ID。所以现在购物车中的商品宽度可以与购物车中的两个不同的行相同(&#39;灰色&#39;彩色&#39;)。后来我用str_replace删除了颜色,所以我可以获得纯粹的产品ID宽度,我可以查询产品信息。 和代码:

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