2014-05-20 38 views
1

我試圖建立購物車,其中車行id是product_id,但我需要它是這樣的:我可以輸入兩個相同的產品購物車,但寬度不同的顏色屬性。寬度表單帖子發佈'id'和'color'。到目前爲止的代碼:建立購物車與兩行爲一個產品

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

回答

0

所以這是我做過什麼: 我做了購物車的行ID一樣,[id_color。所以現在可以有購物車寬度相同的產品product_id購物車中的兩個不同的行('灰色'和'彩色')。後來我用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!"; 
     } 
    } 
}