-1

我想通过存储来自 ORDER 页面的订单值并将它们显示在 CART 页面上来保存产品。这是两个页面的编码。有人可以帮我吗?它适用于 wordpress 主题。

订单页面

<?php
session_start();
/*
TEMPLATE NAME: Order Page
*/


$git_product_id = $_GET['git_product_id'];
$git_required = $_GET['git_required'];
$git_action = $_GET['git_action'];

if ($git_product_id != "" && $git_required != "" && $git_action != "" ){            
    ?>
        <form action="" method="POST" class="add-form">
            <label>
                <?php 

                    switch($git_required){

                        case "name" :
                            // showing Name
                            echo "Account Name:";
                        break; 

                        case "url" :
                            // showing url
                            echo "Web Url:";
                        break;

                    };
                ?>
            </label>
            <input type="text" name="git_required" value="" />
            <input type="hidden" name="git_product_id" value="<?php echo $git_product_id; ?>" />
            <input type="hidden" name="git_action" value="add" />
            <input type="submit" value="Add to cart" class="add-to-cart-button" />
        </form> 
    <?php

} else {

    echo "Sorry an error took place.";
}

?>

==================================================== =========

购物车页面

<?php
session_start();
/*
TEMPLATE NAME: Cart Page
*/

?>

<?php
    $git_product_id = $_POST['git_product_id'];
    $git_required = $_POST['git_required'];
    $git_action = $_POST['git_action'];

    if ($git_product_id != "" && $git_action != "" ){

        switch($git_action){

            case "add" :
                // adding product
                    // checking first if the product is already added
                    if (isset($_SESSION['cart'][$git_product_id])){
                        echo "You have already added this product";                     
                    } else {
                        // I AM NOT SURE IF THIS CODING IS OKAY. PLEASE CHECK THIS
                        $_SESSION['cart'][$git_product_id] = array('product_id' => $git_product_id, 'git_required' => $git_required );
                    }
            break;

            case "remove":
                // removing product
                unset($_SESSION['cart'][$git_product_id]);
            break;

            case "empty" :
                // empty cart
                unset($_SESSION['cart']); 
            break;              

        } 
    }
?>

<?php 

    if ($_SESSION['cart'] != ""):
        foreach($_SESSION['cart'] as $product => $qty) : ?>
        <tr>
            <td>
                <?php // I WANT TO SHOW HERE EACH PRODUCT ID and RESPECTIVE REQUIRED INFO ?>
                <?php // BUT I DON'T KNOW HOW TO DO IT ?>
            </td>

            <td>
                <form action="" method="post">
                    <input type="hidden" name="git_product_id" value="<?php echo $product; ?>" />
                    <input type="hidden" name="git_required" value="<?php echo $qty; ?>" />
                    <input type="hidden" name="git_action" value="remove" />
                    <input type="submit" value="Remove" />
                </form>
            </td>

        <?php endforeach; ?> 
    <?php endif; ?>

    <form action="" method="POST">
        <input type="hidden" name="git_product_id" value="<?php echo $product; ?>" />
        <input type="hidden" name="git_required" value="<?php echo $qty; ?>" />
        <input type="hidden" name="git_action" value="empty" />
        <input type="submit" value="empty" />
    </form>             
4

1 回答 1

3

你写了$_SESSION['cart']['$product']...,但你应该写$_SESSION['cart'][$product]...没有'round $product...

说明:'$product'不是 $product 的值,而只是字符串$product

$product = 5;
var_dump($product);       // -> int(5)
var_dump("$product");     // -> string(1) "5"
var_dump('$product');     // -> string(8) "$product"
于 2013-02-18T13:00:23.743 回答