2014-05-20 2 views
1

장바구니 행 ID가 product_id 인 장바구니를 만들려고하는데 다음과 같이해야합니다. 장바구니에 두 개의 동일한 제품을 입력 할 수 있지만 너비가 다른 색 속성 . 폭 폼 포스트 나는 'id'와 'color'를 게시합니다. 지금까지 코드 :하나의 제품에 2 행이있는 장바구니 작성

<?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_color]처럼 쇼핑 카트 행 ID를했다. 이제 shoppingcart width와 동일한 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!"; 
     } 
    } 
}