• We just launched and are currently in beta. Join us as we build and grow the community.

Dynamic Ajax Shopping Cart

georgisio

Runtime Environment Expert
G Rep
0
0
0
Rep
0
G Vouches
0
0
0
Vouches
0
Posts
163
Likes
132
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
This Dynamic Ajax Shopping Cart is a simple project written in AJAX, jQuery and PHP. This project created to make the shopping cart dynamically without using of internet for the presentation how the shopping cart works. The system has a products that you want to add to the cart and automatically total the expenses that you bought and create a checkout just like from the online shops.

Sample Code

Index.php - This script is for the query of products from the page and sum it all.

  1. $(

    document)

    .ready

    (

    function

    (

    )

    {
  2. var

    Arrays=

    new

    Array

    (

    )

    ;
  3. $(

    '.add-to-cart-button'

    )

    .click

    (

    function

    (

    )

    {
  4. var

    thisID =

    $(

    this

    )

    .parent

    (

    )

    .parent

    (

    )

    .attr

    (

    'id'

    )

    .replace

    (

    'detail-'

    ,

    ''

    )

    ;
  5. var

    itemname =

    $(

    this

    )

    .parent

    (

    )

    .find

    (

    '.item_name'

    )

    .html

    (

    )

    ;
  6. var

    itemprice =

    $(

    this

    )

    .parent

    (

    )

    .find

    (

    '.price'

    )

    .html

    (

    )

    ;

  7. if

    (

    include(

    Arrays,

    thisID)

    )
  8. {
  9. var

    price =

    $(

    '#each-'

    +

    thisID)

    .children

    (

    ".shopp-price"

    )

    .find

    (

    'em'

    )

    .html

    (

    )

    ;
  10. var

    quantity =

    $(

    '#each-'

    +

    thisID)

    .children

    (

    ".shopp-quantity"

    )

    .html

    (

    )

    ;
  11. quantity =

    parseInt(

    quantity)

    +

    parseInt(

    1

    )

    ;
  12. var

    total =

    parseInt(

    itemprice)

    *

    parseInt(

    quantity)

    ;
  13. $(

    '#each-'

    +

    thisID)

    .children

    (

    ".shopp-price"

    )

    .find

    (

    'em'

    )

    .html

    (

    total)

    ;
  14. $(

    '#each-'

    +

    thisID)

    .children

    (

    ".shopp-quantity"

    )

    .html

    (

    quantity)

    ;
  15. var

    prev_charges =

    $(

    '.cart-total span'

    )

    .html

    (

    )

    ;
  16. prev_charges =

    parseInt(

    prev_charges)

    -

    parseInt(

    price)

    ;
  17. prev_charges =

    parseInt(

    prev_charges)

    +

    parseInt(

    total)

    ;
  18. $(

    '.cart-total span'

    )

    .html

    (

    prev_charges)

    ;
  19. $(

    '#total-hidden-charges'

    )

    .val

    (

    prev_charges)

    ;
  20. }
  21. else
  22. {
  23. Arrays.push

    (

    thisID)

    ;
  24. var

    prev_charges =

    $(

    '.cart-total span'

    )

    .html

    (

    )

    ;
  25. prev_charges =

    parseInt(

    prev_charges)

    +

    parseInt(

    itemprice)

    ;
  26. $(

    '.cart-total span'

    )

    .html

    (

    prev_charges)

    ;
  27. $(

    '#total-hidden-charges'

    )

    .val

    (

    prev_charges)

    ;
  28. var

    Height =

    $(

    '#cart_wrapper'

    )

    .height

    (

    )

    ;
  29. $(

    '#cart_wrapper'

    )

    .css

    (

    {

    height:

    Height+

    parseInt(

    45

    )

    }

    )

    ;
  30. $(

    '#cart_wrapper .cart-info'

    )

    .append

    (

    '<div class="shopp" id="each-'

    +

    thisID+

    '"><div class="label">'

    +

    itemname+

    '</div><div class="shopp-price"> $<em>'

    +

    itemprice+

    '</em></div><span class="shopp-quantity">1</span><img src="remove.png" class="remove" /><br class="all" /></div>'

    )

    ;
  31. }
  32. }

    )

    ;
  33. $(

    '.remove'

    )

    .livequery

    (

    'click'

    ,

    function

    (

    )

    {
  34. var

    deduct =

    $(

    this

    )

    .parent

    (

    )

    .children

    (

    ".shopp-price"

    )

    .find

    (

    'em'

    )

    .html

    (

    )

    ;
  35. var

    prev_charges =

    $(

    '.cart-total span'

    )

    .html

    (

    )

    ;
  36. var

    thisID =

    $(

    this

    )

    .parent

    (

    )

    .attr

    (

    'id'

    )

    .replace

    (

    'each-'

    ,

    ''

    )

    ;
  37. var

    pos =

    getpos(

    Arrays,

    thisID)

    ;
  38. Arrays.splice

    (

    pos,

    1

    ,

    "0"

    )
  39. prev_charges =

    parseInt(

    prev_charges)

    -

    parseInt(

    deduct)

    ;
  40. $(

    '.cart-total span'

    )

    .html

    (

    prev_charges)

    ;
  41. $(

    '#total-hidden-charges'

    )

    .val

    (

    prev_charges)

    ;
  42. $(

    this

    )

    .parent

    (

    )

    .remove

    (

    )

    ;
  43. }

    )

    ;
  44. $(

    '#Submit'

    )

    .livequery

    (

    'click'

    ,

    function

    (

    )

    {
  45. var

    totalCharge =

    $(

    '#total-hidden-charges'

    )

    .val

    (

    )

    ;
  46. $(

    '#cart_wrapper'

    )

    .html

    (

    'Total Charges: $'

    +

    totalCharge)

    ;
  47. return

    false

    ;
  48. }

    )

    ;
  49. var

    single_li_offset =

    200

    ;
  50. var

    current_opened_box =

    -

    1

    ;
  51. $(

    '#wrap li'

    )

    .click

    (

    function

    (

    )

    {
  52. var

    thisID =

    $(

    this

    )

    .attr

    (

    'id'

    )

    ;
  53. var

    $this =

    $(

    this

    )

    ;
  54. var

    id =

    $(

    '#wrap li'

    )

    .index

    (

    $this)

    ;
  55. if

    (

    current_opened_box ==

    id)
  56. {
  57. $(

    '#wrap .detail-view'

    )

    .slideUp

    (

    'slow'

    )

    ;
  58. return

    false

    ;
  59. }
  60. $(

    '#cart_wrapper'

    )

    .slideUp

    (

    'slow'

    )

    ;
  61. $(

    '#wrap .detail-view'

    )

    .slideUp

    (

    'slow'

    )

    ;
  62. current_opened_box =

    id;
  63. var

    targetOffset =

    0

    ;
  64. if

    (

    id<=

    3

    )
  65. targetOffset =

    0

    ;
  66. else

    if

    (

    id<=

    7

    )
  67. targetOffset =

    single_li_offset;
  68. else

    if

    (

    id<=

    11

    )
  69. targetOffset =

    single_li_offset*

    2

    ;
  70. else

    if

    (

    id<=

    15

    )
  71. targetOffset =

    single_li_offset*

    3

    ;
  72. $(

    "html:not(:animated),body:not(:animated)"

    )

    .animate

    (

    {

    scrollTop:

    targetOffset}

    ,

    800

    ,

    function

    (

    )

    {
  73. $(

    '#wrap #detail-'

    +

    thisID)

    .slideDown

    (

    500

    )

    ;
  74. return

    false

    ;
  75. }

    )

    ;
  76. }

    )

    ;
  77. $(

    '.close a'

    )

    .click

    (

    function

    (

    )

    {
  78. $(

    '#wrap .detail-view'

    )

    .slideUp

    (

    'slow'

    )

    ;
  79. }

    )

    ;
  80. $(

    '.closeCart'

    )

    .click

    (

    function

    (

    )

    {
  81. $(

    '#cart_wrapper'

    )

    .slideUp

    (

    )

    ;

  82. }

    )

    ;
  83. $(

    '#show_cart'

    )

    .click

    (

    function

    (

    )

    {
  84. $(

    '#cart_wrapper'

    )

    .slideToggle

    (

    'slow'

    )

    ;
  85. }

    )

    ;
  86. }

    )

    ;
  87. function

    include(

    arr,

    obj)

    {
  88. for

    (

    var

    i=

    0

    ;

    i<

    arr.length

    ;

    i++

    )

    {
  89. if

    (

    arr[

    i]

    ==

    obj)

    return

    true

    ;
  90. }
  91. }
  92. function

    getpos(

    arr,

    obj)

    {
  93. for

    (

    var

    i=

    0

    ;

    i<

    arr.length

    ;

    i++

    )

    {
  94. if

    (

    arr[

    i]

    ==

    obj)

    return

    i;
  95. }
  96. }
  97. </

    script>

Style.css - This file is for the dynamic design of the page and help for the nice output of process.

  1. html,

    body{
  2. margin

    :

    0

    ;
  3. padding

    :

    0

    ;
  4. border

    :

    0

    ;
  5. outline

    :

    0

    ;
  6. }
  7. h1{
  8. text-shadow

    :

    1px

    1px

    3px

    ;
  9. }
  10. ul {
  11. list-style-type

    :

    none

    ;
  12. margin

    :

    0

    ;
  13. padding

    :

    0

    ;
  14. overflow

    :

    hidden

    ;
  15. }

  16. .header

    ul{
  17. background

    :

    #000

    ;
  18. }

  19. li {
  20. float

    :

    left

    ;
  21. }

  22. li a {
  23. display

    :

    block

    ;
  24. color

    :

    white

    ;
  25. text-align

    :

    center

    ;
  26. padding

    :

    14px

    16px

    ;
  27. text-decoration

    :

    none

    ;
  28. }

  29. li a:

    hover

    {
  30. background-color

    :

    #95e0eb

    ;
  31. }
  32. #wrap

    {

    width

    :

    100%

    ;

    min-height

    :

    900px

    ;

    top

    :

    0px

    ;

    position

    :

    relative

    ;

    bottom

    :

    0px

    ;

    }
  33. #wrap

    ul{

    margin

    :

    0px

    ;

    padding

    :

    0px

    ;

    width

    :

    700px

    ;

    text-align

    :

    center

    ;

    }
  34. #wrap

    .detail-view

    {
  35. border

    :

    1px

    solid

    #E2E2E2

    ;
  36. border-top

    :

    1px

    solid

    #E2E2E2

    ;
  37. left

    :

    0

    ;
  38. height

    :

    380px

    ;
  39. overflow

    :

    hidden

    ;
  40. clear

    :

    both

    ;
  41. display

    :

    none

    ;
  42. margin-left

    :

    13px

    ;
  43. margin-bottom

    :

    15px

    ;
  44. width

    :

    96%

    ;
  45. }
  46. #wrap

    .detail-view

    .close

    {

    text-align

    :

    right

    ;

    width

    :

    98%

    ;

    margin

    :

    5px

    ;

    }
  47. #wrap

    .close

    a{

    padding

    :

    6px

    ;

    height

    :

    10px

    ;

    width

    :

    20px

    ;

    color

    :

    #525049

    ;

    }
  48. #wrap

    .detail-view

    .detail_images

    {

    float

    :

    left

    }
  49. #wrap

    .detail-view

    .detail_info

    {
  50. float

    :

    right

    ;
  51. font-family

    :

    "Helvetica Neue"

    ,

    Helvetica,

    "Nimbus Sans L"

    ,

    Arial,

    sans-serif

    ;
  52. color

    :

    #525049

    ;
  53. margin-right

    :

    20px

    ;
  54. margin-top

    :

    30px

    ;
  55. text-align

    :

    justify

    ;
  56. width

    :

    250px

    ;
  57. font-size

    :

    12px

    ;
  58. }
  59. #wrap

    .detail-view

    .detail_info

    label{

    font-size

    :

    12px

    ;

    text-transform

    :

    uppercase

    ;

    letter-spacing

    :

    1px

    ;

    line-height

    :

    60px

    ;

    }
  60. #wrap

    .detail-view

    .detail_info

    p{

    height

    :

    110px

    ;

    }
  61. a#show_cart

    {
  62. background

    :

    none

    repeat

    scroll

    0

    0

    #95e0eb

    ;
  63. border

    :

    1px

    solid

    #118da9

    ;
  64. margin-left

    :

    800px

    ;
  65. margin-top

    :

    4px

    ;
  66. cursor

    :

    pointer

    ;
  67. border-radius

    :

    3px

    ;
  68. display

    :

    inline-block

    ;
  69. font

    :

    9px

    /21px

    "Helvetica Neue"

    ,

    Helvetica,

    "Nimbus Sans L"

    ,

    Arial,

    sans-serif

    ;
  70. letter-spacing

    :

    2px

    ;
  71. color

    :

    #525049

    ;
  72. padding

    :

    8px

    ;
  73. text-decoration

    :

    none

    ;
  74. text-transform

    :

    uppercase

    ;
  75. }
  76. .add-to-cart-button{
  77. background

    :

    none

    repeat

    scroll

    0

    0

    #95e0eb

    ;
  78. border

    :

    1px

    solid

    #E8E7DC

    ;
  79. cursor

    :

    pointer

    ;
  80. display

    :

    inline-block

    ;
  81. font

    :

    9px

    /21px

    "Helvetica Neue"

    ,

    Helvetica,

    "Nimbus Sans L"

    ,

    Arial,

    sans-serif

    ;
  82. letter-spacing

    :

    2px

    ;
  83. padding-top

    :

    10px

    ;

    color

    :

    #525049

    ;
  84. margin-top

    :

    15px

    ;
  85. text-transform

    :

    uppercase

    ;
  86. }
  87. .add-to-cart-button

    :

    hover

    {
  88. background

    :

    none

    repeat

    scroll

    0

    0

    #41cc65

    ;
  89. }
  90. .shopp

    {

    background

    :

    none

    repeat

    scroll

    0

    0

    #F8F8F3

    ;

    }
  91. #wrap

    ul li{
  92. list-style-type

    :

    none

    ;
  93. height

    :

    146px

    ;
  94. width

    :

    160px

    ;
  95. margin-left

    :

    10px

    ;
  96. margin-bottom

    :

    15px

    ;
  97. float

    :

    left

    ;
  98. padding

    :

    15px

    0px

    0px

    0px

    ;
  99. font-family

    :

    "LubalGraphBdBTBold"

    ,

    Tahoma;
  100. font-size

    :

    2em

    ;
  101. border

    :

    solid

    #fff

    1px

    ;
  102. overflow

    :

    hidden

    ;
  103. }
  104. .footer

    {

    height

    :

    25px

    ;

    background

    :

    #E2E2E2

    }
  105. #wrap

    ul li:

    hover

    {

    border

    :

    solid

    #f3f4ee

    1px

    ;

    }
  106. #wrap

    ul li div{
  107. height

    :

    31px

    ;
  108. text-align

    :

    center

    ;
  109. width

    :

    160px

    ;
  110. margin-top

    :

    10px

    ;
  111. position

    :

    relative

    ;
  112. bottom

    :

    0px

    ;
  113. padding-top

    :

    6px

    ;
  114. padding-bottom

    :

    4px

    ;
  115. background

    :

    #f3f4ee

    ;
  116. font

    :

    12px

    /21px

    "Helvetica Neue"

    ,

    Helvetica,

    "Nimbus Sans L"

    ,

    Arial,

    sans-serif

    ;
  117. opacity

    :

    0.8

    ;
  118. color

    :

    #525049

    ;
  119. text-shadow

    :

    0px

    1px

    0px

    #555

    ;
  120. }
  121. img#cart

    {

    bottom

    :

    0px

    ;

    position

    :

    fixed

    ;

    margin-left

    :

    30px

    ;

    }
  122. #wrap

    ul li {

    cursor

    :

    pointer

    ;

    }
  123. #cart_wrapper

    {
  124. border

    :

    solid

    #E8E7DC

    1px

    ;
  125. min-height

    :

    120px

    ;
  126. width

    :

    100%

    ;
  127. padding-top

    :

    15px

    ;
  128. display

    :

    none

    ;
  129. background

    :

    #E2E2E2

    ;
  130. font

    :

    12px

    /21px

    "Helvetica Neue"

    ,

    Helvetica,

    "Nimbus Sans L"

    ,

    Arial,

    sans-serif

    ;
  131. position

    :

    relative
  132. }
  133. #Submit

    {
  134. height

    :

    40px

    ;
  135. float

    :

    left

    ;
  136. width

    :

    100px

    ;
  137. background

    :

    #95e0eb

    ;
  138. }
  139. #Submit

    :

    hover

    {
  140. background

    :

    #41cc65

    ;
  141. }
  142. .closeCart

    {

    cursor

    :

    pointer

    ;

    }
  143. button

    {
  144. background

    :

    none

    repeat

    scroll

    0

    0

    #FFFFFF

    ;
  145. border

    :

    1px

    solid

    #E8E7DC

    ;
  146. width

    :

    140px

    ;
  147. cursor

    :

    pointer

    ;
  148. display

    :

    inline-block

    ;
  149. font

    :

    9px

    /21px

    "Helvetica Neue"

    ,

    Helvetica,

    "Nimbus Sans L"

    ,

    Arial,

    sans-serif

    ;
  150. letter-spacing

    :

    2px

    ;
  151. padding-top

    :

    12px

    ;

    color

    :

    #525049

    ;
  152. margin-top

    :

    1px

    ;
  153. border

    :

    solid

    #ccc

    1px

    ;

    padding

    :

    8px

    ;
  154. -webkit-border-radius:

    8px

    ;
  155. -moz-border-radius:

    8px

    ;
  156. margin-left

    :

    20px

    ;
  157. text-transform

    :

    uppercase

    ;
  158. }
  159. button

    :

    hover

    {
  160. background

    :

    none

    repeat

    scroll

    0

    0

    #F8F8F3

    ;
  161. }
  162. .cart-total

    {

    background

    :

    none

    repeat

    scroll

    0

    0

    #F8F8F3

    ;

    }
  163. .shopp

    ,

    .cart-total

    {
  164. border

    :

    solid

    #E8E7DC

    1px

    ;

    padding

    :

    8px

    ;
  165. -webkit-border-radius:

    8px

    ;
  166. -moz-border-radius:

    8px

    ;

    font-size

    :

    12px

    ;
  167. background

    :

    url

    (

    remove.png

    )

    center

    right

    no-repeat

    5px

    ;
  168. border-radius

    :

    8px

    ;
  169. font-family

    :

    "LubalGraphBdBTBold"

    ,

    Tahoma;
  170. margin-top

    :

    3px

    ;
  171. width

    :

    320px

    ;
  172. float

    :

    left

    ;
  173. }
  174. #cart_form

    {

    width

    :

    570px

    ;

    padding-left

    :

    15px

    ;

    }
  175. div.shopp

    span{

    float

    :

    left

    ;

    }
  176. div.shopp

    div.label

    {

    width

    :

    130px

    ;

    float

    :

    left

    ;

    }
  177. div.shopp

    div.shopp-price

    {

    width

    :

    70px

    ;

    float

    :

    left

    ;

    }
  178. .quantity

    {

    float

    :

    left

    ;

    margin-top

    :

    -3px

    ;

    width

    :

    20px

    ;

    }
  179. img.remove

    {

    float

    :

    right

    ;

    cursor

    :

    pointer

    ;

    }
  180. .cart-total

    b{

    width

    :

    130px

    ;

    }

2016-09-08_16_42_04-ajax_shopping_carts.jpg

Hope that you learn from this tutorial and don't forget to Like & Share this project and the website. Enjoy Coding...!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

335,402

335,410

Top