Getting items into cart with info from data-attributes
Hello
I'm doing an online shop for the first time so i'm still getting into grasps with this.
The first task, which is retrieving the items info and putting them in the cart doesn't work, therefore i cannot check if the other tasks are also working (like price calculation, increase/decrease price and remove cart display when item is there or is not)
The articles info is inside a custom data-attribute so i'm trying to retrieve it. I've made a fiddle that you can check below:
Here is my HTML code:
- $(document).ready(function(){
-
- //add items to cart
- var itemData = $('#section-shop button').each(function(){
- $(this).on('click', function(){
- $(this).data('shop-listing', function(product, description, price){
- product.appendTo('#shoppingcart-form tbody tr td h3');
- description.appendTo('#shoppingcart-form tbody tr td p');
- price.appendTo('#shoppingcart-form output');
-
- //get input label to increase/decrease number
- var inp = $('#shoppingcart-form tbody tr td:last-child input').val();
-
- //increase/decrease price
- var totalPrice = price;
- totalPrice.appendTo('#shoppingcart-form tfoot ol li:first-child output');
-
- if($('#shoppingcart-form tbody button:first-child').click(function(){
- price++;
- totalPrice++;
- inp++;
- }));
-
- if($('#shoppingcart-form tbody button:last-child').click(function(){
- price--;
- totalPrice--;
- inp--;
- }));
-
- //add VAT
- var VAT = totalPrice * 20/100;
- VAT.appendTo('#shoppingcart-form tfoot ol li:nth-child(2) output');
-
- //Total Price with VAT
- var total = VAT;
-
- //print total price
- total.appendTo('#shoppingcart-form tfoot ol li:last-child output');
- });
- });
- });
-
- //show/hide shopping cart when first item is choosen or is empty
- var shoppingCart = $('#section-shoppingcart');
-
- if(shoppingCart.length > 0) {
- $(this).css('opacity', 1);
- } else {
- $(this).css('opacity', 0);
- }
-
- //remove items from the cart
- $('#shoppingcart-form tbody tr td:first-child div button').on('click', function() {
- $(this).remove(itemData);
- });
- console.log(itemData);
-
- //Checkout button inactive
- $('#shoppingcart-form footer button').on('click', function(e){
- e.preventDefault();
- $(this).attr('disabled', 'disabled').prop('disabled', true).css('opacity', '0.5');
- $(this).text('Processing');
- });
- });
How can i retrieve the items to the shopping cart?
Any help is appreciated.